A slight problem with PS1:
(Sorry if this is a duplicate, it didn't pop up when I asked it before, but:)
I'm having a bit of trouble with the first piece of code in PS1. The current piece of code I have yields me a list of all odd numbers.. which, I guess, is a first step, but doesn't really help me since I'm not precisely sure why.
for y in xrange(2, x-1):
if x%y==0: x=1+x; break
else: primelist.append(x); x=x+1; count=count+1; break
print primelist; print x; print count
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
This might be a bit strange looking, I don't know, but here's my intention on this (that's obviously going awry):
Start with x, a prime. (Yes, I know 5 isn't the first prime, it's 2 and 3, but I'll get to that in a second.) Primelist is where I want to store prime numbers, and count counts the sequence of primes (5 being the third prime number). So I want to create the range from 2 to x-1, so I can test all numbers besides itself and one. The plan from there is to test with the modulo operation if any number in that range divides evenly into the prime candidate (x). If one does, try the next number, because x isn't prime. If it doesn't, append that number to primelist, increase x by one (try the next number), and increase the count sequence by one since we've found another prime.
The problem, as I said, is that primelist is filled with odd numbers when they're obviously not prime! (e.g., 9, 15, 21, etc.) There's obviously some sort of error here I'm not catching.
Hello maxwell. I see that you are using arrays and just want to remind you that consider the problem set and compare it to lectures. Since by this problem set arrays havent been taught I assumes students are not expected to resolve this problem set by using arrays. There should be an easier way without using complex functions. I would suggest to try it one more time yourself just figuring out the algorithm. I dont think you need anything more than if, else if and while and some variables. ...If you still have a problem you can consider the following code
PrimeCounter = 2
odd = 3
interval = 1000
if interval > 0:
while PrimeCounter < (interval + 1):
n = odd - 1
PrimeCounter = PrimeCounter + 1
odd = odd + 2
print (str(interval) +'th prime number is ' + str(odd-2))
elif interval == 0:
print "You entered 0"
print "You entered negative number"
the else is really a for else construct
so align the else with for and take out the break at the end.
Not the answer you are looking for? Search for more explanations.
castlepeak: wow that worked, thanks a lot! just goes to show you, kids: mind your indents. i was worried i had the code fundamentally wrong, but that's a weight off my shoulders!
Balash: thanks for the tip, the final code i ended up using to finish up ps1 had bits from your example sort of melded into mine (mainly because i thought it was quicker to spit out the list all at once rather than cycle through 1000 "The xth prime number is y." I know we hadn't been taught arrays/lists yet, but i just happened across them in the reading and figured they'd be useful, so i just went for it.
Sure no problem maxwell. I hope it helped. Hey you guys, could you do me a small favour? Can you help me promote my fundraising? its for my college... I really need it to get my studies going. Hope its not too much to ask.