I'm a little stuck on ps1a as well... i am sure there is a problem in my flow control. If i print the list of primes generated I get wrong #s like 95,121,209, etc... My code is here: http://codepad.org/mAnfenzT
You code is honestly kind of confusing. It has a lot of variables that really don't make it clear what their purpose is. This is the reason good, clear, commenting is valuable. I see that you did at least comment about two of them, but it would be very helpful, at least to me, if you explained how the rest of your code is supposed to work, either in additional comments, or in here.
kgiax-this is the new code: http://codepad.org/qvOzbiBC I took out the list (which was not being used) and put in some more comments. Thank you very much for even looking...
ARG! I typed out a long reply but this stupid site refreshed on me for no reason. Whatever, I'll give you the short version: http://codepad.org/wctnu092 I added two types of print statements to your code: one tells you which conditional code blocks get reached, the other prints the values of the variables. These two things will allow you to easily debug your code. I also lowered index to 10: start small, get 10 to work, and then increase it a little bit at a time. Anyway, I believe your problem lies in line 21: this line runs every single time the end of this loop is reached, no matter what. Your answer to this seems to be lines 9 and 14. However, when candid is increased by 2, the line 6 while loop doesn't restart, it keeps going! That means it will reach Line 21 every time, no matter what. Basically, you haven't implemented your conditionals and loops properly. Use the debugging statements I added and follow each value manually in your head: start with 3 as candid, and mentally follow the code, and watch where it goes wrong. Once you get whats going on, you can rewrite your conditionals to properly send the code in the right places.
kgiax pointed out that your variable names didn't give enough of a hint as to how they were being used. So you added some comments that cleared that up. That was good. Even better would have been to rename the variables based on the comments. Then you wouldn't even need comments--the variables themselves would say what they were. index=1000 #for gettting the nth prime Call it numPrimesToFind. candid=3 #this is the candidate being evaluated Call it candidate or candidateToTest or candidateToEvaluate. prime=1 #prime number counter Call it primeNumberCounter or numPrimesFound. testno=2 #this is the test divisor Call it testDivisor or divisor. Clear comments are good. Clear code is better.
Thanks for the help and for the tips, guys. Managed to get myself pretty confused there...
When you get confused, back up. Solve an easier task. Strip away all the confusing stuff until you get to a point where you KNOW something works. And the way you know it works is that you write a test and run it and the test says "yes this worked" or "no this didn't work." Also, make sure the inner loop works solidly before attempting to put the outer loop around it.
Join our real-time social learning platform and learn together with your friends!