anonymous
  • anonymous
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
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
e.mccormick
  • e.mccormick
x=2 i=2 while x
anonymous
  • anonymous
give your variables descriptive names - it makes it easier to read and understand for ppl who didn't write it http://dpaste.com/1256761/
anonymous
  • anonymous
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 y-1 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!
1 Attachment

Looking for something else?

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

More answers

e.mccormick
  • e.mccormick
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.
anonymous
  • anonymous
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?
1 Attachment
e.mccormick
  • e.mccormick
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.
e.mccormick
  • e.mccormick
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.
e.mccormick
  • e.mccormick
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.

Looking for something else?

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