Help with ps1! I have a problem with my code... its successfully finding odd numbers, but once I have it try to find primes it gives me 5, 5, 7, 7, 7, 7, 9. It won't go past 9, which isn't even prime. If someone could point me in the right direction with how to fix this, I'd really appreciate it! candidate=3 count=2 count_limit= int(input("What prime would you like to calculate?")) while count<= count_limit: candidate += 2 for factor in range(2,candidate-1): if candidate%factor ==0: notprime=candidate else: prime=candidate
First id say is that you want to ask yourself what exactly do you want this script to do. I am not that experienced, do you want this to generate prime numbers or tell the user if the input they typed is a prime number? From my view it appears you are trying to do both.
Well, I'm trying to do problem set 1, problem 1, where you are supposed to compute and print the 1000th prime. So, no, I am not trying to tell if a number is prime or not. Just generate them up to a certain point
As it is, your code doesn't print anything because it loops infinitely : count is never incremented in the while loop. Another comment: please post you code in a pastebin (codepad.org for example) and not here because it's easier to read and to to copy/paste. I attach to my post a diagram I made to explain the logic of my solution.
is that all of your code? pls use a code pasting site http://dpaste.com/686577/
What are you doing with the prime and notprime variables?
ok, I edited again, now its only giving me primes, but its giving me lots of duplicates and I don't know how to get rid of that. Also, it won't do the final print instructions when I reach the count limit. Any suggestions? http://dpaste.com/hold/686699/
It's doing duplicates because you're calling print inside that 'else' case, which is called several times for every prime as you check each of its possible factors. You could solve this by using the if-else case to set a boolean that tells you if a candidate is prime or not. Also, since that 'else' case gets called multiple times for each prime, you are incrementing 'count' multiple times for each prime, and I don't think you want to do that. The last print statement doesn't get called because 'count' skips over 'count_limit' during the last iteration of the while loop.
Yeah, I figured that was why it was doing it, I'm just not sure how to set up a boolean to fix it. Thanks for your help :)
If you use a boolean in the for loop, you can check what it is after the for loop but inside the while loop, then do the stuff for primes there. You just have to make sure you reset the boolean at the beginning of each iteration of the while loop.
Alright, I think I figured it out (at least, it correctly spits out the 1000th prime number). In case anyone is interested, here is my code: http://dpaste.com/687021/
i'm not sure why you needed lines 11 and 12. here is a variation of your solution: http://dpaste.com/687207/
I had lines 11 and 12 in there because otherwise when it tested non-factors of non-prime numbers, and the remainder was not zero, it labelled the number as a prime (e.g. 14/5 has a remainder of 4, so it thought it was prime for that factor)
did you see how i made it work without that?
It took me a little while to figure it out... but yeah, I think I do now
cool - i left your code in there commented out - sometimes it helps to remove all those commented lines to be able to see it better
Alright, so I tried to convert my code for problem 2 in the same set, and it seems to be working, I'm just not sure how to tell that the numbers it is giving me are actually correct. Also, I'm not sure if I did the ratio right. http://dpaste.com/687368/
take a better look at lines 15 and 16 - math.log is a function, it 'returns' the log of a number. http://pastebin.com/52DfqBmu http://docs.python.org/library/math.html#math.log
Join our real-time social learning platform and learn together with your friends!