`
to_zoe_yang
  • 浏览: 144432 次
  • 性别: Icon_minigender_2
  • 来自: 01
社区版块
存档分类
最新评论

Problem 50

 
阅读更多

问题描述:

 

The prime 41, can be written as the sum of six consecutive primes:

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

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?


解决问题:

 

 

package projecteuler;

import java.util.Arrays;

public class Problem50 {

	public static boolean[] prime = new boolean[1000000];

	public static boolean IsPrime(long number) {

		for (int i = 2; i * i <= number; i++) {
			if (number % i == 0)
				return false;
		}
		return true;
	}

	public static void main(String[] args) {
		Arrays.fill(prime, false);
		int[] elements = new int[1000000];
		Arrays.fill(elements, 0);
		int index = 0;
		for (int i = 2; i < 1000000; i++) {
			if (IsPrime(i)) {
				prime[i] = true;
				elements[index] = i;
				index++;
			}
		}

		int len = 0;
		int sum = 0;
		int max = 0;
		for (int i = 0; i < index; i++) {
			sum = 0;
			for(int j =i; j<index; j++){
				sum += elements[j];
				if (sum > 1000000) {
					break;
				}
				if (sum<1000000&&prime[sum]) {
					if (len < j-i) {
						len = j-i;
						max = sum;
//						System.out.println("I:"+i+",J:"+j+",Sum:"+sum);
					}
				}
			}
		}
		System.out.println("Max:"+max);
		
	}

}

 

分享到:
评论

相关推荐

    The C10K problem

    例如,一台配备1000MHz处理器、2GB RAM及1000Mbit/s以太网卡的机器,理论上足以支持每秒为20000个客户端提供服务,每个客户端的平均需求为50KHz、100KB数据量和50Kbits/s的带宽。这表明,在当前的硬件配置下,实现...

    Artificial Intelligence and Problem Solving

    cLAssificATiOn......Page 50 Refs......Page 52 3 Missionaries & Cannibals......Page 53 chOOsinG An APPrOPriATe rePresenTATiOn......Page 55 sOLuTiOn......Page 58 humAn PrOBLem sOLvinG......Page 60 humAn...

    JAVA-50-problem.zip_site:www.pudn.com

    "JAVA-50-problem.zip" 这个压缩包文件提供了50道Java编程题目,旨在帮助初学者加深对Java语言的理解,增强编程实践能力。文件来源于www.pudn.com,这是一个提供各种技术资源的网站,对于学习者和开发者来说是个宝贵...

    Hackers Get to the Root of the Problem

    标题与描述:“Hackers Get to the Root of the Problem”(黑客深入问题核心) 该标题与描述揭示了黑客攻击中的一个核心问题:通过获取系统最高权限(root)来控制计算机或网络,这一行为对用户构成了巨大威胁。...

    Problem Solving with Algorithms and Data Structures 黄哥Python培训推荐材.pdf - 快捷方式

    6. 算法和数据结构的实际应用:内容中提到了“50个练习”,这很可能是指通过实际编码练习来加强对算法和数据结构的理解和应用。 7. 社群交流:内容还提到了通过QQ群(qq:***)进行学习交流。QQ群是中文互联网环境下...

    50-Java-programming-problem.rar_java programming_site:www.pudn.c

    这个名为"50-Java-programming-problem.rar"的压缩文件包含了一份精心挑选的Java编程题集合,旨在帮助初学者提升技能,同时融入了一些有趣的数学知识。这些题目通常涵盖基础语法、数据类型、控制结构、函数、类与...

    APIO2015 Problemset

    ### APIO2015 Problemset - Bali Sculptures #### 问题背景 在亚洲太平洋信息学奥林匹克(Asia-Pacific Informatics Olympiad,简称APIO)2015年的竞赛中,有一道名为“Bali Sculptures”的题目。这道题目的背景...

    如何在c#中使用mapx50

    MapX 50 是一款由 Autodesk 公司开发的地图组件,它允许开发者在应用程序中集成地图功能,包括显示地图、地图操作、地理编码、路线规划等。在C#中使用MapX 50,需要安装相应的.NET版本的MapX库,并通过引用该库来...

    爱立信CSR数据采集规范

    20.3 APZ 212 50 RESTART/RELOAD/CP FAULT 16 21. APG40 related problem 28 21.1 These data must be provided for every trouble report. 28 21.2 STS related problem 28 21.3 Timezone problems 28 21.4 Audit ...

    The Hungarian Method for The Assignment Problem

    著名的匈牙利算法介绍,Hungarian algorithm,用于解决矩形分配问题。该文档是Springer出版图书“50 Years of Integer Programming 1958-2008”的第二章节。

    computer network 5th andre problem solutions

    即使在网络路径中有50个开关,额外的总距离仅为100公里,这相对于5000公里的总距离来说几乎可以忽略不计。 ### 6. 信号传播延迟 第六个问题涉及信号的往返传播延迟。这里给出了一个具体示例,请求和响应的总传播...

    submit50:这是submit50,CS50的提交问题的命令行工具

    用法英语 submit50 problem西班牙语 LANGUAGE=es submit50 problem国际化添加新语言首先确保安装了babel , submit50在开发模式下安装了submit50 : pip install babel pip install -e .生成翻译模板: python setup...

    ACM必做50题的解题-数学

    练习c语言基础,要想编程功底扎实,这些题目必须的要会做才行!

    monty_hall_problem:Python中的Monty Hall问题

    可以选择是否与其他剩余的关门一起切换门参赛者做出选择,他们的最终选择揭晓理论: 在游戏秀的阶段,主持人打开了一扇露出山羊的门,可以认为,如果参赛者赢得奖金,则有50/50的机会。 但是,这是不正确的。 更仔细...

    Behzad Razavi_Errata in Problem Sets.pdf

    - **问题7.2**:第二句话应更改为:“假设\(W/L_1 = 50/0.5\),\(I_{D1} = I_{D2} = 0.1mA\)。” - **问题7.20**:将\(I_{D1}\)和\(I_{D2}\)更改为0.05mA。 - **问题7.24**:将偏置电流更改为0.1mA。 ##### 第8章 -...

    Problem C.链表操作(2019网研院)

    =50)个节点的顺序链表,有以下3种操作方式: 1 移除节点值为x的节点 2 翻转链表,对调整个链表的顺序 3 查询链表值为x的节点所指的下一个节点的值 输入: 第一行输入T,表示数据的组数 每组第一行输入 n (表示节点...

    Predict whether income exceeds $50K/yr based on census data. Als

    Machine Learning, Classification problem. https://archive.ics.uci.edu/ml/datasets/Adult Abstract: Predict whether income exceeds $50K/yr based on census data. Also known as "Census Income" dataset. ...

    express-api-problem:Express包可自动将您的异常转为API Problem JSON响应

    它提供了的直接实现,并将您引发的异常以以下格式返回,且内容类型的标头设置为application/problem+json { " status " : 403 , " type " : " http://example.com/problems/out-of-credit " , " title " : " ...

Global site tag (gtag.js) - Google Analytics