0

I'm creating a program which checks if the number numb is prime and if it's not then it factorizes the number into prime numbers and prints out the factors but the problem fails with big numbers.

for n in primes:
    if numb % n == 0:
        a = 0
        while numb % n == 0:
            a += 1
            factors[n] = a
            prime = False
            numb = numb / n

When numb is big (18 digits long) the program fails at the numb = numb / n and it doesn't divide correctly (for example 231351651321912318 / 2 = 1.1567582566095616e+17)

Numbers always should divide without any decimal places so can I make that the result is more accurate?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

The operator / calls for floating-point division and that causes your operands to be divided as floating-point numbers. 18 digits is at the limit of floating-point precision. For integer division use // instead.

BoarGules
  • 16,440
  • 2
  • 27
  • 44