Compute the multi-exponent base^exponent (modulo modulus) . - Java java.lang

Java examples for java.lang:Math Operation

Description

Compute the multi-exponent base^exponent (modulo modulus) .

Demo Code

// This program is free software; you can redistribute it and/or
//package com.java2s;

import java.math.BigInteger;

public class Main {
    /**//from w  w w  .  j  a v a 2  s . co  m
     * 
     * Compute the multi-exponent {@code base^exponent (modulo
     * modulus)}. 
     * <P>
     *
     * Asserts that the {@code base} and {@code exponent} array have
     * the same length.
     * 
     * @param base the bases
     * @param exponent the exponents
     * @param modulus
     * @return the exponent {@code base[0]^exponent[0] *
     * base[1]^exponent[1] * ... (modulo modulus)}
     */
    public static BigInteger multi_exponent(BigInteger[] base,
            BigInteger[] exponent, BigInteger modulus) {
        assert base.length == exponent.length;
        BigInteger res = BigInteger.ONE;
        for (int i = 0; i < base.length; i++)
            res = res.multiply(base[i].modPow(exponent[i], modulus)).mod(
                    modulus);
        return res;
    }
}

Related Tutorials