Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (anonymous):

Assignment 1: Computing Prime numbers: I try to generate the first 1000 prime numbers by testing each numbers. I wrote this program but it doesn't seem to work. Can anyone check it for me?~ thanks! candidate=3 y=2 n=0 while (n<1001): while (candidate%y>0, candidate>y): y=y+1 if (y==candidate): n=n+1 print y candidate=candidate+1 print ('done')

OpenStudy (anonymous):

int x=1; int y; int main() { loop: int Prime [1000] = {x}; int n=1; signed int m=1; if (n/Prime[y]!=1) { n=n+2; x++; x++; goto loop; } else if (n/Prime[y]==1) { cout << n; n=n+2; x++; x++; goto loop; } else if (n/Prime[y]>=1) { n=n+2; x++; x++; goto loop; } else {n=n+2; goto loop; } system("PAUSE"); }

OpenStudy (anonymous):

ejeffreywang: I was unable to follow your reasoning within the block while (candidate%y>0, candidate>y):. For each number (in your case candidate) you need to loop through all the numbers from 2 to up to half of it. The moment the remainder is zero break out of the loop as the number is not prime. On the other hand if you get to the end of the loop then your number is prime. Display it and increment n. I've attached my version of the solution. If you have any questions just ask.

OpenStudy (vaboro):

Please, consider adding comments to your code. It's now easy to read.

OpenStudy (vaboro):

Any program ideally follows some logic. Thus, the process of program execution can be expressed by a flow chart. Flow charts are covered somewhere further during the course. Can you prepare a flow chart for your script? Did you try to write pseudo code for your script? Frankly speaking, I didn't understand what your program is doing...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!