| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Inefficient Primality Methods

Page history last edited by Bill McEachen 1 year, 4 months ago

credit any of my work

 

I will cover a few, including using Pascal's triangle (binomial), Wilson's, etc.  I won't treat methods that are specialized for specialized forms.

Near as I know all are deterministic.

This site https://brilliant.org/wiki/prime-testing/ covers:  AKS, Fermat, Miller-Rabin, Wilson's, and Trial division.

Many are included at http://unsworks.unsw.edu.au/fapi/datastream/unsworks:53023/SOURCE01?view=true  Appendix E.3  (migrated/dead ?)

 

The method via the Pascal's triangle I first saw at the paper link included in this source (PariGP):  primeByPascals.gp  MersennePrime (MPE=31) timing ~ 47ms

 

Wilson's method is shown at the great site rosettacode.org here: https://www.rosettacode.org/wiki/Primality_by_Wilson%27s_theorem  timing: MersennePrime (MPE=9689, 2917 digits) -killed after 9min

 

Miller-Rabin is covered at RosettaCode here: https://www.rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test

 

AKS is covered at RosettaCode here: https://www.rosettacode.org/wiki/AKS_test_for_primes

 

Trial division is covered at RosettaCode here: https://www.rosettacode.org/wiki/Primality_by_trial_division timing:  MersennePrime (MPE=9689, 2917 digits) ~ killed after 174min

 

Primality via Latin triangles is discussed here: https://users.monash.edu.au/~iwanless/papers/redLRdiv.pdf

Its main formula is  R(k,n) ≡  ((-1)^(k-1) * (k-1)!)^(n-1) (mod n)

In the paper, we see:

k\n 

1 

 

Near as I can tell, R(k,n) =1 for n=prime for all k (composites will see >1 and eventually 0 ad infinitum).  I tested a 6-digit n=prime(10)*prime(20)*prime(30) and k=3.  R(k,n)=176957 indicating n is composite.  For n=8191, k=9 R(k,n)=1 as expected.

Looking at OEIS A050217, using k=4 the first few terms yield 56, 875, 1013 as we would hope for such pseudoprimes.

Similarly using A188755, using k=4 the first few terms yield 1, 1, 39529384, .... So one needs to evaluate k>>4 to obtain useful results.  k=6 for the first two gave 1 &  14134019.  k=8 for  the first gave 1 as well.

The test is one-line essentially:   

 q= ( (-1)^(floor(n/2)-1) * (floor(n/2)-1)! ) ^(n-1) 

Candidate is prime if q%n==1 otherwise not

Here is a basic Pari script to check N via the Latin square mechanism: LatinTri.gp   timing  8191 ~ 3.5-sec, RAM-intensive (w/ 8Gb, O/F trying n=prime(20000) ).  Slower than Wilson's

 

Montferrier:  (for non-square odd n) all  (n+k^2)  are non-square for k=1,2,3... (n-3)/2

eg n=13 yields 14,17,22,29,38  none of which are square and so 13 is prime.

eg n=27  we see 27+9=36 which IS square so 27 is composite.

Here is a basic Pari script to check N:  Montferrier.gp   VERY slow (parallel version 10-digit killed after 7min)

Method lends itself to parallel processing (not tried).

 

Zsigmondy:  A number n is prime if, and only if, it is not expressible as ab + xy where a, b, x, y are positive integers such that a+b = x-y

eg a, b, x, y = 1,3,7,3  yields ab+xy = 63 and since 1+3=7-3,  63 is composite

 

Vecchi: A number n > 5 is prime if, and only if, a-b = n and  a|B = primepi' with gcd(a,b) = 1 where primepi'  is the product of all odd primes < sqrt(n)

 

Jolivad's Test: An odd number n is prime if, and only if, there is no odd square with a root <= (2n-9)/3 which, when increased by 8n, gives a square

Near as I understand it, let n=21, yielding checking odd squares <= 11 so (1,3,5,7,9,11).  We see 1^2+8*n=169 which IS a square, so 21 is composite.

Let n=19, we must check 1,3,5,7,9  which give 153,161,177,201,233 none of which are square, and so 19 is prime.

Here is a basic parallel processing script: Jolivad.gp  timing: 10-digit ~133 sec

 

Laurent's Test: Laurent noted that Q = 0 if n is composite, or 1 if n is prime.  Q=N/D with  N=  exp(2Pi*iGamma(n)/n) -1  D= exp(-2Pi*i/n -1  (my transcription may not be right)

 

Vantieghem's Test: n>2 prime IFF product of (2^k-1) yields integer Mod (2^n-1) , product over k=1 to (n-1).

Here is a basic Pari script (parallel version PENDING): Kilgore.gp  timing n=3571 ~ 11sec  VERY slow

 

Ladera et al (primepi only):  see the link for a double product expression that is correct but very slow: https://hal.archives-ouvertes.fr/hal-03527940/document

Here is a basic Pari script:  ladera.gp

Here is a 2nd link: https://d1wqtxts1xzle7.cloudfront.net/77207697/2106-libre.pdf?1640298118=&response-content-disposition=inline%3B+filename%3DNew_generating_and_counting_Functions_of.pdf&Expires=1645501966&Signature=TzOsunLYOULG3STFTYDCBVfZ~PnOFC3boqOB2ZO9aN~jsrfbK~RWNKQR1pmqk0iyQVSl544~BBkUWZ~LjiXB6xtWdFC6NqhczDAXDt0daH2ZC0Gl4kufyTSaDU8~Lv0U7Neni2O30an5Qo5huUWd-oKkxAE0Ru4prFFcGdmgDtLxr4RpA734mxevkS55xDuentefWGGu73XhXA-Yy-RvVXzrNEVmNGEIjQ2xuFtffheGMcbr~hg2VdJZXJA~Xy1Ciho63yOEqvl0~tovz6xlXvX2g1DpeapL8WKA7IYkG3fSAJQExw7mWhcXZAJ1qDdjzdsjOqGm4AP8xmeGkgUbQg__&Key-Pair-Id=APKAJLOHF5GGSLRBV4ZA

 

OEIS sequence  A213891appears to generate purely prime terms, its code is at its link in Pari & similar software (Maple, Mathematica).  See https://oeis.org/A213891 

 

There are other OEIS sequences that are quite prime-rich.  Some include A270834, A270951, A270997, A271116, A128288.  A308078&A309290 involve the binomial and are RAM-intensive.

 

via Odd Composites (Barker) primepi only: Using this, primepi= maxx/2 - result.  Here is a basic script in Python ( cpu-driven, RAM driven is faster but one cannot compute nearly as high): OCprimalityList2.py

( a parallel version is: PENDING ).

 

via posts by Christopher Wolfe, slow but

Applying q=exp ((4*I*Pi*(1+ k*2^k)) / (-3 + (-1)^k - 2*k)), k>2  generates the primes along with a subset of Poulet numbers (& other pseudoprimes) where real(q)>(1-epsilon)

Here is a basic Pari script ( must set precision, RAM high enough) :  wolfe_primality.gp

 

my custom scripting based on Barker's (parallel version):  binPrime_parallel.gp  timing:  MersennePrime (MPE=110503 (33265 digits) ) timing ~2.75min

 

*********

Huen Yeong Kong has a factoring method for prime generation, cited here: https://gauravtiwari.org/this-prime-generating-product-factors/

 

Here is a basic PariGP script that illustrates it:  YKHuen.txt    It generates the first 100K primes in ~ 6sec

 

Hang put out a simple primality alg Dec 2022, but it cannot compete speed-wise.  Here is the basic script:  Hangprimality.gp

 

 

 

Comments (0)

You don't have permission to comment on this page.