This file implements a number of functions on integers that fit into 32 bits.
In particular, it allows (non-probabilistic) primality testing, factorization of all (32-bit) MachineInteger (−231 ≤ x < 231) by using trial division with the sequence of 16-bit primes.
The functions are tested in test/sitools/sitools.as.nw.
Exports of SmallIntegerTools
nextPrime: MachineInteger -> MachineInteger Returns the closest prime bigger than the given one.
previousPrime: MachineInteger -> MachineInteger Returns the closest prime smaller than the given one.
prime?: MachineInteger -> Boolean Primality test.
sqrt: MachineInteger -> MachineInteger Integer root.
factor: MachineInteger -> SparseIndexedPowerProduct(I, I) Factors positive integer smaller than 231.
moebiusMu: SparseIndexedPowerProduct(I, I) -> MachineInteger Compute the Möbius μ function.
eulerPhi: SparseIndexedPowerProduct(I, I) -> MachineInteger Compute the Euler ϕ function.
Export of SmallIntegerTools
nextPrime: MachineInteger -> MachineInteger
Description
Returns the closest prime bigger than the given one.
Export of SmallIntegerTools
previousPrime: MachineInteger -> MachineInteger
Description
Returns the closest prime smaller than the given one.
Export of SmallIntegerTools
prime?: MachineInteger -> Boolean
Usage
macro I == MachineInteger;
n: I := max;
assert(prime? n);
n: I := 2^30 + 3;
assert(prime? n);
Description
Primality test.
Export of SmallIntegerTools
sqrt: MachineInteger -> MachineInteger
Usage
macro I == MachineInteger;
n: I := 2^30 + 3;
k: I := sqrt n;
assert(k = 2^15);
n := 2^30 - 35;
k := sqrt n;
assert(k = 2^15 -1);
Description
Integer root.
For an integer 0 ≤ n < 231 the function returns k = ⌊⌋.
The algorithm loops over the bits of the result starting with the biggest and adjusts them. If n = ∑ i=0rni2i with ni ∈ and nr = 1 then the highest bit in k appears at position s = ⌊⌋. We take 2s as as starting value and add more bits to the result by checking whether the square of the result will become bigger than n.
Export of SmallIntegerTools
factor: MachineInteger -> SparseIndexedPowerProduct(I, I)
Description
Factors positive integer smaller than 231.
The following is basically an implementation of Algorithm A of Section 4.5.4 in [Knu69], i. e., it uses trial division with a sequence of all 16-bit primes as division candidates.
Export of SmallIntegerTools
moebiusMu: SparseIndexedPowerProduct(I, I) -> MachineInteger
Description
Compute the Möbius μ function.
The following function uses formula (4.57) of [GKP94]. If m = p1m1p2m2…prmr is the factorization of m ∈ ℕ into distinct prime factors p1,…,pr then the Möbius function is
(182) |
Export of SmallIntegerTools
eulerPhi: SparseIndexedPowerProduct(I, I) -> MachineInteger
Description
Compute the Euler ϕ function.
The Euler ϕ or totient function ϕ(m) encodes the number of divisors of m that are relatively prime to m. See, for example, [GKP94, Chp. 4.9].
It is defined as ϕ(1) = 1 and ϕ(pk) = pk − pk−1 for any prime number p and any integer k ≥ 1. Furthermore, ϕ is multiplicative, i. e., ϕ(m1m2) = ϕ(m1)ϕ(m2) if gcd(m1,m2) = 1. If m = p1m1p2m2…prmr is the factorization of m ∈ ℕ into distinct prime factors p1,…,pr then the Euler totient function is defined by
(183) |
(184) |