(ps1b) My program seems to be stuck in an infinite loop, but I'm not sure how this is happening. Code: http://dpaste.com/533437/
what input does it give you an infinite loop?
You're asking for input from the user with raw_input(), which returns a string rather than an integer.
Yeah, change line 7 to n = int(raw_input()) and it should be fixed But I'm pretty sure there's also a logical error... How are you checking whether primeCandidate is actually prime?
If the number is divisible by a whole number then the number is not added to the sum and primeCandidate is incremented. If the divisor is greater than half of primeCandidate, that means that no whole number evenly divides into it, making it prime. There does still seem to be a problem with my program logically, so please let me know if see a problem in that logic. Right now my ratio converges to zero instead of one. Thanks.
You're taking the log of the sum rather than the sum of the logs. Also, you're missing 3 from your prime numbers. You can fix that by initiating primeSum with log(2) + log(3) at the beginning.
You're also missing the last prime number when the input is prime. For example, when I enter a prime number 7919, that number doesn't get added in.
one way to make your program faster at least is to skip all even numbers since no even numbers past 2 are prime. thatll cut the amount of numbers processed in half. another suggestion would be to write a separate function to detect if the number is prime or not something like def isPrime(a): i = 0 while (i<a/2+1) if (a % i == 0): return False else: return True
Thanks so much for the help everyone!
Join our real-time social learning platform and learn together with your friends!