Skip to content

Instantly share code, notes, and snippets.

@farleyknight
Created November 23, 2012 23:21
Show Gist options
  • Save farleyknight/4137690 to your computer and use it in GitHub Desktop.
Save farleyknight/4137690 to your computer and use it in GitHub Desktop.
package primes;
import java.util.*;
public class Algorithm1 {
// Find the nth prime number
public static int nthPrime(int n) {
// We are at the kth prime number, so far
int k = 1;
// We start our counter at 1
int i = 1;
// Count up by kth primes until we've reached
// the nth prime.
while (k <= n) {
i += 1;
if (isPrime(i)) {
k += 1;
}
}
return i;
}
// Check if the integer i is prime by computing the
// remainder of i against all the numbers less than i.
//
// Very brute force.
public static boolean isPrime(int i) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
return false;
}
}
return true;
}
}
package primes;
import java.util.*;
public class Algorithm2 {
static ArrayList<Integer> primes;
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
System.out.println(nthPrime(n));
}
// Find the nth prime number
public static int nthPrime(int n) {
// Initialize our list of primes
primes = new ArrayList<Integer>();
// We are at the kth prime number, so far
int k = 1;
// We start our counter at 1
int i = 1;
// Count up by kth primes until we've reached
// the nth prime.
while (k <= n) {
i += 1;
if (isPrime(i)) {
k += 1;
}
}
return i;
}
// A better way to check for primes by keeping track of
// previously found primes.
public static boolean isPrime(int i) {
int j = 0;
while (j < primes.size() && primes.get(j) < i) {
if (i % primes.get(j) == 0) {
return false;
}
j += 1;
}
// Once we find a prime, add it to the list
primes.add(i);
return true;
}
public void testPrimes() {
// ...
}
}
package primes;
import java.util.*;
public class Algorithm3 {
static ArrayList<Integer> primes;
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
System.out.println(nthPrime(n));
}
// Find the nth prime number
public static int nthPrime(int n) {
// Initialize our list of primes
primes = new ArrayList<Integer>();
// We are at the kth prime number, so far
int k = 1;
// We start our counter at 1
int i = 1;
// Count up by kth primes until we've reached
// the nth prime.
while (k <= n) {
i += 1;
if (isPrime(i)) {
k += 1;
}
}
return i;
}
// A better way to check for primes by keeping track of
// previously found primes.
public static boolean isPrime(int i) {
int j = 0;
// We only need to check the primes less than or equal to the sqrt(i)
while (j < primes.size() && primes.get(j) <= Math.sqrt(i)) {
if (i % primes.get(j) == 0) {
return false;
}
j += 1;
}
primes.add(i);
return true;
}
}
<project name="PrimeNumberAlgos" default="dist" basedir=".">
<description>
A few prime number algorithms written in Java.
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="lib" location="lib"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<path id="classpath.base" />
<path id="classpath.test">
<pathelement location="/usr/share/java/junit.jar" />
<pathelement location="${build}" />
<pathelement location="${src}" />
<path refid="classpath.base" />
</path>
<target name="init" depends="clean">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source">
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile" description="generate the distribution">
<mkdir dir="${dist}/lib"/>
<jar jarfile="${dist}/lib/PrimeNumberAlgos-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="test" depends="compile" description="run the unit tests">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="primes.TestAlgos" />
</junit>
</target>
<target name="clean" description="clean up" >
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
import junit.framework.*;
public class JUnitTestExample extends TestCase{
public void testCase1(){
assertTrue( "TestExample", true );
}
}
package primes;
import java.util.*;
public class Main {
public static void main(String[] args) {
// Do stuff here..
}
}
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.*;
public class PrimeNumber2 {
static ArrayList<Integer> primes;
// Find the nth prime number
public static int nthPrime(int n) {
// Initialize our list of primes
primes = new ArrayList<Integer>();
// We are at the kth prime number, so far
int k = 1;
// We start our counter at 1
int i = 1;
// Count up by kth primes until we've reached
// the nth prime.
while (k <= n) {
i += 1;
if (isPrime(i)) {
k += 1;
}
}
return i;
}
// A better way to check for primes by keeping track of
// previously found primes.
public static boolean isPrime(int i) {
int j = 0;
while (j < primes.size() && primes.get(j) < i) {
if (i % primes.get(j) == 0) {
return false;
}
j += 1;
}
// Once we find a prime, add it to the list
primes.add(i);
return true;
}
}
package primes;
import junit.framework.*;
public class TestAlgos extends TestCase {
public static int primes[] = {1,2,3,5,7,11,13,17,19,23,29,31,37,
41,43,47,53,59,61,67,71,73,79,83,89,
97,101,103,107,109,113,127,131,137,
139,149,151,157,163,167,173,179,181,
191,193,197,199,211,223,227,229,233,
239,241,251,257,263,269,271,277,281,
283,293,307,311,313,317,331,337,347,
349,353,359,367,373,379,383,389,397,
401,409,419,421,431,433,439,443,449,
457,461,463,467,479,487,491,499,503,
509,521,523,541};
// Test the first 100 primes, including the 0th prime being 1
public void testAlgo1() {
for (int i = 0; i < 100; i++) {
assertEquals("Check prime " + i, primes[i], Algorithm1.nthPrime(i));
}
}
public void testAlgo2() {
for (int i = 0; i < 100; i++) {
assertEquals("Check prime " + i, primes[i], Algorithm2.nthPrime(i));
}
}
public void testAlgo3() {
for (int i = 0; i < 100; i++) {
assertEquals("Check prime " + i, primes[i], Algorithm3.nthPrime(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment