fork download
  1. class Euler1 {
  2. public long calculate(long limit, long number){
  3. if (number > limit) return 0L;
  4.  
  5. long count = limit / number;
  6. return number * (count + 1L) * count / 2L;
  7. }
  8.  
  9. public long sum(long[] data, int index, long combination, long limit, int level){
  10. long result = 0L;
  11. if (combination == 1L);
  12. else{
  13. if ((level & 1L) == 1L)
  14. result += calculate(limit, combination);
  15. else
  16. result -= calculate(limit, combination);
  17. }
  18.  
  19. if (level > data.length)
  20. return result;
  21. else{
  22. for (; index < data.length; index++)
  23. result += sum(data, index + 1, combination * data[index], limit, level + 1);
  24. }
  25.  
  26. return result;
  27. }
  28.  
  29. public static void main(String[] args) {
  30. Euler1 e = new Euler1();
  31.  
  32. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L}, 0, 1L, 999999L, 0));
  33. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L}, 0, 1L, 9999999L, 0));
  34. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L}, 0, 1L, 99999999L, 0));
  35. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L}, 0, 1L, 999999999L, 0));
  36.  
  37. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L}, 0, 1L, 999999L, 0));
  38. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L}, 0, 1L, 9999999L, 0));
  39. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L}, 0, 1L, 99999999L, 0));
  40. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L}, 0, 1L, 999999999L, 0));
  41.  
  42. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L}, 0, 1L, 999999L, 0));
  43. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L}, 0, 1L, 9999999L, 0));
  44. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L}, 0, 1L, 99999999L, 0));
  45. System.out.println(e.sum(new long[]{2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L}, 0, 1L, 999999999L, 0));
  46. }
  47. }
Success #stdin #stdout 0.05s 213760KB
stdin
Standard input is empty
stdout
404095596800
40409594590409
4040958909040980
404095905404095610
409738188044
40973752813859
4097373234996935
409737322997982171
414490217263
41448855991592
4144879962351863
414487987708284041