I am just starting on trying to program so bear with me. I am working on the problem set 1a of the intro to cs open course ware. I will attach the code I have so far at the bottom. It will calculate right to a certain number but won't once I go so far. My question is what am I doing wrong? Am I going about it the completely wrong way? number=2 primecount=0 while primecount<500: if (number!=2) and (number%2 == 0) or (number!=3) and (number%3 == 0) or (number!=5) and (number%5 == 0) or (number!=7) and (number%7 == 0) or (number!=11) and (number%11 == 0) or (number!=13) and (number%13 == 0
Ok, so the whole code did not show up so I am going to attach the file.
please use a code pasting site: - http://dpaste.com - http://pastebin.com - https://gist.github.com/ - http://pastie.org - http://codepad.org - http://ideone.com - http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste. http://dpaste.com/1536542/
what is the definition of a prime number?
Sorry about the attachment. I will use a pasting site if I have any further questions. And it is a number that is only divisible by 1 and itself. How would I test for this though? As I am typing this I was googling prime numbers and saw something called AKS primality test. Could I use this to do the calculations instead of the mess I created?
The testing that you're doing is all literal values. You need to stop and think about how you could do this with a formula and/or a data structure. Hint: there's a fact about factoring non-prime numbers that can make your program run faster.
I fixed the code so it now runs along the same line you started on. It seems to perform it's job but i only checked quickly. If you have any questions about the changes or anything I am available.
You're doing it wrong. Suppose you have a list of primes less than `n`. How would you check that `n` is prime using that list?
As you find new primes, continue to add them to your prime list.
Consider the following: ``` primes = [] # list of primes primecount = 500 # number of primes to find number = 2 # starting point while len(primes) < primecount: # write something here to determine if number is prime if number_is_prime: # add number to the list of primes # print out number number += 1 #move onto the next number ```
Join our real-time social learning platform and learn together with your friends!