A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

I am entering my code for problem set 1 problem 1, ( http://pastebin.com/nzG9Nva7, under v:3.0), and recieve 7921 instead of what it should be (which is 7919). Can someone point me in the right direction as to what I'm doing wrong?

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

    The error is with your code at line 57 prime_number_candidate=prime_number_candidate+2 So even though your loop ends at the correct iteration, you actually incremented prime_number_candidate before the last iteration ends. That's why you get your last prime number (7919) + 2. There are several ways to solve this, however each solution have different merits/faults. 1. subtract 2 from your prime_number_candidate after you exit the outer while loop. To get your last prime number. 2. use another variable (lastPrime) to store the last prime number found (not the next prime_number_candidate), and print the variable lastPrime . 3. initialize your loop differently such that you increment prime_number_candidate as the first (or one of the first) instructions in your outer while loop. This would cause prime_number_candidate to be equal to the prime number (if the number is prime).

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

    Man, It's always the little things that I miss...oh well. I chose number three, and have implemented it. Thanks for the help stuyboys!

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

    You're welcome.

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