Java BigInteger Calculate splitBigInt(BigInteger value, int n)

Here you can find the source of splitBigInt(BigInteger value, int n)

Description

split Big Int

License

Open Source License

Declaration

public static BigInteger[] splitBigInt(BigInteger value, int n) 

Method Source Code

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

import java.math.BigInteger;

public class Main {
    public static BigInteger[] splitBigInt(BigInteger value, int n) {
        if (n <= 1) {
            return new BigInteger[] { value };
        }//from w w  w  .  j  a v  a2s .c o m

        BigInteger[] values = new BigInteger[n];
        BigInteger total = BigInteger.ZERO;
        for (int i = 0; i < n; ++i) {
            values[i] = value.multiply(BigInteger.valueOf(Math.round(Math.random() * 100)));
            total = total.add(values[i]);
        }

        BigInteger mod = total.divide(value);

        if (mod.compareTo(BigInteger.ZERO) == 0) {
            return new BigInteger[] { value };
        }

        total = BigInteger.ZERO;
        for (int i = 0; i < n; ++i) {
            values[i] = values[i].divide(mod);
            total = total.add(values[i]);
        }

        int randIndex = (int) Math.ceil(Math.random() * values.length) - 1;

        //Add any remainder to a random index
        values[randIndex] = values[randIndex].add(value.subtract(total));

        return values;
    }
}

Related

  1. serializeModexpResponse(BigInteger response)
  2. serializeQuery(BigInteger modulus, BigInteger base, BigInteger exponent, boolean brief, String... modexps)
  3. shift(BigInteger integer, int distance)
  4. shiftLeft(final BigInteger register, final BigInteger input, final int n)
  5. split(BigInteger maxValue, int numberOfSplits)
  6. splitFloat(BigInteger rawFloat, int signWidth, int exponentWidth, int mantissaWidth)
  7. sqrt(BigInteger n)
  8. sqrt(BigInteger n)
  9. sqrt(BigInteger n)