shh.util
Class NumThy

Object
  extended byNumThy

public class NumThy
extends Object

This class provides static methods from elementary number theory, strictly limited to the int data type. The engine for prime factorization is trial division.

Throughout this page of documentation, "Cohen" means Henri Cohen, A Course in Computational Number Theory, first edition, Graduate Texts in Mathematics 138, Springer-Verlag, Berlin, 1993.

Author:
Mark McConnell

Field Summary
static double ARTIN_CONSTANT
          Artin's constant: the product over all primes p of 1 - 1/(p(p-1)).
 
Constructor Summary
NumThy()
           
 
Method Summary
static int[] extGcd(int a, int b)
          Given non-negative integers a and b, returns [u,v,d] such that the gcd of a and b is d and au + bv = d.
static int[][] factor(int n)
          Returns the prime factorization of n as an array of two-element int arrays.
static int gcd(int a, int b)
          Returns the greatest common divisor as a non-negative int.
static int getPrime(int n)
          Returns the nth prime, for 0 ≤ n < 4792.
static int invModN(int a, int n)
          Given a ≥ 0 and n ≥ 1, returns the multiplicative inverse of a modulo n.
static boolean isPrime(int n)
          Whether n is prime.
static int lcm(int a, int b)
          Returns the least common multiple as a non-negative int.
static void main(String[] args)
          Tests the order of GL(n,q) and friends.
static int nextPrime(int n)
          Returns the smallest positive prime > n.
static BigInteger orderGL(int n, int q)
          Returns the order of GL(n) over the finite field of q elements.
static BigInteger orderPGL(int n, int q)
          Returns the order of PGL(n) over the finite field of q elements.
static BigInteger orderPSL(int n, int q)
          Returns the order of PSL(n) over the finite field of q elements.
static BigInteger orderSL(int n, int q)
          Returns the order of SL(n) over the finite field of q elements.
static String pairsToString(int[][] x)
          E.g., converts [[2,3],[3,1]] to
2^3 * 3.
static int power(int a, int i, int n)
          Finds ai modulo n using a binary powering algorithm (O(log i)).
static int primitiveRoot(int p)
          Returns the smallest positive primitive root modulo the prime p.
static int quadRes(int a, int b)
          Returns the value of the quadratic residue symbol (a|b).
static int totient(int n)
          Returns the Euler φ-function, the totient, of n.
static int totient(int[][] fac)
          Returns the Euler φ-function, the totient, of the positive integer whose factorization, as produced by factor(int), is fac.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ARTIN_CONSTANT

public static final double ARTIN_CONSTANT
Artin's constant: the product over all primes p of
1 - 1/(p(p-1)).
Assuming the extended Riemann hypothesis, Hooley proved this is the density of p such that 2 is a primitive root mod p.

To get the density of p such that a given a > 1 is a primitive root, we adjust this value by a finite number of factors depending on a.

References:

See Also:
Constant Field Values
Constructor Detail

NumThy

public NumThy()
Method Detail

getPrime

public static int getPrime(int n)
Returns the nth prime, for 0 ≤ n < 4792. The 0th prime is 2. This provides all the primes < the square root of Integer.MAX_VALUE. Use nextPrime(int) for primes up to Integer.MAX_VALUE.

Throws:
ArrayIndexOutOfBoundsException - If n is out of bounds.

isPrime

public static boolean isPrime(int n)
Whether n is prime. For negative n, we take the absolute value.


factor

public static int[][] factor(int n)
Returns the prime factorization of n as an array of two-element int arrays. Example: [[2,3], [3,2], [11,1]] is the factorization of 792. The primes will occur in increasing order. If n is negative, it is replaced with its absolute value. The factorization of 1 is an array of length 0.

For pretty-printing, see pairsToString(int[][]).

Throws:
IllegalArgumentException - If n = 0.

nextPrime

public static int nextPrime(int n)
Returns the smallest positive prime > n.

Throws:
IllegalArgumentException - If the return value would be negative in the int datatype.

totient

public static int totient(int n)
Returns the Euler φ-function, the totient, of n.

Throws:
IllegalArgumentException - If n < 1.

totient

public static int totient(int[][] fac)
Returns the Euler φ-function, the totient, of the positive integer whose factorization, as produced by factor(int), is fac.


extGcd

public static int[] extGcd(int a,
                           int b)
Given non-negative integers a and b, returns [u,v,d] such that the gcd of a and b is d and au + bv = d. Cohen, Algorithm 1.3.6.


gcd

public static int gcd(int a,
                      int b)
Returns the greatest common divisor as a non-negative int.


lcm

public static int lcm(int a,
                      int b)
Returns the least common multiple as a non-negative int.


invModN

public static int invModN(int a,
                          int n)
Given a ≥ 0 and n ≥ 1, returns the multiplicative inverse of a modulo n. The return value is between 1 and n-1. Adapted from Cohen, Algorithm 1.3.6. Works entirely within the int datatype.

Throws:
ArithmeticException - If a is not invertible modulo n.

power

public static int power(int a,
                        int i,
                        int n)
Finds ai modulo n using a binary powering algorithm (O(log i)). The return value is between 0 and n - 1. If n is negative, it is first replaced with its absolute value.

Throws:
IllegalArgumentException - If n is 0.

primitiveRoot

public static int primitiveRoot(int p)
Returns the smallest positive primitive root modulo the prime p.

Throws:
IllegalArgumentException - If p is not prime.

quadRes

public static int quadRes(int a,
                          int b)
Returns the value of the quadratic residue symbol (a|b). Here b must be odd. We interpret (2|b) as -1 to the power (b2-1)/8.

Returns:
1 or -1, or is 0 if the inputs are not relatively prime.
Throws:
IllegalArgumentException - If b is even, or (in particular) 0.

orderGL

public static BigInteger orderGL(int n,
                                 int q)
Returns the order of GL(n) over the finite field of q elements. n must be ≥ 1, and q must be a prime power.


orderSL

public static BigInteger orderSL(int n,
                                 int q)
Returns the order of SL(n) over the finite field of q elements. n must be ≥ 1, and q must be a prime power.


orderPGL

public static BigInteger orderPGL(int n,
                                  int q)
Returns the order of PGL(n) over the finite field of q elements. n must be ≥ 1, and q must be a prime power.


orderPSL

public static BigInteger orderPSL(int n,
                                  int q)
Returns the order of PSL(n) over the finite field of q elements. n must be ≥ 1, and q must be a prime power.


main

public static void main(String[] args)
Tests the order of GL(n,q) and friends. Give it n q.


pairsToString

public static String pairsToString(int[][] x)
E.g., converts [[2,3],[3,1]] to
2^3 * 3.