Standard library / math/big

big.Int.ModInverse()

Compute a modular multiplicative inverse. If none exists, return nil and leave receiver unchanged; zero modulus panics. This method writes to the receiver (use a separate destination to preserve inputs when needed). Expected stdout: '4\n'.

Formal Syntax & Structure

go syntaxgo
func (z *Int) ModInverse(g, n *Int) *Int

Parameters & Attributes

NameTypeDescription
g*IntValue to invert.
n*IntNonzero modulus.

Return Value

*Int

Returns the result described above; see the example for the observed behavior.

Working Example

Example Snippetgo
package main
import (  "fmt"  "math/big")
func main() {  fmt.Println(new(big.Int).ModInverse(big.NewInt(3), big.NewInt(11)))}
Go Reference