Dart BigInt modPow()
Syntax & Examples

BigInt.modPow() method

The `modPow()` method returns this BigInt to the power of `exponent` modulo `modulus`.


Syntax of BigInt.modPow()

The syntax of BigInt.modPow() method is:

 BigInt modPow(BigInt exponent BigInt modulus) 

This modPow() method of BigInt returns this integer to the power of exponent modulo modulus.

Parameters

ParameterOptional/RequiredDescription
exponentrequiredthe exponent
modulusrequiredthe modulus

Return Type

BigInt.modPow() returns value of type BigInt.



✐ Examples

1 Calculate modular exponentiation

In this example,

  1. We define a base BigInt base, an exponent BigInt exponent, and a modulus BigInt modulus.
  2. We use the modPow() method to calculate the result of base^exponent mod modulus.
  3. We print the result to standard output.

Dart Program

void main() {
  BigInt base = BigInt.from(2);
  BigInt exponent = BigInt.from(10);
  BigInt modulus = BigInt.from(7);
  BigInt result = base.modPow(exponent, modulus);
  print('Result of 2^10 mod 7: $result');
}

Output

Result of 2^10 mod 7: 2

2 Calculate another modular exponentiation

In this example,

  1. We define a base BigInt base, an exponent BigInt exponent, and a modulus BigInt modulus.
  2. We use the modPow() method to calculate the result of base^exponent mod modulus.
  3. We print the result to standard output.

Dart Program

void main() {
  BigInt base = BigInt.from(3);
  BigInt exponent = BigInt.from(25);
  BigInt modulus = BigInt.from(5);
  BigInt result = base.modPow(exponent, modulus);
  print('Result of 3^25 mod 5: $result');
}

Output

Result of 3^25 mod 5: 3

3 Calculate one more modular exponentiation

In this example,

  1. We define a base BigInt base, an exponent BigInt exponent, and a modulus BigInt modulus.
  2. We use the modPow() method to calculate the result of base^exponent mod modulus.
  3. We print the result to standard output.

Dart Program

void main() {
  BigInt base = BigInt.from(5);
  BigInt exponent = BigInt.from(3);
  BigInt modulus = BigInt.from(13);
  BigInt result = base.modPow(exponent, modulus);
  print('Result of 5^3 mod 13: $result');
}

Output

Result of 5^3 mod 13: 8

Summary

In this Dart tutorial, we learned about modPow() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.