has anyone tried doing the prime number problem 1? I can generate a list but am having trouble with the rest.
ok, so I tried this. Thought it was in response to my post but now I cannot find it. but it goes over 1000 and doesn't stop until after 7000+. Ideas? #prime numbers to 1000 prime_count = 1 start_number = 2 number_to_check = 2 while prime_count <= 1000: result = number_to_check % start_number if result > 0: start_number +=1 elif result == 0: if start_number == number_to_check: print number_to_check number_to_check +=1 prime_count +=1 start_number =2 else: number_to_check +=1 start_number = 2
I think that if you rewrite your if/else with an eye towards eliminating duplicate statements, your problem will show itself. If you want to speed this code up, 2 is the only even prime number.
If you want something that just 'works', change this line: while prime_count <= 1000: into this: while number_to_check <= 1000: If you want to learn about how to improve that function, take a look here: http://pastebin.com/2Qdvb60c
pls use a code pasting site to post code - be sure to select Python syntax highlighting if it has that option. use line numbers when referring to specific parts of the code. http://dpaste.com http://pastebin.com http://pastie.org http://codepad.org
Yay! Thanks bdean20. I will also look at the links you and bwCA sent. would like to understand it better.
Join our real-time social learning platform and learn together with your friends!