(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/
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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?
Not the answer you are looking for? Search for more explanations.
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
i = 0