Function reference · Math

powmod

Computes base^exponent mod modulus exactly, without computing the full power.

Notation

powmod(base, exponent, modulus)

Write it in an expression: nerdamer('powmod(2,10,7)'), or in the Playground.

nerdamer.powmod

nerdamer.powmod(base: ExpressionInput, exponent: ExpressionInput, modulus: ExpressionInput): Expression

A method on the default import. Arguments can be strings, as in Notation.

Parameters

base
An integer.
exponent
A nonnegative integer.
modulus
A positive integer.

Returns

The remainder, an integer from 0 up to modulus - 1.

Examples

1024 divided by 7 leaves 2.

powmod(2,10,7)
// → 2
Open in Playground

Large exponents are fast, because the full power is never built.

powmod(3,200,13)
// → 9
Open in Playground

A negative base still gives a remainder from 0 up to modulus - 1.

powmod(-3,3,7)
// → 1
Open in Playground

A symbolic argument keeps the call as it is.

powmod(x,2,7)
// → powmod(x, 2, 7)
Open in Playground

Good to know

  • A negative exponent or a modulus below 1 is an error.

JavaScript examples

nerdamer.powmod(2, 10, 7).text()
// → 2

← Back to Function reference