A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
I did problem set 1a after the second lecture and was able to get something to work. After the third lecture, I went back and tried to incorporate some of the new stuff. However, I seem to have a snag that I can't seem to figure out. It does great until x=25. It finds the 24th prime (change x to 24), but when x is 25 it returns 95 as a prime number.
Any thoughts?
x = 25 ##to stop when reaching the Xth prime number
count = 1 ##count starts at one since it will miss 2, the first prime #
prime_n = 1 ##testing number
while (count<x): ##to stop when reaching the Xth prime number
prime_n +=
anonymous
 5 years ago
I did problem set 1a after the second lecture and was able to get something to work. After the third lecture, I went back and tried to incorporate some of the new stuff. However, I seem to have a snag that I can't seem to figure out. It does great until x=25. It finds the 24th prime (change x to 24), but when x is 25 it returns 95 as a prime number. Any thoughts? x = 25 ##to stop when reaching the Xth prime number count = 1 ##count starts at one since it will miss 2, the first prime # prime_n = 1 ##testing number while (count<x): ##to stop when reaching the Xth prime number prime_n +=

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Full Code: x = 25 ##to stop when reaching the Xth prime number count = 1 ##count starts at one since it will miss 2, the first prime # prime_n = 1 ##testing number while (count<x): ##to stop when reaching the Xth prime number prime_n += +2 for test_divisor in range (2,prime_n/2): ## stopping the test at one half of the number if (prime_n%test_divisor == 0): ## checks for remainder prime_n += +2 ## if there is a remainder moves on to the next prime count += + 1 ##moves the count to find the xth prime number print 'the #', count,'prime number is', prime_n

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0if you use dpaste.com or something similar to post your code it is easier for everyone. first  don't forget that the range function will return a list that does NOT contain the "stop" number  prime_n/2 in this case. next  it looks like you are changing the number you are testing within the loop that changes the divisor  that can lead to incorrect results  putting print statements in your is a good debugging tool, i've put a print statement in your code that will let you see what is happening http://dpaste.com/545075/.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks a ton. I see what the problem is now. It is moving on to the next prime and continuing with the same number for the test divisor. Should have done this check before posting, but I'm still learning. Thanks
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.