Hey guys what's up? I'm new to the course and very interested in learning however I can. I'm looking at the second assignment which asks to create a program to find the 1000th prime number and I'm stuck thinking on it. Can someone help me out?
Hello sora , you replied under my question, I'm sorry I can't help , i think we're talking about two different sections
To find whether a number is prime or not, we need to see whether it is divisible by a number besides 1 or itself. A good place to start might be making a function that checks whether a given number is prime or not. After that function, you could make a loop that keeps track of how many prime numbers have been found, and stop at the 1,000 mark. There are also many ways to go about the same problem.
last time I tried it kept going none stop...
sorry I thought it was the same question... okay i've finally succeeded in the prime number generator but I have to ask is there a way to number them in the list so I wouldn't have to count the "1000th" prime? I see it's 7919 but I had to to actually count to see that!
Python has a function len() which returns the length of whatever is put in the parenthesis: len( yourList ) There's one little math fact that can REALLY speed your program up: All non-prime numbers will factor down to prime numbers.
I ran a counter to keep track of where I was in the calculation. Here's my blog post about it. http://exemplaretutoring.com/2016/02/01/finished/ my program lists all the primes up to 7919 in this version (skipping 2) To display the 1000th prime number, i just need to write... if count == 999: print x I wrote comments with // like java which is what I'm used to. forgot python is # for comments
Pretty slow program, huh? There are simple ways to get it to run much faster: time ./primes2.py 1000 7919 real 0m0.028s user 0m0.016s sys 0m0.008s
yupp. it is pretty damn slow. I've seen some other solutions but they were beyond what I knew at the time. Also there's a solution involving sqrt I see online
I'm pretty sure when you test whether a number has any factors or not, you only need to check the numbers (equal maybe) or below the sqrt.
dcaligiuri, if you want to play with it, read the last part of my first post.
seems interesting. I'll look into it soon. Right now I'm working on the Biology String problem - which is of special interest to me since I majored in Biology!
Join our real-time social learning platform and learn together with your friends!