repthy
Class NumThy

Object
  extended byNumThy

public class NumThy
extends Object

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

"Cohen" means Henri Cohen, A Course in Computational Number Theory, Graduate Texts in Mathematics 138, Springer.

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 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 n greater than or equal to 0 (the 0th prime is 2) and less than 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 > the argument.
static BigInteger orderGL(int n, int q)
          Returns the order of GL(n) over the finite field of q elements, as a BigInteger.
static BigInteger orderPGL(int n, int q)
          Returns the order of PGL(n) over the finite field of q elements, as a BigInteger.
static BigInteger orderPSL(int n, int q)
          Returns the order of PSL(n) over the finite field of q elements, as a BigInteger.
static BigInteger orderSL(int n, int q)
          Returns the order of SL(n) over the finite field of q elements, as a BigInteger.
static int power(int a, int i, int n)
          Returns a^i modulo n.
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 phi-function, the totient, of n.
static int totient(int[][] fac)
          Returns the Euler phi-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. It is related to the density of p such that a given a greater than 1 is a primitive root, but we must first adjust 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 n greater than or equal to 0 (the 0th prime is 2) and less than 4792. This provides all the primes less than or equal to the square root of 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, and the exponents will be positive. If n is negative, it is silently replaced with its absolute value. The factorization of 1 is an array of length 0.

Throws:
IllegalArgumentException - If n is 0.

nextPrime

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

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

totient

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

Throws:
IllegalArgumentException - If n is less than 1.

totient

public static int totient(int[][] fac)
Returns the Euler phi-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 gcd of a and b is d and au + bv = d. From 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)
Returns a^i modulo n. The return value is between 0 and n - 1. If n is negative, it is first replaced with its absolute value.

Uses a binary powering algorithm (O(log i)).

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 not be even. 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, as a BigInteger. n must be greater than or equal to 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, as a BigInteger. n must be greater than or equal to 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, as a BigInteger. n must be greater than or equal to 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, as a BigInteger. n must be greater than or equal to 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.