Hello, I am pretty new at coding, and I'm having issues with the primes exercise.I understand the concept, and I think I found a way to test if a number is prime, but I think the program keeps looping and its calculating all prime numbers. how can i make it stop at the 1000th and print it? any suggestions? here is my code x = 0 x < 5000 ans = 2 while ((4*ans) - 1) % ans != x: ans = ans + 1 print ans
Hoot! You just asked your first question! Hang tight while I find people to answer it for you. You can thank people who give you good answers by clicking the 'Good Answer' button on the right!
Hi GusCal, could you please let us know what the x<5000 part means? Also, in your while loop, are you using the equation that professor Grimson put on the board? If you look in the pdf document containing this assignment you will see that he gives a couple guidelines as to how you could more easily determine if a number is prime. It involves testing each possible factor of the prime to see if the number has any factors. Also, the only reason why you keep looping must be that the conditions of your while loop while((4*ans)-1)%ans!=x, must always not equal x. There is no other reason why your program would keep looping. Try changing the conditions of your loop.
hi, the x<5000 it was a futile effort to prevent the infinite looping. I thought that by noting that x was lesser value than 5000 it would at some pint stop and give some form of answer. as fas as the equation it was actually empirically produced. I overlooked the gide lines i guess.
Interesting. Your problem is that you are increasing ans in the while loop, while trying to put a limit on x. Try nesting a while loop within a while loop to control the number being modularly divided by and the amount of prime. Get back to me if you need further help.
I will try that. thank you \m/
Join our real-time social learning platform and learn together with your friends!