Modular congruence

From LifeWiki
Jump to navigation Jump to search
Ambox notice.png This article may require cleanup to meet LifeWiki's quality standards.

Let p%q = p-⌊pq⌋*q ("p modulo q"), ie. adding or subtracting integer multiples of q to p such that it lies in the range 0 ≤ x < q.[n 1]

Two numbers a,b are congruent modulo a third number n iff a%n = b%n.

Such congruences are denoted as a ≡ b (mod n). For instance, 5 ≡ 8 (mod 3), because 5%3 = 5-3 = 8%3 = 8-6 = 2. Alternatively (for integer modulo bases n), x%n is equivalent to taking x's expansion in base n and removing all but the last digit. Modular arithmetic is generally concerned with the behaviour of operations under modulo by a fixed base.

Given fixed integers k,x ≠ 0, the sequence a(i) = i*k % n oscillates, with period ngcd(n,k) = lcm(n,k)k.

For a modulo base n and period p, for there to exist any integers k with sequences of period p, p must be a divisor of n (written p|n). The number of k whose addition has period p is

#({k: k ∈ ℕ and k<n and ngcd(n,k)=p}) = { φ(p) . p|n0 . . .else

where φ(p) is Euler's totient function, the number of 1 ≤ k < p such that gcd(p,k)=0 (A000010).[n 2]

Modular multiplication

Two numbers a,b are called multiplicative inverses modulo n if a*b ≡ 1 (mod n), and b is then occasionally written a-1.

If gcd(n,a) ≠ 1, gcd(n,a) will divide any number of the form a*b % n, which as such cannot equal 1, so a does not have a multiplicative inverse. However, the set S = {k: k ∈ ℕ and k<n and k|n} form a group under multiplication, meaning that for all integers c ∈ S, {c*k%n: k ∈ S} = S (ie. mapping a multiplication over the set permutes it[n 3]), and every member has an inverse in the set (findable using the extended Euclid's algorithm).

There is no similarly simple statement for fixed n and p, enumerating the k such that the period of multiplication by k (modulo n) is p. Multiplication by numbers not coprime to n is neither

  • injective (such that all numbers 0 ≤ x < n produce distinct values under multiplication by k modulo n), since there are only ngcd(n,k) destinations they can be mapped to, nor
  • surjective (such that for given n,k, every value y can have an x found such that k*x ≡ y (mod n)), since x would generally have to equal y*k-1.

Due to this, similarly to cellular automata in spaces with finitely many states, recursively applying a multiplication will cause integers to eventually enter one of several cycles, across which the period varies.[n 4] Implementations of modular exponentiation to large powers with small modulo bases can be further optimised by detecting the length of the cycle that an exponentiation process enters and finding the phase that it ends in by taking the remaining exponent (upon entry to the cycle) modulo this length, and using it as the index in the cycle.

The Chinese remainder theorem states that an unknown x's value (mod n) can be determined uniquely from a set of congruences of the form x ≡ ci (mod mi) for .mi ∈ m(mi) = n, where all pairs of indexes i ≠ j have gcd(mi,mj).

Relationship with bitwise operators

In programming, where x,n are integer (as is usually necessary for bitwise operators), because it corresponds with truncating x's base-2n expansion to the last digit, and its binary expansion to the last n bits, x % 2n = x & 2n-1, the latter of which can be more efficient to compute. The Stanford bit-twiddling hacks explain division by 1 less than a power of 2.

Notable congruences

These provide algebraic reduction steps for forms that can arise generally in modular arithmetic problems.

It is also given as cp-1 ≡ 1 (mod p), however this pertains only for coprime c,p, whereas cp ≡ n (mod p) is irrespective
  • Wilson's theorem states that for an integer n, (n-1)! ≡ -1 (mod n) if n is prime, otherwise (n-1)! ≡ 0 (mod n) unless n = 4, in which case it is congruent instead to 2.[n 6]
  • Lucas's theorem: (nk) ≡ ∏i=0(niki) (mod b), where ni is the ith base-b digit of n.

for modular exponentiation computation

  • x*y ≡ (x%n)*(y%n) (mod n), so xp%n can be calculated as (( ... (((x%n)*x)%n)* ... )*x)%n (preventing any number partway through the computation from exceeding (n-1)*x)
  • Euler's theorem: For coprime x,m xφ(m) ≡ 1 (mod m), so xn ≡ xn%φ(m) (mod m).
  • If n is computed from its factorisation, n%φ(m) can be computed efficiently also
  • Note that Fermat's little theorem is the special case of Euler's theorem, since for primes p, φ(p) = p-1
see also exponentiation by squaring

Uses in Conway's Game of Life

See also

  • Pólya enumeration theorem (the enumeration equations must return integers, so the division in the averaging is equivalent to floordiv, meaning the output of the equivalent modulo must be 0, through which the theorems above can be proven)

Notes

Let f=λ n,k: ' '*(len(str(n))-len(str(k)))+str(k)

  1. Note that for p ≠ 0, this causes sgn(p%q) = sgn(q), to make pq⌋*q+p%q=p hold, as in Python. In other programming languages such as C, p%q = |p-⌊pq⌋*q|*sgn(p), so ie. -4%3=-1, since its integer integer division operator, /, rounds towards 0 instead of -∞. In most programming languages, modulo has the same precedence as multiplication and division, and expressions involving multiple of the same precedence adjacent without brackets are evaluated left-to-right.
  2. These can be tabulated by running print('\n'.join(map(λ n: 'n='+f(m,n)+': (\n '+',\n '.join(map(λ p: p[1],filter(λ p: p[0],map(λ p: (λ t: (len(t),'p='+f(m,p)+': ('+','.join(map(str,t))+')'))(tuple(filter(λ k: n//gcd(n,k)==p,range(n)))),range(1,n+1)))))+')',range(m:=16)))) in the Python terminal. The lengths of the tuples, over all values of p (instead of only nonzero ones), is the triangular array A054522.
  3. a multiplication by a coprime to n also permutes the entire set of congruence classes, not just the other coprimes.
  4. An equivalent program to the previous one (for the cycle obtained by beginning at x=1) is print('\n'.join(map(λ n: 'n='+f(m,n)+': ('+','.join((λ t: map(λ p: f(m,t.count(p)),range(1,n)))(tuple(map(λ k: (λ f: f(f))(λ f: λ r,t: (λ r: t.index(r)+1 if r in t else f(f)(r,(r,)+t))(k*r%n))(1,()),range(1,n)))))+')',range(1,m:=16)))), which generates A057593,
    and the number of cycles by print('\n'.join(map(λ n: 'n='+f(m,n)+': '+','.join(map(λ k: f(m,(λ f: f(f))(λ f: λ c,i,o,p: (λ o: o if all(c) else f(f)(c,c.index(0),o,p+1))(o+(c[i]==p)) if c[i] else f(f)(c[:i]+(p,)+c[i+1:],k*i%n,o,p))((0,)*n,0,0,1)),range(n))),range(1,m:=24)))) (returning A348888, which has a nice diagram showing the cycles formed by different n,k).
  5. Being prime is sufficient but not necessary, composite p for which np ≡ n (mod p) are called Fermat pseudoprimes to base n. The smallest composite pseudoprime p to n is A000790(n), which is periodic over n with period 277# * 23#, where # is the primorial.
  6. Explained as an exercise in the MODA chapter involving placements of rooks on toroidal chessboards, and in the Pólya enumeration theorem article

Further reading