org.rubato.math.arith
Class NumberTheory

java.lang.Object
  extended by org.rubato.math.arith.NumberTheory

public final class NumberTheory
extends java.lang.Object

Various number theoretic functions.

Author:
Gérard Milmeister

Method Summary
static int divideMod(int x, int y, int n)
          Divide x by y modulo n.
static int exgcd(int x, int y, int[] res)
          The extended Euclidean algorithm.
static int gcd(int x, int y)
          The greatest common divisor of two integers.
static int inverseMod(int x, int n)
          Returns an integer y such that x * y = 1 mod n.
static boolean isPrime(int n)
          Returns true iff n is a prime integer.
static int lcm(int x, int y)
          The least common multiple of x and y.
static int mod(int x, int n)
          Returns x mod n.
static int powerMod(int x, int n, int p)
          Raises x to the n-th power modulo p.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

gcd

public static int gcd(int x,
                      int y)
The greatest common divisor of two integers.

Returns:
the gcd of x and y, always non negative

exgcd

public static int exgcd(int x,
                        int y,
                        int[] res)
The extended Euclidean algorithm.

Parameters:
res - is an integer array that will contain two values such that: res[0]*x + res[1]*y = gcd(x,y)
Returns:
the greatest common divisor of x and y, always non negative

lcm

public static int lcm(int x,
                      int y)
The least common multiple of x and y.

Returns:
lcm of x and y

mod

public static int mod(int x,
                      int n)
Returns x mod n.

Parameters:
n - the modulus
Returns:
x mod n, always non-negative.

divideMod

public static int divideMod(int x,
                            int y,
                            int n)
                     throws ZeroDivisorException
Divide x by y modulo n.

Parameters:
n - the modulus of the division
Returns:
the quotient of the division
Throws:
ZeroDivisorException - if gcd(y,n) != 1

inverseMod

public static int inverseMod(int x,
                             int n)
                      throws ZeroDivisorException
Returns an integer y such that x * y = 1 mod n.

Parameters:
x - the integer to be inverted
n - the modulus of the division
Throws:
ZeroDivisorException - if gcd(x,p) != 1

powerMod

public static int powerMod(int x,
                           int n,
                           int p)
                    throws ZeroDivisorException
Raises x to the n-th power modulo p.

Returns:
x^n mod p
Throws:
ZeroDivisorException - if n < 0 and gcd(x,p) != 1

isPrime

public static boolean isPrime(int n)
Returns true iff n is a prime integer.