Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created February 16, 2026 14:13
Show Gist options
  • Select an option

  • Save Hermann-SW/b4d422842b22da4d5f2838a211f38e02 to your computer and use it in GitHub Desktop.

Select an option

Save Hermann-SW/b4d422842b22da4d5f2838a211f38e02 to your computer and use it in GitHub Desktop.
Sequential determination of primitive root of n-th Euclid prime
euclid_prime_find_root(n) = {
my(En = vecprod(primes(n)) + 1, phi = En - 1);
for(g=2, En-1,
if(lift(kronecker(g, En))==-1,
my(is_root = 1);
for(i=1, n,
if(lift(Mod(g, En)^(phi / prime(i))) == 1,
is_root = 0;
break;
);
);
if(is_root, return(g));
);
);
}
@Hermann-SW
Copy link
Author

Hermann-SW commented Feb 16, 2026

Demonstration on 457th Euclid prime,
and demonstration of huge speedup over GP znprimroot() (which has to factor N-1 first):

hermann@7950x:~$ gp -q seq.gp 
? #
   timer = 1 (on)
? euclid_prime_find_root(457)
cpu time = 8,368 ms, real time = 8,370 ms.
31
? lift(znprimroot(prime(457)#+1))
cpu time = 1min, 34,734 ms, real time = 1min, 34,744 ms.
31
? #digits(prime(457)#+1)
1368
? 

Parallelized in par2.gp gist:

hermann@7950x:~$ gp -q par2.gp 
? #
   timer = 1 (on)
? euclid_prime_find_root(457)
cpu time = 14,400 ms, real time = 1,216 ms.
31
?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment