Stuck with problem set 1. I cannot figure out what is wrong with my code and I have tried completely rewriting it at least 5 times. here is my current version which by my (obviously flawed) logic, should work: http://www.pastebucket.com/2882
To check the logic, we need to understand the goal of the program. Could you share with us the question?
my goal for running this particular program would be to have it print the first prime number then the second.... all the way until the 10th. I made it 10th even though in the problem its the 1000th because i was trying to make something that would work then worry about the details.
So what seems to be not working?
the program prints the next prime number after "candidate" the number of times that "itersleft" is set to..
You have a little problem with flow control. The extents of the while are not clear to me, because I don't know the language, and there are no delimiters for how far the while's encompass. Can you tell me, according to you, how the program knows how far the while's include?
I don't see the end of the else's either. Does it cover one statement or something else?
i think I understand the problem better based on that^ but with regards to your question I think I had such bad style that I cant even tell
You need to know before you can start coding. In Java or C++, we write while(condition){ ... multiple statements } so it is clear. If the braces {} are missing, the while covers only one statement.
See if you can find that from your notes or documentations.
I'm pretty sure the while covers everything indented under the while in python
You're right, Python uses indents, but indents with the same number of spaces.
I have the impression that you just print too many times. If you print outside of the second while, you'd be better off.
alright, thanks. I'll try it out
You also need to increment the candidate and reset divisor to 2 after printing a prime.
By the way, my bad. Your print statement is indented correctly.
lol just realized that
also it works now and I think I love you
#nohomo
unless your a girl in which case HAVE MY BABIES
haha! Great job. Just a few comments: 1. candidate starts at 4, you'd be missing 2 and 3. 2. "itemsleft" would be more appropriately named itemsDone. 3. since you have to go up to 10000 primes, efficiency counts. while divisor<candidate/2+1 would be more efficient if you write while divisor<= sqrt(candidate) If this is ok with your teacher. 4. Also, you are testing divisors which are even, applies only to 2. You don't need to test 4,6, 8... You should do well.
correction: ... while divisor<sqrt(candidate+1)
took me a second to understand that... that is much more efficient, thanks. also for ruling out even numbers coul I use an if statement like "if candidate%2==0:"?
python doesn't seem to understand sqrt...
i see, i would have to import math first, but importing hasn't been covered ye in the course so I'll just use (candidate+1)**0.5
with regards to the even odd thing, i just decided to start with three and add 2 to candidate at every iteration. then I get to use neat numbers like 999 anyway XD
Yep, sounds good for the even odd thing. Sometime people use a variable for incrementing, initially set to one, then to two after the first increment. If you do the square-root, make sure you do it only once for each candidate, otherwise you lose the advantage. Keep up the good work and good luck!
In fact, if your candidate starts at 3, you can increment your candidates by 2, and you will never even have to test for even candidate, nor the divisor 2.
Maybe I'm missing something and this is a very clever algorithm, but how are you testing for the prime numbers you've already established? Wouldn't it be more efficient to save the computed numbers in a tuple or list and add to it?
Yes, that would be a desirable thing to do. However, I do not know if you have learned how to use arrays or not. Basically, you store all primes found (starting from 2) in an array, and instead of testing with the next odd number as a divisor, you would test with the next prime. That would be much more efficient, if you go up to the tens of thousands of primes.
Would an array have any advantage over using a list, or is this just a question of syntax?
List is probably the array equivalent in Python. So yes, anything that stores integers would do the job.
Merci! :)
I think you may need to put parentheses around where you're printing candidate. All your other code seems to be correct.
Je t'en prie!
Join our real-time social learning platform and learn together with your friends!