I've been trying to make a python script that tells if a number is a prime number or not. I've tried by my own way, but I ran into a big problem.
When I'm trying to divide a big number it gives me a error OverflowError: integer division result too large for a float
. I found that if I use //
method at the divide it stops giving me that error, but I can't find a way to find if the number is prime.
This is the first script I made, that gave the OverflowError: integer division result too large for a float
:
product = 1
list = []
i = 13
for num in range(i):
list.append(num)
list = [x for x in list if x != 0]
for x in list:
product *= x
final = product + 1
final2 = final/i
if float.is_integer(final2) == True:
print("Prime")
else:
print("Not prime")
As you can see, I used to divide the final
by i
. If the number was a prime one, it will return float.is_integer
. But if the i
variable was a big number, it will give the error.
Then I used the //
method but I have no idea how to check if the the number is prime.
Here is the second scrpt, it's the same but replacin the /
for a //
:
list = []
i = 17
for num in range(i):
list.append(num)
list = [x for x in list if x != 0]
for x in list:
product *= x
final = product + 1
final2 = final//i
if final2%1 == 0: #Here I have no idea of how to check if is a prime
print("Prime")
else:
print("Not prime")
I know this is not the best way to check if a number is prime.