Problem12_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Problem12

Problem12

 2011/11/8 7:52:45  水木清华77  http://1031709750-qq-com.iteye.com  我要评论(0)
  • 摘要:packagecom.shui.mu.yao.io.algorithm;importjava.util.ArrayList;importjava.util.List;publicclassProblem12{publicstaticvoidmain(String[]args){longstart=System.currentTimeMillis();intx=500;intcount=1;while((x/=2)!=0){count++;}List<Integer>
  • 标签:
package com.shui.mu.yao.io.algorithm;

import java.util.ArrayList;
import java.util.List;

public class Problem12 {

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		int x = 500;
		int count = 1;
		while ((x /= 2) != 0) {
			count++;
		}
		List<Integer> seed = new ArrayList<Integer>();
		seed.add(2);
		int factor = 2;
		while (seed.size() < 500) {
			boolean isPrime = true;
			for (int s : seed) {
				if (factor < s)
					break;
				if (factor % s == 0)
					isPrime = false;
			}
			if (isPrime)
				seed.add(factor);
			factor++;
		}

		int N = (int) Math.pow(2, count);
		int sum = 0;
		List<Node> list = new ArrayList<Node>();
		while (true) {
			N++;
			sum = (1 + N) / 2 * N;
			list.clear();
			for (int s : seed) {
				if (sum < s)
					break;
				int count_ = 0;
				int temp = sum;
				while (temp % s == 0) {
					count_++;
					temp = temp / s;
				}
				if (count_ != 0) {
					list.add(new Node(s, count_));
				}

			}
			int result = 1;
			for (Node node : list) {
				result = result * (node.getY() + 1);
			}
			if (result > 500) {
				int re=1;
				for (Node node : list) {
					System.out.println("factor:" + node.getX() + "\t"
							+ "count:" + node.getX());
					re*=Math.pow(node.getX(), node.getY());
				}
				if(re==sum)
					System.out.println("this is a true result");
				System.out.println("sum:" + sum);
				System.out.println("N:" + N);
				break;
			}

		}

		long end = System.currentTimeMillis();
		System.out.println("time:" + (end - start) + "ms");

	}

}

class Node {
	private int x;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	private int y;

	Node(int x, int y) {
		this.x = x;
		this.y = y;
	}
}
  • 相关文章
发表评论
用户名: 匿名