Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
I'm working on the first problem set and came up with the following code, but cannot figure out how to get it to achieve the desired outcome of generating the first 1000 prime numbers. Any suggestions?
>>> y=7920
>>> x=2
>>> i=2
>>> while x<y and i<x:
if x%i==0:
i=i+1
if x%i!=0:
print x
if x%i!=0:
i=i+1
if x%i!=0:
print x
x=x+1
 10 months ago
 10 months ago
I'm working on the first problem set and came up with the following code, but cannot figure out how to get it to achieve the desired outcome of generating the first 1000 prime numbers. Any suggestions? >>> y=7920 >>> x=2 >>> i=2 >>> while x<y and i<x: if x%i==0: i=i+1 if x%i!=0: print x if x%i!=0: i=i+1 if x%i!=0: print x x=x+1
 10 months ago
 10 months ago

This Question is Open

e.mccormickBest ResponseYou've already chosen the best response.1
x=2 i=2 while x<y and i<x: Therefore, i is not less than x and the loop exits. Tip: If you had put a test print at the very top of your loop, you would have seen it did nothing, which would show you that the loop was ending prematurely. Tip2: A code posting service like pastebin or the gists on github is really useful. People can't copy and paste your code from here to test it. The lines collapse and the code gets messed up. But if you use those, the code is easily shared.
 10 months ago

bwCABest ResponseYou've already chosen the best response.0
give your variables descriptive names  it makes it easier to read and understand for ppl who didn't write it http://dpaste.com/1256761/
 9 months ago

nsanBest ResponseYou've already chosen the best response.0
Hi Guys, Thank You for replying! I took your advice from the above and have reworked the whole thing. In words, this is what I'm trying to do: N=1001 Y=3 I=2 Count=2 Begin with y. divide y by i. if there is a remainder, increase y by 1. If there is no remainder, increase i until it is y1 big. If there is still no remainder, increase y by 1 and increase count by 1. Repeat the process until count is 1 less than N. I wrote some code to try to capture the above, but can't even get to the end of it without the compiler throwing up some issue. I've attached my best efforts, is what I'm doing wrong an error of logic (again) or is it a coding issue? And how do I make it right? Thanks again, I really appreciate the help!
 9 months ago

e.mccormickBest ResponseYou've already chosen the best response.1
count=count+1 and y=y+1 You can't use and that way. Put them on seperate lines, or use: count,y = count+1, y+1 If for some reason you feel you myst do this on one line. Python is different in that it allows that for assignment. most languages would not let you use that. You should also print the values in there. Doing so shows that this is an infinite loop and count never advances.
 9 months ago

nsanBest ResponseYou've already chosen the best response.0
I made the changes you suggested and realized that it's an infinite loop. I started again and tried the attached version, does this make more sense? It still isn't doing what I want it to do, but is the logic/ layout any better? What do I need to change to improve it to the point where it spits out the 1000th prime?
 9 months ago

e.mccormickBest ResponseYou've already chosen the best response.1
To make editing easier, why not do this as a .py file, and not type it all in every time? Someone linked this into to IDLE, and it is great for explaining how you can do this: https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html You just want a basic solution, right? Nothing too elegant at this point. What I think you need is multiple loops nested inside one another. The outer loop would be the collecting a count and printing the primes. Once it hits 1000 of them, stop. Use say the first 10 for testing, just because it is faster. The inner loop would be something that starts with the last prime found, advances 1, and tests if any lower number divides it.
 9 months ago

e.mccormickBest ResponseYou've already chosen the best response.1
This is the concept of thenested loop in psudocode: number=where to stop count=counter to trigger stopping candidate=first number to test test=low number to test with, 1 or 2. while count is less than number while test is less than candidate if remainder of candidate/test is 0 if candidate equals test: candidate is prime else: break out of inner loop iterate test iterate count reset test iterate candidate You may run into some odd reactions in there. If so, use a lot of prints to see where things go wrong. Also, when doing all the prints and initial testing, set number to something like 15. That will cause it to stop quickly and you can see if it found 3, 5, 7, 11, and 13 properly.
 9 months ago

e.mccormickBest ResponseYou've already chosen the best response.1
You goal is to get something like this: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 .... lots of removed numbers ... 7789 7793 7817 7823 7829 7841 7853 7867 7873 7877 7879 7883 7901 7907 7919 If your program stops before 7919, then your iterate candidate is in the wrong place.
 9 months ago
See more questions >>>
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.