Java examples for java.lang:int prime
Check for primality using a very simple method.
import java.math.BigDecimal; import java.math.BigInteger; import java.security.SecureRandom; public class Main{ /** Convienence variable for the BigDecimal Zero */ private static BigDecimal ZERO = new BigDecimal("0"); /** Convienence variable for the BigDecimal One */ private static BigDecimal ONE = new BigDecimal("1"); /** Convienence variable for the BigDecimal Two */ private static BigDecimal TWO = new BigDecimal("2"); /** Convienence variable for the BigInteger Zero */ private static BigInteger ZERO_INT = new BigInteger("0"); /** Convienence variable for the BigInteger Two */ private static BigInteger TWO_INT = new BigInteger("2"); /** The amount that the square root could be wrong. */ static private BigDecimal error; /** Counter for the number of iterations */ static private int iterations; /** The scale for division functions */ static private int scale = DEFAULT_SCALE; /** The maximum number of iterations when calculating the square root */ static private int maxIterations = DEFAULT_MAX_ITERATIONS; /** Thirty Seconds in milliseconds */ static public final long THIRTY_SECONDS = 30000; /**/* ww w .java2 s. c om*/ * <p> * Check for primality using a very simple method. * </p> * <p> * We will iterate through the odd numbers X and use the big integer B to * check B = 0 (mod X). If this is true then B is not prime and we exit. * Otherwise we continue the search till we reach the square root of B. * </p> * * <dd><b>Input</b> : <i>n</i> : An odd integer greater than 1 </dd> * <dd><b>Output</b> : the answer <u>composite</u> or <u>prime</u></dd> * <dd><i>sqrt</i> := square root of <i>n</i></dd> * <dd>for <i>i</i> = 2; <i>i</i> < <i>sqrt</i>+2; <i>i</i>++</dd> * <dl> * <dd>if (<i>n</i> mod <i>i</i> == 0) return <u>composite</u></dd> * </dl> * <dd>return <u>prime</u></dd> * * @param n * BigInteger to check for primality * @return true if the BigInteger is prime */ static public boolean checkPrimality(BigInteger n, boolean verboseOutput) { boolean rval = true; long time = System.currentTimeMillis(); if (verboseOutput) { System.out.println("Big Integer = " + n); System.out.println("Starting time = " + time); } if (n.mod(TWO_INT).compareTo(ZERO_INT) == 0) { rval = false; if (verboseOutput) System.out.println(n + " is divisible by 2"); } else { BigInteger sqrt = getSquareRoot(n).toBigInteger(); BigInteger modulo = new BigInteger("3"); if (verboseOutput) { System.out.println("Square Root = " + sqrt); System.out.println("Current Modulo = " + modulo); } while ((modulo.compareTo(sqrt) <= 0) && rval) { time = checkTime(modulo, time); if (n.mod(modulo).compareTo(ZERO_INT) == 0) { rval = false; if (verboseOutput) { System.out.println("Current Modulo = " + modulo); System.out .println(n + " is divisible by " + modulo); } } else { modulo = modulo.add(TWO_INT); } } if (rval) { if (verboseOutput) { System.out.println("Current Modulo = " + modulo); } } } return rval; } /** * <p> * Wrapper function to getSquareRoot(BigDecimal n) * </p> * * @param n * BigInteger to find the square root of * @return square root approximation */ static public BigDecimal getSquareRoot(BigInteger n) { return getSquareRoot(new BigDecimal(n)); } /** * <p> * Here we use the algorithm of the ancient greek mathematitian Heron of * Alexandria to find the square root of the number. It can be found in his * anthology Metrica, book 1 chapter 8. The algorithm has been redone over * the centuries by Newton and Ibn al-Banna. * </p> * * <p> * adapted from <a href="http://www.merriampark.com/bigsqrt.htm"> * http://www.merriampark.com/bigsqrt.htm * </a> * </p> * * @param n * the BigDecimal to find the square root of * @return the approximate square root. */ static public BigDecimal getSquareRoot(BigDecimal n) { // Make sure n is a positive number if (n.compareTo(ZERO) <= 0) { throw new IllegalArgumentException(); } BigDecimal initialGuess = getInitialApproximation(n); // System.out.println ("Initial guess " + initialGuess.toString ()); BigDecimal lastGuess = ZERO; BigDecimal guess = new BigDecimal(initialGuess.toString()); // Iterate iterations = 0; boolean more = true; while (more) { lastGuess = guess; guess = n.divide(guess, scale, BigDecimal.ROUND_HALF_UP); guess = guess.add(lastGuess); guess = guess.divide(TWO, scale, BigDecimal.ROUND_HALF_UP); // System.out.println ("Next guess " + guess.toString ()); error = n.subtract(guess.multiply(guess)); if (++iterations >= maxIterations) { more = false; } else if (lastGuess.equals(guess)) { more = error.abs().compareTo(ONE) >= 0; } } return guess; } /** * <p> * Utility function to output the current modulo every 30 seconds * </p> * * @param m * The current modulo * @param t * the time since the last print out * @return time since last printout */ static private long checkTime(BigInteger m, long t) { long rval = t; if (System.currentTimeMillis() - t >= THIRTY_SECONDS) { System.out.println("Current Modulo = " + m); rval = System.currentTimeMillis(); } return rval; } /** * <p> * Find the initital approximation to begin the search for the square root * of the prime number * </p> * * <p> * adapted from <a href="http://www.merriampark.com/bigsqrt.htm"> * http://www.merriampark.com/bigsqrt.htm * </a> * </p> * * @param n * the number to ge the approximation of when starting the square * root function * @return the intitial approximation */ static private BigDecimal getInitialApproximation(BigDecimal n) { BigInteger integerPart = n.toBigInteger(); int length = integerPart.toString().length(); if ((length % 2) == 0) { length--; } length /= 2; BigDecimal guess = ONE.movePointRight(length); return guess; } }