I'm trying to work through problem 251 on Project Euler. Problem 251 - Project Euler
I've got a working algorithm with a few shortcuts while maintaining 100% accuracy. However, it isn't fast enough finding all triplets such that a+b+c <= 100,000 in 35.557 seconds. If I want to find all triplets such that a+b+c<= 110,000,000, then my algorithm has to much faster.
long start = System.currentTimeMillis(); long end = System.currentTimeMillis(); int tot = 0; int lim = 100000; for(long a = 2; a <= lim; a+=3) { for(long b = 1; a+b <= lim; b++) { if(b!=a) { double c = (Math.pow(a+1,2)*(8*a-1))/(27.0*Math.pow(b, 2)); if(Algorithms.isInt(c)&&a+b+c<=lim) { tot++; //end = System.currentTimeMillis(); //System.out.println("a = " + a + " ; b = " + b + " ; c = " + c + " ; a + b + c = " + (a+b+c) + " ; tot = " + tot + " ; Time = " + ((double)(end-start)/1000.0) + " sec."); } } } if(a%1000==0) { end = System.currentTimeMillis(); System.out.println("a = " + a + " ; tot = " + tot + " ; Time = " + ((double)(end-start)/1000.0) + " sec."); } } System.out.println(tot); end = System.currentTimeMillis(); System.out.println("Elapsed time: " + (double)(end-start)/1000.0 + " sec.");
Algorithms.isInt()
public static boolean isInt(double val) { return ((int)val==val); }
The output when run right now:
a = 2000 ; tot = 2135 ; Time = 1.388 sec. a = 5000 ; tot = 4514 ; Time = 3.455 sec. a = 8000 ; tot = 6616 ; Time = 5.446 sec. a = 11000 ; tot = 8262 ; Time = 7.376 sec. a = 14000 ; tot = 9756 ; Time = 9.244 sec. a = 17000 ; tot = 11268 ; Time = 11.043 sec. a = 20000 ; tot = 12699 ; Time = 12.803 sec. a = 23000 ; tot = 14110 ; Time = 14.473 sec. a = 26000 ; tot = 15187 ; Time = 16.084 sec. a = 29000 ; tot = 15587 ; Time = 17.63 sec. a = 32000 ; tot = 15943 ; Time = 19.108 sec. a = 35000 ; tot = 16277 ; Time = 20.523 sec. a = 38000 ; tot = 16572 ; Time = 21.875 sec. a = 41000 ; tot = 16767 ; Time = 23.164 sec. a = 44000 ; tot = 16914 ; Time = 24.391 sec. a = 47000 ; tot = 16916 ; Time = 25.555 sec. a = 50000 ; tot = 16916 ; Time = 26.653 sec. a = 53000 ; tot = 16916 ; Time = 27.684 sec. a = 56000 ; tot = 16916 ; Time = 28.658 sec. a = 59000 ; tot = 16916 ; Time = 29.568 sec. a = 62000 ; tot = 16916 ; Time = 30.412 sec. a = 65000 ; tot = 16916 ; Time = 31.189 sec. a = 68000 ; tot = 16916 ; Time = 31.904 sec. a = 71000 ; tot = 16916 ; Time = 32.553 sec. a = 74000 ; tot = 16916 ; Time = 33.14 sec. a = 77000 ; tot = 16916 ; Time = 33.663 sec. a = 80000 ; tot = 16916 ; Time = 34.121 sec. a = 83000 ; tot = 16916 ; Time = 34.52 sec. a = 86000 ; tot = 16916 ; Time = 34.852 sec. a = 89000 ; tot = 16916 ; Time = 35.12 sec. a = 92000 ; tot = 16916 ; Time = 35.324 sec. a = 95000 ; tot = 16916 ; Time = 35.465 sec. a = 98000 ; tot = 16916 ; Time = 35.542 sec. 16916 Elapsed time: 35.557 sec.
Would threading help any?