Created
February 24, 2010 18:19
-
-
Save ananthakumaran/313682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Prime { | |
public static boolean isPrime1(int n) { | |
if (n <= 1) { | |
return false; | |
} | |
if (n == 2) { | |
return true; | |
} | |
for (int i = 2; i <= Math.sqrt(n) + 1; i++) { | |
if (n % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean isPrime2(int n) { | |
if (n <= 1) { | |
return false; | |
} | |
if (n == 2) { | |
return true; | |
} | |
if (n % 2 == 0) { | |
return false; | |
} | |
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { | |
if (n % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
// http://stackoverflow.com/questions/2385909/most-elegant-way-to-write-isprime-in-java/2385999#2385999 | |
public static boolean isPrime3(long n) { | |
if(n < 2) return false; | |
if(n == 2 || n == 3) return true; | |
if(n%2 == 0 || n%3 == 0) return false; | |
long sqrtN = (long)Math.sqrt(n)+1; | |
for(long i = 6L; i <= sqrtN; i += 6) { | |
if(n%(i-1) == 0 || n%(i+1) == 0) return false; | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.Map.Entry; | |
import java.util.TreeMap; | |
import junit.framework.Assert; | |
import org.junit.Test; | |
public class PrimeTest { | |
public PrimeTest() { | |
} | |
@Test | |
public void testIsPrime() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { | |
Prime prime = new Prime(); | |
TreeMap<Long, String> methodMap = new TreeMap<Long, String>(); | |
for (Method method : Prime.class.getDeclaredMethods()) { | |
long startTime = System.currentTimeMillis(); | |
int primeCount = 0; | |
for (int i = 0; i < 1000000; i++) { | |
if ((Boolean) method.invoke(prime, i)) { | |
primeCount++; | |
} | |
} | |
long endTime = System.currentTimeMillis(); | |
Assert.assertEquals(method.getName() + " failed ", 78498, primeCount); | |
methodMap.put(endTime - startTime, method.getName()); | |
} | |
for (Entry<Long, String> entry : methodMap.entrySet()) { | |
System.out.println(entry.getValue() + " " + entry.getKey() + " Milli seconds "); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isPrime3 2213 Milli seconds | |
isPrime2 3039 Milli seconds | |
isPrime1 6030 Milli seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment