A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
(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/
anonymous
 5 years ago
(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/

This Question is Closed

Shriram
 5 years ago
Best ResponseYou've already chosen the best response.0what input does it give you an infinite loop?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0You're asking for input from the user with raw_input(), which returns a string rather than an integer.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Yeah, 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0If 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0You'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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0You'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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0one 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks so much for the help everyone!
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.