I was given a homework assignment in Java to create classes that find Prime number and etc (you will see in code better).class Primes {    public static boolean IsPrime(long num) {        if (num%2==0){            return false;        }        for (int i=3; i*i<=num;i+=2) {            if (num%i==0) {                return false;                }        }        return true;    }    //    End boolen IsPrime    public static int[] primes(int min, int max){        int counter=0;        int arcount=0;        for (int i=min;i<max;i++){            if (IsPrime(i)){                counter++;            }            }        int [] arr= new int[counter];        for (int i=min;i<max;i++){            if (IsPrime(i)){                arr[arcount]=i;                arcount++;            }                        }        return arr;    }    //    End Primes    public static String tostring (int [] arr){        String ans=;        for (int i=0; i<arr.length;i++){            ans= ans+arr[i]+  ;        }        return ans;    }    public static int closestPrime(long num){        long e = 0 , d = 0 , f = num;        for (int i = 2; i <= num + 1 ; i++){            if ((num + 1) % i == 0){                if ((num + 1) % i == 0 && (num + 1) == i){                    d = num + 1;                    break;                }                num++;                i = 1;            }        }        num = f;        for (int i = 2; i < num; i++){            if ((num - 1) % i == 0){                if ((num - 1) % i == 0 && (num - 1) == i){                    e = num - 1;                    break;                }                num--;                i = 1;            }        }        num = f;        if (d - num < num - e) System.out.println(Closest Prime: +d);        else System.out.println(Closest Prime: +e);        return (int) num;    }     //    End closestPrime}//end classThe goal of my code is to be faster (and correct). I'm having difficulties achieving this. Suggestions?class Primes {     public static boolean IsPrime(int num) {         if (num==1){             return false;         }         for (int i=2; i<Math.sqrt(num);i++) {             if (num%i==0) {                 return false;             }         }         return true;     }          //  End boolen IsPrime     public static int[] primes(int min, int max){         int size=0;         int [] arrtemp= new int[max-min];         for (int i=min;i<max;i++){             if (IsPrime(i)){                 arrtemp[size]=i;                 size++;             }            }         int [] arr= new int[size];         for (int i=0;i<size;i++){                arr[i]=arrtemp[i];             }          return arr;     }      public static String tostring (int [] arr){        String ans=;        for (int i=0; i<arr.length;i++){            ans= ans+arr[i]+  ;        }        return ans;    }    public static int closestPrime(int num) {        int count=1;            for (int i=num;;i++){            int plus=num+count, minus=num-count;            if (IsPrime(minus)){                return minus;            }            if (IsPrime(plus)) {                return plus;            }            count=count+1;        }    }    //    End closestPrime}//end classI think you can improve this code a bit:Test Code:/**Ex2: Overall time: 2545  m-sec*/public class PrimesTester {    public static void main(String[] args){        long t1 = new Date().getTime();  // primes        testPrime(100000);        testPrime(1000000);        testPrime(10000000);        testPrime(100000000);        long t2 = new Date().getTime();        System.out.println(Ex2: Overall time: +(t2-t1)+  m-sec);     }    public static void testPrime(int num) {            System.out.println();        System.out.println(***** Testing class Primes for: +num+*****);        int  min= num, max = min+min/2;        long t1 = new Date().getTime();  // primes        int[] primes = Primes.primes(min,max);        long t2 = new Date().getTime();        long p10_6 = Primes.closestPrime(min);        long t3 = new Date().getTime();        System.out.println(Ex2: prime test, time: +(t2-t1)+  m-sec , number of primes: +primes.length);        System.out.println(Ex2: closestPrime test, time: +(t3-t2)+  m-sec , prime: +p10_6);    }}
Primes Tester for speed performance
java;performance;homework;primes
The goal of my code is to be faster (and correct). I'm having difficulties achieving this. Suggestions?This is a neverending task. My suggestion is to stop performance improvements without a goal. To test the correctness, you should write unit tests.To explain this a little bit more: There are some advanced algorithms, like sieves, deterministic prime tests, and so on. They depend on the fact that you have to know them or that you have the math background.One of the questions is the expected input. Depending on the input, the algorithm can be optimized.An other approach would be trading computations against memory. You could precalculate all primes and store them inside the class.An other question is the architecture of the platform, depending on the virtual machine and the platform, there could be different techniques.You could use a native call, to do it in c or assembler.And so on, there is no end.Some obvious things from your code:Use a code convention, best practice would be the java code convention from sun/oracle.And, as already said, avoid abbreviations.long t1 = new Date().getTime();  // primesIf you want to measure the distance between two moments, use System.currentTimeMillis().for (int i=2; i<Math.sqrt(num);i++)This will most probably calculate the sqrt for every iteration. sqrt is one the more expensive operations. Do it once and save the result in a variable.And you do not need to test all divisors. You do not have to test with numbers, which are known not to be prime (This means, you do not have to test with any number 2*n, n>1. Only with numbers 2*n + 1, n>0. Or only with numbers 6n +- 1, n > 0. Or any similar approach around the local minimums of the euler phi function)     int [] arr= new int[size];     for (int i=0;i<size;i++){            arr[i]=arrtemp[i];         } To copy an array, you can use System.arraycopy(...).for (int i=min;i<max;i++){         if (IsPrime(i)){             arrtemp[size]=i;             size++;         }        }To find all primes, you could use the sieve of Eratosthenes, which is one of the fastest ways inside the java integer range.int [] arrtemp= new int[max-min];To reduce memory usage, increase locality and help the caches, you could use a BitSet. A custom, handmade implementation would be more efficient than the one found inside the java library.public static boolean IsPrime(long num) {    ....}You could use a deterministic Miller-Rabin test here.