A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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/

  • This Question is Closed
  1. Shriram
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    what input does it give you an infinite loop?

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You're asking for input from the user with raw_input(), which returns a string rather than an integer.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks so much for the help everyone!

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.