Here you can find the source of isBigPrime(BigInteger number)
public static boolean isBigPrime(BigInteger number)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 liXiaopeng. All rights reserved. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * * Contributors://from www . j a v a 2 s .c om * LiXiaopeng - initial API and implementation * * Create on 2011-3-7 ????03:28:18 *******************************************************************************/ import java.math.BigInteger; import java.util.Random; public class Main { public static boolean isBigPrime(BigInteger number) { BigInteger random = createRandomInteger(number, new Random()); return testPrime(number, random); } public static boolean isBigPrime(BigInteger number, int agains) { BigInteger[] randoms = new BigInteger[agains]; Random r = new Random(); for (int index = 0; index < agains; index++) { randoms[index] = createRandomInteger(number, r); } for (BigInteger bi : randoms) { if (!testPrime(number, bi)) { return false; } } return true; } private static BigInteger createRandomInteger(BigInteger number, Random random) { BigInteger result = new BigInteger(number.bitLength(), random); if (result.compareTo(number) >= 0) { result = result.divide(BigInteger.valueOf(2)); } if (result.compareTo(BigInteger.valueOf(0)) == 0) { result = BigInteger.valueOf(1); } return result; } private static boolean testPrime(BigInteger number, BigInteger random) { System.out.println(random.intValue()); BigInteger result = random.modPow( number.add(BigInteger.valueOf(-1)), number); System.out.println(result.intValue()); return result.intValue() == 1; } }