What am i doing wrong? First assignment, first problem- To calculate the 1000th prime number, Can you please explain it to me, prime=3 check=2 count=1 while count<1000: if prime%check != 0: check=check+1 if prime==check: count=count+1 else: prime=prime+1 print count
pls use a code pasting site and select Python syntax highlighting. http:\\dpaste.com http:\\pastebin.com http:\\codepad.org it helps to write down in words the steps you need to take to solve the problem then turn that into code. when you test a number (n) for primeness, you want to see if it is evenly divisible by all numbers between 2 and n-1. to test 5 you need to divide by 2, 3, and 4. to test seven you need to divide by 2, 3, 4, 5, and 6. your code doesn't do that. print statements help a lot when trying to debug - i added one to yours - it should help you see what is going on - it will print out a lot of stuff on the screen, you can use ctrl-c to stop it and examine the results. http://dpaste.com/793098/ have you read the Tutorial in the Python documentations? A for loop might help.
Thankyou for your help! Now i see what i was doing wrong. I did listen to the lecture before trying to solve the problem. But i am totally new to programming and i did not understand much about loops from the video lectures. Maybe i will refer some books for understanding the loops better and then try the next problems. I was thinking of taking a programming class at school next year, so wanted to learn a bit about it. thanx again :)
That is really helpful!
that is in the reading list for both the 2008 and 2011 course
Join our real-time social learning platform and learn together with your friends!