#!/usr/bin/env python # coding: utf-8 # # The largest prime (so far) # # A new record for the largest prime has been found lately. Let explore this number and know more about it and how to deal with it using some Python, Numpy and finally using we will take a loog at Cython and GMP. # In[1]: import numpy as np import math from datetime import datetime get_ipython().run_line_magic('load_ext', 'Cython') # This number of was found in 2016 using the Great Internet Mersenne Prime Search (GIMPS). This prime is one of the Mersenne Prime whice are of the defined by the following formula: # # $$2^p-1$$ # # Where $p$ is a prime number. This is called a psudo-prime meaning not all Mersenne Prime are primes. # # $2^2-1=3$ # # $2^3-1=7$ # # $2^5-1=31$ # # $2^7-1=127$ ... # # In[2]: 2**2-1 # In[3]: 2**3-1 # In[4]: 2**5-1 # In[5]: 2**7-1 # The smallest none-prime (composite) number is $2^{11}-1=2047$ which is a composite number. # In[7]: 2**11-1 # The largest prime number is that was found lately is: # $$2^{74,207,281}-1$$ # # The reason why I didn't put the actual value is that the number if very large (over 22 million digits). # In[9]: p = 74207281 the_number = (2 ** p) - 1 # # Lucas–Lehmer test # # A very easy way to test a Mersenne Prime is the Lucas-Lehmer test which uses the Lucas series. # # $4, 14, 37634 ... $ # # The series starts with 4 then uses the last value in this formula: # # $S_i=S_{i-1}^2-2$ # In[10]: S = 4 print(S) print(len(str(S))) # In[11]: S = S ** 2 - 2 print(S) print(len(str(S)), "digits") # In[12]: S = S ** 2 - 2 print(S) print(len(str(S)), "digits") # In[13]: S = S ** 2 - 2 print(S) print(len(str(S)), "digits") # In[14]: S = S ** 2 - 2 print(S) print(len(str(S)), "digits") # In[15]: S = S ** 2 - 2 print(S) print(len(str(S)), "digits") # In[16]: # The largest prime (so far) the_number = 2 ** 74207281 - 1 print(int(math.log10(the_number))+1, "digits") # The test is if the Mersenne Prime number of $n^2-1$ is a divided by $S_{n-2}$ you should get a remaining of 0. The problem is this series goes very large very fast. So there is another way to do this test. # In[20]: p = 74207281 the_number = (2 ** p) - 1 S = 4 time_stamp = datetime.now() for i in range(p-2): S = (S ** 2 - 2) % the_number if i % 1 == 0: print(i, datetime.now() - time_stamp,"") time_stamp = datetime.now() if S == 0: print("PRIME") # In[21]: get_ipython().run_cell_magic('cython', '', 'cdef unsigned long p = 61\ncdef unsigned long P = (2 ** p) - 1\n\nS = 4\nfor i in range(p-2):\n S = S ** 2 - 2\n S = S % P\n if i % 10 == 0:\n print(i)\nif S == 0:\n print("PRIME")\n') # In[ ]: get_ipython().run_cell_magic('cython', '--link-args=-lgmp', '\ncdef extern from "gmp.h":\n ctypedef struct mpz_t:\n pass\n \n ctypedef unsigned long mp_bitcnt_t\n \n cdef void mpz_init(mpz_t)\n cdef void mpz_init_set_ui(mpz_t, unsigned int)\n \n cdef void mpz_add(mpz_t, mpz_t, mpz_t)\n cdef void mpz_add_ui(mpz_t, const mpz_t, unsigned long int)\n \n cdef void mpz_sub (mpz_t, const mpz_t, const mpz_t)\n cdef void mpz_sub_ui (mpz_t, const mpz_t, unsigned long int)\n cdef void mpz_ui_sub (mpz_t, unsigned long int, const mpz_t)\n \n cdef void mpz_mul (mpz_t, const mpz_t, const mpz_t)\n cdef void mpz_mul_si (mpz_t, const mpz_t, long int)\n cdef void mpz_mul_ui (mpz_t, const mpz_t, unsigned long int)\n \n cdef void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t)\n \n cdef void mpz_mod (mpz_t, const mpz_t, const mpz_t)\n \n cdef unsigned long int mpz_get_ui(const mpz_t)\n\n#cdef unsigned long p = 61\ncdef mp_bitcnt_t p = 74207281\ncdef mpz_t t # = 1\ncdef mpz_t a # = 1\ncdef mpz_t P # = (2 ** p) - 1\ncdef mpz_t S # = 4\n\nmpz_init(t)\nmpz_init_set_ui(t, 1)\n\nmpz_init(a)\nmpz_init_set_ui(a, 2)\n\nmpz_init(P)\nmpz_mul_2exp(P,t,p)\nmpz_sub_ui(P,P,1)\n\nmpz_init(S)\nmpz_init_set_ui(S, 4)\n\nfor i in range(p-2):\n #S = S ** 2 - 2\n mpz_mul(S,S,S)\n mpz_sub_ui(S,S,2)\n \n #S = S % P\n mpz_mod(S,S,P)\n \n if i % 1000 == 0:\n print(i)\nif mpz_get_ui(S) == 0:\n print("PRIME")\nelse:\n print("COMPOSITE")\n\n#print(mpz_get_ui(P))\n') # In[ ]: