0

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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 2
    It sounds as though what you want is a way to tell whether an integer is exactly divisible by another integer. The safe way to do that is to use the modulo operator `%`. For example, `final` is divisible by `i` if and only if `final % i == 0`. That way you avoid the use of floating-point arithmetic entirely. – Mark Dickinson Mar 12 '22 at 12:35
  • 3
    Does this answer your question? [How do you check whether a number is divisible by another number?](https://stackoverflow.com/questions/8002217/how-do-you-check-whether-a-number-is-divisible-by-another-number) – Mark Dickinson Mar 12 '22 at 12:37

0 Answers0