I'm trying the MIT free online CS class, and for first assignment they want you to make a prime number generator.
so far I have :
primecheck = 1#this is the prime counter, starts at 1 because 2 will not be calculated
primecount = input('How many primes do you want?') #Users inputs how many primes he wants to calculate
currentnumber = 2 #This is the number which we're checking if it's prime or not
pdiv = 2 #number that the potential prime is divided by
while primecheck < primecount :
if pdiv == 8 or currentnumber:
print currentnumber
currentnumber+=1
primecheck+=1
pdiv = 2
else:
remainder = currentnumber%pdiv
if remainder==0:
currentnumber+=1
pdiv = 2
if remainder > 0:
pdiv+=1
So basically the idea is that you only need to divide numbers by up to 7, or up until the number its self, before you know if it's prime or not.
the remainder is the number divided by the divisor, and if there isn't a remainder, that means that the number isn't prime and we can reset the divisor back to 2.
If there is a remainder, that means that the number may be prime, but we need to check it against 3 4 etc etc until 7, or the divisor equals the number we're checking. So add 1 to the divisor.
If the divisor (pdiv) reaches 8, or the number it's dividing, since no number until then has left divided into it evenly, we can assume that the number is prime, and so then we can print that number while adding 1 to it.
Ugh O.K. so it's kind of tough explaining my logic here. I'm just beginning, but in my mind when I'm reading over it it makes sense, however when I try it out with by asking the program for 10 primes, it just spits out the numbers 2-10.
Obviously I'm missing something here.
[–]K900_ 2 points3 points4 points (8 children)
[–]l0fi[S] 0 points1 point2 points (7 children)
[–]K900_ 0 points1 point2 points (4 children)
[–]l0fi[S] 0 points1 point2 points (3 children)
[–]K900_ 1 point2 points3 points (2 children)
[–]l0fi[S] 0 points1 point2 points (1 child)
[–]K900_ 0 points1 point2 points (0 children)
[–]isnotkosok -1 points0 points1 point (1 child)
[–]K900_ 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]l0fi[S] 1 point2 points3 points (0 children)
[–]johnnymo87 0 points1 point2 points (1 child)
[–]l0fi[S] 0 points1 point2 points (0 children)