0

I'm currently trying to make a simple primality test using python to test prime number.

Here is the code I've made for the test:

def is_prime(n):      
    if np.mod(n, 2) == 0:          
        return False

    f = 3

    while np.square(f) <= n:       
        if np.mod(n, f) == 0:
            return False
        f += 2

    return True             

I run the code, and it works fine for small numbers. Then I enter a huge number (for convenience, I chose 2^128), it returns True. But when I enter 2^129, 2^130, 2^131, 2^132 (and so on), it always returns True. I believe my loop has been skipped. To verify this, I modified the code like below:

while np.square(f) <= n:       
    if np.mod(n, f) == 0:
        print(f)    #Added line
        return False
    f += 2

print(f)     #Added line

I test this again for some small numbers, and it'll print the correct value of f. But for arbitrarily large numbers (higher than '2^50', maybe), it'll only print 3 (the initial value of f). So I am sure that this while loop has been skipped.

Is there a way to fix this?

P/S:

  • I've also made other primality test. But I want to use this test as a verification for the result of those tests (though it'll take longer)

  • I also using Numba to speed up my code (@jit(nopython=True)). This is why I use a lot of np function, as Numba work efficiency with NumPy

Edit 1:

  • I have re-test the code without, and it works perfectly fine (even with massive numbers) but it much slower. So I guess there is a problem with Numba when handling a large number
The 2nd
  • 113
  • 6
  • `2^129` mean `1 << 129`? – Boseong Choi Mar 20 '20 at 03:41
  • NumPy doesn't handle huge numbers. NumPy is built around machine-native data types, not arbitrary-precision integers. – user2357112 Mar 20 '20 at 03:44
  • Numba cannot accelerate arbitrary-precision arithmetic. – user2357112 Mar 20 '20 at 03:52
  • 2
    (Also, calling `np.square` or `np.mod` instead of `**2` or `%` isn't necessary to use NumPy, and won't provide any benefit. The benefits of NumPy come from operating on NumPy arrays. If `a` and `b` are NumPy arrays, `a**2` and `a%b` will automatically perform operations across the whole arrays in efficient C-level loops with minimal per-element overhead. On the other hand, if `a` and `b` are scalars, calling `np.square` or `np.mod` won't provide any efficiency benefit.) – user2357112 Mar 20 '20 at 03:56
  • That's not a NumPy array, and even if it was, you wouldn't get the benefits of NumPy by operating on individual elements like that. – user2357112 Mar 20 '20 at 04:05
  • Numba can deal with individual elements more efficiently, but it still needs to work with native data types to have any benefit. – user2357112 Mar 20 '20 at 04:05
  • Thanks for your answer! By the way, do you have any suggestions to make my code work well with Numba? I'd appreciate for that:D – The 2nd Mar 20 '20 at 04:08
  • @user2357112supportsMonica I noticed that without Numba, NumPy works fine. Even with numbers like `2**86243`, NumPy can handle it and calculated quickly. The problem only occurs, if I use Numba. – The 2nd Mar 20 '20 at 06:04
  • 1
    NumPy is falling back on Python object operations there. It's not actually accelerating anything. – user2357112 Mar 20 '20 at 06:16
  • Yes, I agree with that, so I guess the problem is that Numba can't handle huge numbers. – The 2nd Mar 20 '20 at 06:25

0 Answers0