could someone please help me with my program for ps1a?
this is my code so far...I dont get error messages but I also get no output.
for evennumbers in range(2, 1000):
if evennumbers%2==0:
evennumbers=evennumbers
for oddnumbers in range(3, 1000):
if oddnumbers%2!=0:
oddnumbers=oddnumbers
for prime_candidate in range(3, 1000):
if prime_candidate==oddnumbers:
while oddnumbers
For me, the easiest way of generating odd-numbered candidates was to start at a low odd number (maybe 5; 2 and 3 being explicitly defined as prime) and increment with 2: candidate = 5 loopThis: [test candidate] candidate = candidate + 2 P.S. It looks like some of your code is chopped off. You can share code samples with dpaste ( http://dpaste.com/).
First of all, running that just ends up assigning "evennumbers" to the value 999. Obviously it's more efficient to only test odd candidates above 2, but it's not necessary if you're having trouble. I can't really help with the rest, but here's a link to my (working) ps1a: http://dpaste.com/601203/
Meant to say "running that first block" **
what does the += operator do. also, the semantics of this line don't make sense to me print "%d is the 1000th prime number" % (number-1) I would guess that %d means the number that is generated once the while block has been completed(though why is it stored in variable "d"?). I am having particular difficulty with the end of the line "% (number-1)" you are asking the computer to use the previous string and an int(with the int variable being 1 less than the prime that was established by the previous while block?) and then find the remainder? I see that the program works, I just don't understand why
@vosDd n += 1 is the same as n = n+1 n -= 1 is the same as n = n-1 n *= 1 is the same as n = n*1 etc. It's just a way to save time for a common operation. I hadn't encountered the %d syntax. This page has an explanation: http://homepage.mac.com/andykopra/pdm/tutorials/python_strings.html The letter after the % has to be d. It's not a variable. It's just another shortcut (so the print statement doesn't get too messy).
Join our real-time social learning platform and learn together with your friends!