Use ==
to check equality of numbers:
for numberToCheck in range(2,10000):
divider = 2
while numberToCheck > divider:
if numberToCheck % divider is 0:
break
else:
divider += 1
if numberToCheck == divider:
print(numberToCheck, "is a prime number.")
Is
operator is used to check the id
of two objects while ==
operator check their values.
Python implements an Array of integers for values between -5
to 256
, and when you create an int object in this range, you get a reference to the existing array implementation. That's why id
of all integers objects in this range is same but it is different for integers objects outside this range, as seen below:
>>> a = -6
>>> b = -6
>>> a is b # a and b has different id
False
>>> a = -5
>>> b = -5
>>> a is b # a and b has same id
True
>>> a = 256
>>> b = 256
>>> a is b # a and b has same id
True
>>> a = 257
>>> b = 257
>>> a is b # a and b has different id
False
And this is the reason, your program prints primes till 251
but not the next prime 257
and afterwards, however your program does run till numberToCheck
reaches 9999
.
Also, you might consider a faster algorithm to generate primes, such as Sieve of Eratosthenes.
Basically, you should check divisibility of numberToCheck
with all primes between 2
and ( previously found prime or square root of numberToCheck
, whichever is less ).