Problem12

简介:

1.package com.shui.mu.yao.io.algorithm;  
2.  
3.import java.util.ArrayList;  
4.import java.util.List;  
5.  
6.public class Problem12 {  
7.  
8.    public static void main(String[] args) {  
9.        long start = System.currentTimeMillis();  
10.        int x = 500;  
11.        int count = 1;  
12.        while ((x /= 2) != 0) {  
13.            count++;  
14.        }  
15.        List<Integer> seed = new ArrayList<Integer>();  
16.        seed.add(2);  
17.        int factor = 2;  
18.        while (seed.size() < 500) {  
19.            boolean isPrime = true;  
20.            for (int s : seed) {  
21.                if (factor < s)  
22.                    break;  
23.                if (factor % s == 0)  
24.                    isPrime = false;  
25.            }  
26.            if (isPrime)  
27.                seed.add(factor);  
28.            factor++;  
29.        }  
30.  
31.        int N = (int) Math.pow(2, count);  
32.        int sum = 0;  
33.        List<Node> list = new ArrayList<Node>();  
34.        while (true) {  
35.            N++;  
36.            sum = (1 + N) / 2 * N;  
37.            list.clear();  
38.            for (int s : seed) {  
39.                if (sum < s)  
40.                    break;  
41.                int count_ = 0;  
42.                int temp = sum;  
43.                while (temp % s == 0) {  
44.                    count_++;  
45.                    temp = temp / s;  
46.                }  
47.                if (count_ != 0) {  
48.                    list.add(new Node(s, count_));  
49.                }  
50.  
51.            }  
52.            int result = 1;  
53.            for (Node node : list) {  
54.                result = result * (node.getY() + 1);  
55.            }  
56.            if (result > 500) {  
57.                int re=1;  
58.                for (Node node : list) {  
59.                    System.out.println("factor:" + node.getX() + "\t"  
60.                            + "count:" + node.getX());  
61.                    re*=Math.pow(node.getX(), node.getY());  
62.                }  
63.                if(re==sum)  
64.                    System.out.println("this is a true result");  
65.                System.out.println("sum:" + sum);  
66.                System.out.println("N:" + N);  
67.                break;  
68.            }  
69.  
70.        }  
71.  
72.        long end = System.currentTimeMillis();  
73.        System.out.println("time:" + (end - start) + "ms");  
74.  
75.    }  
76.  
77.}  
78.  
79.class Node {  
80.    private int x;  
81.  
82.    public int getX() {  
83.        return x;  
84.    }  
85.  
86.    public void setX(int x) {  
87.        this.x = x;  
88.    }  
89.  
90.    public int getY() {  
91.        return y;  
92.    }  
93.  
94.    public void setY(int y) {  
95.        this.y = y;  
96.    }  
97.  
98.    private int y;  
99.  
100.    Node(int x, int y) {  
101.        this.x = x;  
102.        this.y = y;  
103.    }  
104.}  

相关文章
|
7月前
|
数据挖掘
|
Go C++
P1001 A+B Problem
P1001 A+B Problem
118 0
|
存储 索引