Hi, i've came across an exercise to find what number from 1 to 10000 has maximum number of divisors.
Here's my solution:
public class Main { // main method that is executed when program starts public static void main(String[] args) { Main m = new Main(); } // declare pieces array private PieceOfNumber[] pieces = new PieceOfNumber[2]; public Main(){ // for loop to start pieces for(int i = 0; i < 2; i++){ pieces[i] = new PieceOfNumber((i * 5000) + 1, (i * 5000) + 5000); pieces[i].start(); } } // create int array which stores number divisors private int[] nums = new int[10000]; // onDone method is called when PieceOfNumber finishes its work // and piece that have finished is passed in private void onDone(PieceOfNumber p){ int from = p.getFrom() - 1; int to = p.getTo(); for(int i = from; i < to; i++){ // add values to final numbers array nums[i] = p.getArray()[i - from]; } } // method to ckeck if all pieces is done private void onAllDone(){ boolean allDone = true; for(PieceOfNumber piece: pieces){ if(!piece.isDone()) allDone = false; } // if all done, find an answer if(allDone) findMax(); } private void findMax(){ int max = 0; int num_with_max = -1; for(int i = 0; i < 10000; i++){ // check if array value is greater than current max if(nums[i] > max){ max = nums[i]; num_with_max = i; } } // print out answer System.out.printf("\n%d has %d divisors.", num_with_max, max); } public class PieceOfNumber extends Thread { private int from; private int to; private boolean done = false; // array to store values of numbers from to private int numbers[] = new int[5000]; // current array index private int index = 0; public PieceOfNumber(int from, int to){ super(); // set values for looping this.from = from; this.to = to; } // some getters to return needed information public int[] getArray(){ return numbers; } public int getFrom(){ return from; } public int getTo(){ return to; } public boolean isDone(){ return done; } @Override public void run(){ // try to loop through numbers try{ loop(); }catch(Exception e){ e.printStackTrace(); } } // loop method public void loop() throws Exception { // print from what number to what number looping System.out.printf("Looping from %d to %d.\n", from - 1, to); for(int pos = from; pos < to; pos++){ for(int num = 0; num < 10000; num++){ // check if number is greater than 0, because it can cause arithmetical error if(num > 0){ // check if number is divisible by n and add it to array if(divisible(pos, num)){ numbers[index]++; } } } index++; } // call onDone done = true; onDone(this); onAllDone(); // stop current thread try{ join(); }catch(InterruptedException e){ e.printStackTrace(); } } // boolean to check if number is divisible by n protected boolean divisible(int num, int n){ if(num % n == 0) return true; else return false; } } }
This code works, but the problem is that it works slower than this:
for(int x = 1; x < 10000; x++){ for(int y = 1; y < 10000; y++){ // code here } }
So I wanna know why. Thanks for good and not so good answers.