fork download
  1. class Main {
  2.  
  3. static final boolean isLucky_div(int n) {
  4. return ((n / 100000) + ((n / 10000) % 10) == ((n / 1000) % 10) + ((n / 100) % 10)) &&
  5. ((n / 100000) + ((n / 10000) % 10) == ((n / 10) % 10) + (n % 10));
  6. }
  7.  
  8. static final boolean isLucky_loop(int a) {
  9. int[] d = new int[6];
  10. for (int i=5; i>=0; --i) {
  11. d[i] = a % 10;
  12. a /= 10;
  13. }
  14. return d[0]+d[1] == d[2]+d[3] && d[2]+d[3] == d[4]+d[5];
  15. }
  16.  
  17. public static void main(String[] args) {
  18. int loops = 10;
  19. for (int k=0; k<2; k++) {
  20. {
  21. // div
  22. long start = System.currentTimeMillis();
  23. int count = 0;
  24. for (int j=0; j<loops; j++) {
  25. for (int i=0; i<1000000; i++) {
  26. if (isLucky_div(i))
  27. count++;
  28. }
  29. }
  30. long time = System.currentTimeMillis() - start;
  31. System.out.println("Divs: n=" + count + " t=" + time + "ms");
  32. }
  33. {
  34. // loop
  35. long start = System.currentTimeMillis();
  36. int count = 0;
  37. for (int j=0; j<loops; j++) {
  38. for (int i=0; i<1000000; i++) {
  39. if (isLucky_loop(i))
  40. count++;
  41. }
  42. }
  43. long time = System.currentTimeMillis() - start;
  44. System.out.println("Loop: n=" + count + " t=" + time + "ms");
  45. }}
  46. }
  47.  
  48. }
  49.  
Success #stdin #stdout 7.23s 380160KB
stdin
Standard input is empty
stdout
Divs: n=50500 t=1373ms
Loop: n=50500 t=2226ms
Divs: n=50500 t=1373ms
Loop: n=50500 t=2195ms