Here you can find the source of powerMod(long base, long exp, long mod)
Parameter | Description |
---|---|
base | Number to use as base |
exp | Number to use as exponent |
mod | Number to use as modulus |
public static long powerMod(long base, long exp, long mod)
//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; } }