1

I have been using this code to check if a number is prime or not:

def pcheck(number):
    if number < 2:
        k = "NO"
    # theres no prime number less than 2
    else:
        i = 2
        factors = []
        while i * i <= number:
            if number % i:
                i += 1
            else:
                number //= i
                factors.append(i)
        if number > 1:
            factors.append(n)
        # get the factors of the number, a prime number will only have 1 factor, not including 1
        if len(factors) == 1:
            k = "YES"
        else:
            k = "NO"
    return k


n = int(input())
print(pcheck(n))

But even with modifcations it would still struggle with number that have 18 digit, this is rather troublesome for me as i cant use it in this case. I have heard but rabin miller test but have never been able to implement it in to my code. Is there a more efficient way to check prime with large numbers but still is as fast as possible?

  • Pollard Rho algorithm? – Abhinav Mathur Oct 14 '20 at 14:34
  • 1
    I am sure there are already topics about calculating whether a number is prime on this site. https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python – unlut Oct 14 '20 at 14:35
  • 5
    You don't need to find ALL the factors in the case of a non-prime - just break the loop at the first factor found. – Mario Camilleri Oct 14 '20 at 14:36
  • 1
    "in python" – no. – MisterMiyagi Oct 14 '20 at 14:41
  • 2
    Also, you can speed up the loop by 50% by eliminating all even numbers immediately, and then starting the loop with i = 3 and going up in twos (i += 2). – Mario Camilleri Oct 14 '20 at 14:49
  • 2
    There is no need to append each and every factor. It will tremendously increase time and space consumed by your program. Just loop through it till `i²<=n` . If any factor is found before i² gets closer to n , then break the loop and print `NO` – Jdeep Oct 14 '20 at 14:55
  • There is a Python implementation of Pollard's Rho algorithm on Wikipedia - adapt that. On my computer it factored 115792089237316195423570985008687907853269984665640564039457584007913129639937 in under a minute. – Mario Camilleri Oct 14 '20 at 15:05
  • Why can't you implement miller-rabin? You can turn miller-rabin into a deterministic test for numbers of 18 digits or less by using the following bases: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, and 41. See [the wikipedia article](https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Testing_against_small_sets_of_bases) for more information. – President James K. Polk Oct 14 '20 at 21:37
  • If you're interested, I have a Miller Rabin Implementation here that uses parallel processes for each miller rabin test and is very fast, faster than sympy isprime on large numbers: https://repl.it/@oppressionslyr/MillerRabinIsPrime#main.py – oppressionslayer Jan 08 '21 at 06:13

1 Answers1

1

There are a lot of ways that you can improve this function. First of all, you only have to check up until the square root of number to determine if you will find any meaningful factors. Also, once you find a factor, you can terminate the function right away (you don't have to keep searching).

In general, though, there is no fast way to do this. It is kind of the beauty of prime numbers. It takes a lot of compute time to know if a large number is prime. It also takes a lot of compute time to find the prime factors of a large number.

Sam Creamer
  • 5,187
  • 13
  • 34
  • 49
  • 2
    He already is only checking up to the square root of the number. It looks at little funny because he checking that i\*i is <= the number which is equivalent. It's a little wasteful because that i\*i multiply is computed every time whereas the square root could be computed ahead of time. – President James K. Polk Oct 14 '20 at 21:38
  • Yep, sorry, I overlooked that. – Sam Creamer Oct 15 '20 at 13:02