PowerMod
Modular exponentiation: a^b mod m, computed without forming a^b directly.
Modular exponentiationWikidataQ1228841Wolfram LanguagePowerMod!Rosetta CodeModular exponentiationPowerMod(a, b, m)modular exponentiation, .enumeratio_power_mod(a, b, m)SymPypow(ZZ.convert(a), ZZ.convert(b), ZZ.convert(m))Oscarpowermod(ZZ(a), ZZ(b), ZZ(m))Juliapowermod(ZZ(a), ZZ(b), ZZ(m))Rustpowermod(a, b, m)PowerMod(a, 1/r, m)the least with ; more generally for enumeratio_power_mod(a, 1/r, m)SymPypow(ZZ.convert(a), ZZ.convert(1/r), ZZ.convert(m))Oscarpowermod(ZZ(a), ZZ(1/r), ZZ(m))Juliapowermod(ZZ(a), ZZ(1/r), ZZ(m))Rustpowermod(a, 1/r, m)PowerMod(u/v, b, m)a rational base, read in as , a list of bases, and an exponent of 0 at any modulusenumeratio_power_mod(u/v, b, m)SymPypow(ZZ.convert(u/v), ZZ.convert(b), ZZ.convert(m))Oscarpowermod(ZZ(u/v), ZZ(b), ZZ(m))Juliapowermod(ZZ(u/v), ZZ(b), ZZ(m))Rustpowermod(u/v, b, m)Typed signatures
| Type | Package | Applies |
|---|---|---|
(integer, rational, integer) -> integer | compute-engine | always |
(number, number, number) -> number | number-theoryreplaces residues | always |
(number, number, number) -> number | residuesreplaces compute-engine | always |
Details
- Computed by repeated squaring, without ever forming directly -- efficient even for huge .
- A negative gives the modular inverse of raised to , when it exists.
- The inverse is undefined whenever ; compute-engine leaves such calls unevaluated.
- Equal to for positive , just far more efficient. See Mod.
- A rational exponent gives the least with — the first element of PowerModList — and stays unevaluated when there is none.
- Threads over lists in any argument.
- Gaussian integers are reduced as Mod reduces them; a rational-integer modulus must be positive, and a result that comes out real is reported in .
Plot
Examples
32 more kept as data, checked against the oraclesApplications #
Properties #
Possible issues #
A nonzero exponent with a negative modulus still declines, as in Wolfram, unlike the exponent-0 case above
live ↗About
| Examples | 66 |
|---|---|
| Checked against | Wolfram Language 60 agree6 differSageMath 48 agree2 differ6 unevaluated8 errorSymPy 29 agree1 differ7 unevaluated27 errorOscar 18 agreeJulia 22 agree42 errorRust 23 agree1 differ6 unevaluated7 error |
| Crosswalk | WikipediaWikidataWolfram LanguageSageMathSymPyOscarJuliaRustRosetta Code |
Implementation
oscarpackages/oracle/src/mappings.tswolframpackages/oracle/src/mappings.tsFor a negative exponent and a Gaussian modulus m, Wolfram checks invertibility of a mod N(m) (a rational integer) rather than mod m itself, so it occasionally declines a case ours answers — e.g. PowerMod[11 - 7I, -4, 7 + 4I] errors since 11 - 7i shares a factor with N(7 + 4i) = 65, though gcd(11 - 7i, 7 + 4i) = 1.
sagepackages/oracle/src/mappings.tsSage's power_mod takes a negative exponent, like ours; no rational base or exponent. The helper reduces into the sign of the modulus and runs over the Gaussian integers, where power_mod has no method.
rustpackages/oracle/src/mappings.tsSage's power_mod takes a negative exponent, like ours; no rational base or exponent.
sympypackages/oracle/src/mappings.tsSymPy has no PowerMod function; this is Python's three-argument pow over SymPy's ZZ integers (pure-Python ones, as the runner pins), which takes a negative exponent through the modular inverse and gives the result the sign of the modulus, where ours declines a modulus below 1 unless the exponent is 0. The emit varies with the operands: a rational exponent s/r is the least root of x^r = a^s through nthroot_mod, as PowerModList (None where there is no root); a symbolic, rational or Gaussian operand goes to pow on SymPy numbers, which holds it (Mod((2 + I)**2, 3)) or ignores the modulus for a rational base. A list threads over whichever argument holds it.
juliapackages/oracle/src/mappings.ts See also: Mod, PowerModList, ModularInverse