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

Problem 27

 
阅读更多

问题描述:

Euler published the remarkable quadratic formula:

n² + n + 41

It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41² + 41 + 41 is clearly divisible by 41.

Using computers, the incredible formula  n² − 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

 

解决问题:

 利用题目本身的限制条件,因为a和b是1000以下,那么先使用boolean[] prime = new boolean[1000001];

 

 

public static boolean IsPrime(int number){
		boolean result = true;
		
		if(number%2==0){
			result = false;
		}else{
			int middle = (int)Math.sqrt(number);
			for(int i=3; i<=middle; i+=2){
				if(number%i==0){
					result = false;
					break;
				}
			}
		}
		
		return result;
	}
	
	public static void find(){
		int a =0, b=0;
		int max_len = 0;
		//先找出1000以内的所有素数
		boolean[] prime = new boolean[1000001];
		Arrays.fill(prime, false);
		for(int i=2;i<=1000001;i++){
			if(IsPrime(i)){
				prime[i] = true;
			}
		}
		
		for(int i=-1000; i<=1000; i++){ //循环a
			for(int j=-1000; j<=1000; j++){ //循环b
				int n;
				for( n=0; n<Math.abs(j); n++){
					int value = n*n+ i*n + j;
					if(value>0&&prime[value]){
						;
					}else{
						break;
					}
				}
				if(max_len<n){
					a = i;
					b = j;
					max_len = n;
				}
			}
		}
		System.out.println("a:"+a+",b:"+b+",len"+max_len);
		System.out.println(a*b);
	}

 

分享到:
评论

相关推荐

    计算机网络第六版答案

    27. Creation of a botnet requires an attacker to find vulnerability in some application or system (e.g. exploiting the buffer overflow vulnerability that might exist in an application). After finding ...

    The Shellcoder's Handbook 2nd Edition

    The Shellcoder's Handbook 2nd Edition About the Authors vii Acknowledgments xi ...The Address Problem 27 The NOP Method 33 Defeating a Non-Executable Stack 35 Return to libc 35 Conclusion 39

    数值分析 第四版答案

    ### 数值分析第四版答案解析 #### 一、概述 《数值分析》是一门重要的计算机科学与数学交叉学科,主要研究如何使用计算机有效地解决数学问题。本书为《数值分析》第四版的答案集,包含了该教科书的主要内容概要及...

    android行业分析

    4.1. G1 还是G2 it's a problem 27 4.2. G1和G2的硬件对比: 27 4.3. 编者注: 29 5. 移动应用程序 29 5.1. 大度咨询发布手机软件发展报告 30 5.2. Apple AppStore 用户喜好精细化分析 58 5.3. android应用分类: 67...

    八年级下英语词汇、短语、重点句自我测试.doc

    26. 问题,苦恼 - problem 27. 〔用手或器具〕击;打 - hit 28. 立即;马上 - immediately 29. 陷入;参与 - get involved 30. 她自己 - herself 31. 绷带 - bandage (n/v) 32. 生病的;有病的 - ill 33. 膝;膝盖 -...

    Artificial Intelligence and Problem Solving

    --- Problem Solving & Traditional AI Problems......Page 27 POLyA’s five sTePs fOr PrOBLem sOLvinG......Page 28 PrOBLem-sOLvinG Techniques......Page 33 humAn WinDOW......Page 44 humAn WinDOW criTeriA ...

    Problem Solving with C++ 7th edition

    编译和运行C++程序是学习C++语言的基础,本教材在第27页提供了编译和运行的详细步骤,为初学者打下良好的实践基础。算术运算符及其优先级是编写有效算法的核心,第71页对此进行了介绍,而第81页则讨论了在使用赋值...

    0-1-knapsack-problem-master (27).zip

    在这个名为“0-1-knapsack-problem-master (27).zip”的压缩包中,我们可以推测包含的是关于0-1背包问题的C语言实现代码。C语言是一种底层、高效的编程语言,常用于解决这种算法问题。 0-1背包问题的基本定义如下:...

    0-1-knapsack-problem-master (27)c.zip

    在这个名为"0-1-knapsack-problem-master (27)c.zip"的压缩包中,我们预计会找到一个C语言实现的0-1背包问题解决方案。C语言是一种底层、高效且灵活的编程语言,非常适合解决这类计算密集型的问题。 在C语言中解决0...

    topoplogy-math-problem

    通过逻辑推理和组合数学的方法,我们可以发现这四个砝码的重量分别为1磅、3磅、9磅和27磅。这是因为,使用这四个砝码可以称量从1到40磅的所有整数重量。例如,要称量1磅的物品,只需使用1磅的砝码;如果要称量2磅,...

    动态规划Hamming_Problem 解题报告

    例如,给定质数2, 3, 和 5,Hamming序列H(2, 3, 5)是2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, ...。题目要求我们找到H(p1, p2, p3)序列中的第i个元素,其中i是一个给定的正整数。 输入数据包括三...

    xml解析器失败方法

    xml解析器失败时候可以尝试打开开始-运行-cmd 输入命令来进行解决

    0-1-knapsack-problem-master (28)c.zip

    在"0-1-knapsack-problem-master (27)c.zip"这个压缩包中,很可能包含了C语言实现0-1背包问题的源代码示例。通过阅读和理解这些代码,你可以深入理解动态规划方法以及如何将其应用于解决实际问题。同时,这也可以...

    redhat-lsb-4.1-27.el7.centos.1.x86_64.rpm

    离线安装包,亲测可用

    Delphi VCLSkin v3.81 官方试用版

    sfOnlyThisForm.News in 3.70 10/27/2005*Support TAdvPageControl.*Support TRzMenuButton, TcxButton with cxbkDropDownButton.*work with RECREATEWND message for skined control has scrollbar.*Fix a memory ...

    redhat-lsb-core-4.1-27.el7.centos.1.x86_64.rpm

    离线安装包,亲测可用

    delphi2010皮肤控件-VCLSkinv5.30FS

    Delphi VCLSkin 5.30 ... VCLSkin is a component to create skinnable user interface for Delphi/C++Builder application, It is easy to use, just put one component on mainform, ...* fix caption paint problem...

    md5\82477210MD5_FastCollision_v1.0.0.5_src.zip

    MD5_STEP(HH, c, d, a, b, block[15], 0x1fa27cf8, 16); MD5_STEP(HH, b, c, d, a, block[2], 0xc4ac5665, 23); MD5_STEP(II, a, b, c, d, block[0], 0xf4292244, 6); MD5_STEP(II, d, a, b, c, block[7], 0x432...

    XLSReadWriteII-v5.20.27-xe4.zip_XLSReadWriteII_names_xe 27.com

    5.20.27 xe4 ! Defined names with non-ansi chars could cause problems. ! Wrong UTF-8 encoding on some pivot tables. ! When inserting rows, not all formulas where adjusted. ! Problem with hyperlinks to ...

    ACM学习资料汇总,ACMer要试试哦!

    - [题目27](http://acm.pku.edu.cn/JudgeOnline/problem?id=1190) —— 深度优先搜索剪枝。 - [题目28](http://acm.pku.edu.cn/JudgeOnline/problem?id=1084) - [题目29]...

Global site tag (gtag.js) - Google Analytics