TriangleNo

  1. import java.util.LinkedHashMap;   
  2.   
  3. /**  
  4.  *   
  5. The sequence of triangle numbers is generated by adding the natural numbers.   
  6. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.   
  7. The first ten terms would be:  
  8.  
  9. 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...  
  10.  
  11. Let us list the factors of the first seven triangle numbers:  
  12.  
  13.  1: 1  
  14.  3: 1,3  
  15.  6: 1,2,3,6  
  16. 10: 1,2,5,10  
  17. 15: 1,3,5,15  
  18. 21: 1,3,7,21  
  19. 28: 1,2,4,7,14,28  
  20. We can see that 28 is the first triangle number to have over five divisors.  
  21.  
  22. What is the value of the first triangle number to have over five hundred divisors?   
  23.  
  24.  */  
  25. public class TriangleNo {   
  26.   
  27.     public static void main(String[] args) {   
  28.   
  29.         LinkedHashMap<Long, Long> map = null;   
  30.         Long divisors = 0L;   
  31.         Long n = 0L;   
  32.   
  33.         long begin = System.currentTimeMillis();   
  34.         System.out.println("begin=" + begin);   
  35.   
  36. //      for (n = 1L; n <= 20L; n++) {   
  37.         for (Long i = 1L; i <= 1000000L; i++) {   
  38.   
  39.             n = n + i;   
  40. //          System.out.println("--------");   
  41. //          System.out.println("i=" + i);   
  42. //          System.out.println("n=" + n);   
  43.   
  44.             map = countPrimes(n);   
  45.             divisors = 1L;   
  46.   
  47.             if(map != null) {   
  48.                 for (Long key : map.keySet()) {   
  49. //                  System.out.println(key + ":" + map.get(key));   
  50.                     divisors = divisors * (map.get(key) + 1);   
  51.                 }   
  52.             }   
  53.   
  54. //          System.out.println("divisors=" + divisors);   
  55.             if(divisors >= 500) {   
  56.                 long end = System.currentTimeMillis();   
  57.                 System.out.println("end=" + end);   
  58.                 System.out.println("time=" + (end - begin));   
  59.                    
  60.                 System.out.println("++++++++");   
  61.                 System.out.println("i=" + i);   
  62.                 System.out.println("n=" + n);   
  63.   
  64.                 if(map != null) {   
  65.                     for (Long key : map.keySet()) {   
  66.                         System.out.println(key + ":" + map.get(key));   
  67. //                      divisors = divisors * (map.get(key) + 1);   
  68.                     }   
  69.                 }   
  70.                 System.out.println("divisors=" + divisors);   
  71.                 System.out.println("++++++++");   
  72.                 break;   
  73.             }   
  74.         }   
  75.     }   
  76.   
  77.     private static LinkedHashMap<Long, LinkedHashMap<Long, Long>> cache = new LinkedHashMap<Long, LinkedHashMap<Long, Long>>();   
  78.   
  79.     private static LinkedHashMap<Long, Long> addMap(LinkedHashMap<Long, Long> map1, LinkedHashMap<Long, Long> map2) {   
  80.   
  81.         for (Long key : map2.keySet()) {   
  82.             if(map1.containsKey(key)) {   
  83.                 map1.put(key, map1.get(key) + map2.get(key));   
  84.             } else {   
  85.                 map1.put(key, map2.get(key));   
  86.             }   
  87.         }   
  88.   
  89.         return map1;   
  90.     }   
  91.   
  92.     private static LinkedHashMap<Long, Long> countPrimes(long n) {   
  93.   
  94.         LinkedHashMap<Long, Long> map = new LinkedHashMap<Long, Long>();   
  95.   
  96.         if(n <= 1) {   
  97.             return map;   
  98.         }   
  99.   
  100.         for (Long i = 2L; i <= n; i++) {   
  101.   
  102.             if (n % i == 0 && isPrime(i)) {   
  103.                 if(map.containsKey(i)) {   
  104.                     map.put(i, map.get(i) + 1);   
  105.                 } else {   
  106.                     map.put(i, 1L);   
  107.                 }   
  108.   
  109.                 if(!cache.containsKey(n / i)) {   
  110.                     cache.put(n / i, countPrimes(n / i));   
  111.                 }   
  112.                 return addMap(map, cache.get(n / i));   
  113.             }   
  114.         }   
  115.   
  116.         return map;   
  117.     }   
  118.   
  119.     private static boolean isPrime(long n) {   
  120.         if(n <= 1) {   
  121.             return false;   
  122.         }   
  123.         for (Long i = 2L; i <= n - 1; i++) {   
  124.             if (n % i == 0) {   
  125.                 return false;   
  126.             }   
  127.         }   
  128.   
  129.         return true;   
  130.     }   
  131.   
  132. }   
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓