<MEMORY>Project Euler NO50

来源:互联网 发布:怎样安装广联达软件 编辑:程序博客网 时间:2024/06/07 03:53

41这个质数,可以写作6个连续质数之和:

41 = 2 + 3 + 5 + 7 + 11 + 13

这是100以下的最长的和为质数的连续质数序列。

1000以下最长的和为质数的连续质数序列包含21个项,和为953.

找出100万以下的最长的何为质数的连续质数序列之和。

100万以下的哪个质数能够写成最长的连续质数序列?


public class Problem50{public static void main(String[] args){long start = System.currentTimeMillis();System.out.print("answer:  ");howmany();long end = System.currentTimeMillis();System.out.print("time:  ");System.out.println(end - start);}static void howmany(){boolean array[] = new boolean[1000000];for (int i = 2; i < 1000000; i++){array[i] = iszhishu(i);}int answer = 0;int maxlen = 0;for (int i = 2; i < 1000000; i++){int len = 0;int t = 0;if (array[i]){for (int j = i; j < 1000000; j++){if (array[j]){len++;t += j;if (t >= 1000000){break;}if (array[t] && len > maxlen){maxlen = len;answer = t;}}}}}System.out.println(answer);}static boolean iszhishu(int n){for (int i = 2; i <= Math.sqrt(n); i++){if (n % i == 0){return false;}}return true;}}




answer:  997651
time:  502



0 0