Java Number Power powerMod(long base, long exp, long mod)

Here you can find the source of powerMod(long base, long exp, long mod)

Description

Modular power.

License

Open Source License

Parameter

Parameter Description
base Number to use as base
exp Number to use as exponent
mod Number to use as modulus

Return

The value of the operation (base^exp) mod modulus.

Declaration

public static long powerMod(long base, long exp, long mod) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w ww . j a va 2  s  .c o  m*/
     * Modular power.
     *
     * @param base Number to use as base
     * @param exp  Number to use as exponent
     * @param mod  Number to use as modulus
     * @return The value of the operation (base^exp) mod modulus.
     */
    public static long powerMod(long base, long exp, long mod) {
        long result = 1;
        base %= mod;
        while (exp > 0) {
            if ((exp & 1) != 0)
                result = (result * base) % mod;
            exp >>= 1;
            base = base * base % mod;
        }
        return result < 0 ? result + mod : result;
    }
}

Related

  1. power2(int power)
  2. powerConverter(String _varSymbol)
  3. powerLevelToDb(double value)
  4. powerLevelToDb0(double value)
  5. powerMean(final double p, final double[] x)
  6. powerOf(final int value, final int powerOf)
  7. powerOf10(int number)
  8. powerOf2Log2(int n)
  9. powerOf31(int n)