IntegerNumberTheoryFunctionsΒΆ
numtheor.spad line 182 [edit on github]
This package provides various number theoretic functions on the integers.
- bernoulli: Integer -> Fraction Integer
bernoulli(n)
returns then
th Bernoulli number. this isB(n, 0)
, whereB(n, x)
is then
th Bernoulli polynomial.
- carmichaelLambda: Integer -> Integer
carmichaelLambda(n)
returns exponent of the multiplicative group of integers modulon
, that is smallest positive integerk
such thati^k rem n = 1
for alli
relatively prime ton
.
- chineseRemainder: (Integer, Integer, Integer, Integer) -> Integer
chineseRemainder(x1, m1, x2, m2)
returnsw
, wherew
is such thatw = x1 mod m1
andw = x2 mod m2
. Note:m1
andm2
must be relatively prime.
- euler: Integer -> Integer
euler(n)
returns then
th Euler number. This is2^n E(n, 1/2)
, whereE(n, x)
is then
th Euler polynomial.
- eulerPhi: Integer -> Integer
eulerPhi(n)
returns the number of integers between 1 andn
(including 1) which are relatively prime ton
. This is the Euler phi functionphi(n)
is also called the totient function.
- fibonacci: Integer -> Integer
fibonacci(n)
returns then
th Fibonacci number,F[n]
. The Fibonacci numbers are defined byF[0] = 0
,F[1] = 1
andF[n] = F[n-1] + F[n-2]
. The algorithm has running timeO(log(n)^3)
. Reference: Knuth, The Art of Computer Programming Vol 2, Semi-Numerical Algorithms.
- harmonic: Integer -> Fraction Integer
harmonic(n)
returns then
th harmonic number. This isH[n] = sum(1/k, k=1..n)
.
- jacobi: (Integer, Integer) -> Integer
jacobi(a, b)
returns the Jacobi symbolJ(a/b)
. Whenb
is odd,J(a/b) = product(L(a/p) for p in factor b )
. Note: by convention, 0 is returned ifgcd(a, b) ~= 1
. IterativeO(log(b)^2)
version coded by Michael Monagan June 1987.
- legendre: (Integer, Integer) -> Integer
legendre(a, p)
returns the Legendre symbolL(a/p)
.L(a/p) = (-1)^((p-1)/2) mod p
(p
prime), which is 0 ifa
is 0, 1 ifa
is a quadratic residuemod p
and-1
otherwise. Note: because the primality test is expensive, if it is known thatp
is prime then usejacobi(a, p)
.
- moebiusMu: Integer -> Integer
moebiusMu(n)
returns the Moebius functionmu(n)
.mu(n)
is either-1
, 0 or 1 as follows:mu(n) = 0
ifn
is divisible by a square > 1,mu(n) = (-1)^k
ifn
is square-free and hask
distinct prime divisors.
- numberOfDivisors: Integer -> Integer
numberOfDivisors(n)
returns the number of integers between 1 andn
(inclusive) which dividen
. The number of divisors ofn
is often denoted bytau(n)
.
- sumOfDivisors: Integer -> Integer
sumOfDivisors(n)
returns the sum of the integers between 1 andn
(inclusive) which dividen
. The sum of the divisors ofn
is often denoted bysigma(n)
.
- sumOfKthPowerDivisors: (Integer, NonNegativeInteger) -> Integer
sumOfKthPowerDivisors(n, k)
returns the sum of thek
th powers of the integers between 1 andn
(inclusive) which dividen
. the sum of thek
th powers of the divisors ofn
is often denoted bysigma_k(n)
.