/* * PrimalityTest.java * * Created on February 3, 2009, 3:44 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package primtesttime; import static java.lang.Math.*; /** * * @author suchenek */ public class PrimalityTest { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here PrimalityTest test = new PrimalityTest(3802499); test.go(); System.out.println(test.getOutput()); } public PrimalityTest(int n) { this.n = n; out = ""; size = round(floor(log(n)/log(2))) + 1; } public void go() { if (n < 2) return; count = 0; out = "" + n; if (isPrime()) out += " is prime \n"; else out += " is not prime \n"; out += "Size = " + size + "\n"; out += "ceil(2^(size/2)) - 2 = " + (round((ceil(exp(0.5*size*log(2))))) - 2) + "\n"; out += "floor(sqrt((2^size - 1))) - 1 = " + (round(floor(sqrt(exp(size*log(2)) - 1))) - 1) + "\n"; out += "count = " + count; } private boolean isPrime() { for (int i = 2; i <= sqrt(n); i++) { count++; if (n%i == 0) return false; } return true; } public String getOutput() { return out; } private String out; private int n, count; private long size; } /* Test data of the form 2^p - 1 2147483647 = 2^31 - 1 524287 = 2^19 - 1 131071 = 2^17 - 1 8191 = 2^13 - 1 127 = 2^7 - 1 31 = 2^5 - 1 7 = 2^3 - 1 3 = 2^2 - 1 */