Notice: Undefined index: HTTP_ACCEPT_ENCODING in C:\server\web\www.showyounger.com\s-system.php on line 2
TriangleNo | 小样儿(ShowYounger)
小样儿,顶呱呱!!![http://www.showyounger.com]
- import java.util.LinkedHashMap;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class TriangleNo {
-
- public static void main(String[] args) {
-
- LinkedHashMap<Long, Long> map = null;
- Long divisors = 0L;
- Long n = 0L;
-
- long begin = System.currentTimeMillis();
- System.out.println("begin=" + begin);
-
-
- for (Long i = 1L; i <= 1000000L; i++) {
-
- n = n + i;
-
-
-
-
- map = countPrimes(n);
- divisors = 1L;
-
- if(map != null) {
- for (Long key : map.keySet()) {
-
- divisors = divisors * (map.get(key) + 1);
- }
- }
-
-
- if(divisors >= 500) {
- long end = System.currentTimeMillis();
- System.out.println("end=" + end);
- System.out.println("time=" + (end - begin));
-
- System.out.println("++++++++");
- System.out.println("i=" + i);
- System.out.println("n=" + n);
-
- if(map != null) {
- for (Long key : map.keySet()) {
- System.out.println(key + ":" + map.get(key));
-
- }
- }
- System.out.println("divisors=" + divisors);
- System.out.println("++++++++");
- break;
- }
- }
- }
-
- private static LinkedHashMap<Long, LinkedHashMap<Long, Long>> cache = new LinkedHashMap<Long, LinkedHashMap<Long, Long>>();
-
- private static LinkedHashMap<Long, Long> addMap(LinkedHashMap<Long, Long> map1, LinkedHashMap<Long, Long> map2) {
-
- for (Long key : map2.keySet()) {
- if(map1.containsKey(key)) {
- map1.put(key, map1.get(key) + map2.get(key));
- } else {
- map1.put(key, map2.get(key));
- }
- }
-
- return map1;
- }
-
- private static LinkedHashMap<Long, Long> countPrimes(long n) {
-
- LinkedHashMap<Long, Long> map = new LinkedHashMap<Long, Long>();
-
- if(n <= 1) {
- return map;
- }
-
- for (Long i = 2L; i <= n; i++) {
-
- if (n % i == 0 && isPrime(i)) {
- if(map.containsKey(i)) {
- map.put(i, map.get(i) + 1);
- } else {
- map.put(i, 1L);
- }
-
- if(!cache.containsKey(n / i)) {
- cache.put(n / i, countPrimes(n / i));
- }
- return addMap(map, cache.get(n / i));
- }
- }
-
- return map;
- }
-
- private static boolean isPrime(long n) {
- if(n <= 1) {
- return false;
- }
- for (Long i = 2L; i <= n - 1; i++) {
- if (n % i == 0) {
- return false;
- }
- }
-
- return true;
- }
-
- }