Optimised Prime Numbers generation and testing (JAVA Code)

Hi, there.

This code was actually written by our teacher, who was an expert in Object Oriented Programming and JAVA. We are placing his code here, to make it public. This code is for Optimised Prime Numbers generation.

 

Thank you sir for the code.

Optimised prime number generation algorithm

Optimised Prime Numbers generation and testing (JAVA Code)

import java.io.*;
import java.util.Set;
import java.util.LinkedHashSet;
import java.util.Iterator;

public class PrimeNumber {
	public void findPrimes(long[] primes) {
		if (primes.length == 0) return;

		primes[0] = 2;
		for (int i=1; i < primes.length; i++) {
			primes[i] = findNextPrime(primes[i-1]);
		}
	}

	public long[] findPrimes(long from, long to) {
		Set primeSet = new LinkedHashSet();

		for (long i = from; i <= to; i++) {
			long prime = findNextPrime(i);
			if (prime <= to) primeSet.add(new Long(prime));
			i = prime-1;
		}

		long[] primes = new long[primeSet.size()];
		Iterator it = primeSet.iterator();
		for(int i=0; it.hasNext(); i++) {
			primes[i] = ((Long)it.next()).longValue();
		}

		return primes;
	}

	public long findNextPrime(long num) {
		for (long i=num+1; ;i++) {
			if (isPrime(i)) return i;
		}
	}

	public void printPrimes(long[] primes) {
		for (int i=0; i < primes.length; i++) {
			System.out.println(primes[i]);
		}
	}

	public boolean savePrimes(long[] primes, String dirName, String fileName)
								throws IOException {
		if (!createFile(dirName, fileName)) return false;

		DataOutputStream primeStream = new DataOutputStream(
										new BufferedOutputStream(
										new FileOutputStream(fileName)));
		for (int i=0; i < primes.length; i++) {
			primeStream.writeLong(primes[i]);
		}

		primeStream.close();

		return true;
	}

	public boolean savePrimesAsText(long[] primes, String dirName, String fileName)
								throws IOException {
		if (!createFile(dirName, fileName)) return false;

		BufferedWriter primeWriter = new BufferedWriter(new FileWriter(fileName));
		for (int i=0; i < primes.length; i++) {
			primeWriter.write(Long.toString(primes[i]));
			//primeWriter.newLine();
		}

		primeWriter.close();

		return true;
	}

	private boolean createFile(String dirName, String fileName) {
		try {
			File primeDir = new File(dirName);
			File primeFile = new File(fileName);

			if (!primeDir.exists()) primeDir.mkdir();
			if (!primeFile.exists()) primeFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean isPrime(long num) {
		//System.out.println("isPrime: " + num);
		if (num == 2) return true;
		if (num%2 == 0) return false;

		long limit = (int)Math.ceil(Math.sqrt(num));
		//long limit = (int)(Math.sqrt(num));
		for (long i = 3; i <= limit; i+=2) {
			if (num%i == 0) return false;
		}

		return true;
	}

	public boolean isPrimeSlow(long num) {
		int count =0;

		for (long i = 1; i <= num; i++) {
			if (num%i == 0) count++;
		}

		if (count == 2) {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) throws IOException {
		PrimeNumber pn = new PrimeNumber();
		long[] primes = pn.findPrimes(1, 20);

		pn.printPrimes(primes);
		pn.savePrimes(primes, "C:/Practice", "primes.data");
		pn.savePrimesAsText(primes, "C:Practice", "primes.txt");

		System.out.println("Primes saved successfully");
	}

/*	public static void main(String[] args) {
		PrimeNumber pn = new PrimeNumber();
		long primes[] = new long[100];

		pn.findPrimes(primes);
		pn.printPrimes(primes);
	}
*/

/*	public static void main(String[] args) {
		PrimeNumber pn = new PrimeNumber();
		boolean ansSlow, ansFast;

		long num = Long.parseLong(args[0]);

		long t1 = System.nanoTime();
		ansSlow = pn.isPrimeSlow(num);
		long t2 = System.nanoTime();
		ansFast = pn.isPrimeFast(num);
		long t3 = System.nanoTime();

		//System.out.println(ansFast);
		System.out.println("Slow=" + (t2-t1)/1000 + " Fast=" + (t3-t2)/1000);
	}
*/

}

Additionally, you can download the file for optimised prime numbers generation from link below (Mediafire public link).

http://adf.ly/mWzIN

 

 

Kindly comment here freely, If you are feeling any problems regarding optimised prime number generation, Share it here.
Good Day.