HDOJ1397 Goldbach's Conjecture(哥德巴赫猜想)
来源:互联网 发布:abb编程培训 编辑:程序博客网 时间:2024/06/06 08:56
Goldbach's Conjecture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7163 Accepted Submission(s): 2784
Problem Description
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
Input
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
Output
Each output line should contain an integer number. No other characters should appear in the output.
Sample Input
610120
Sample Output
121
使用素数筛选法打表,因为p1+p2=n,所以只需要循环一次。
判断p1是不是素数,判断n-p1是不是素数(注意范围)
import java.util.Scanner;public class Main{private static Scanner scanner;private static boolean isPrime[];public static void main(String[] args) {scanner = new Scanner(System.in);dabiao();while(scanner.hasNext()){int n = scanner.nextInt();if(n==0){break;}int count= 0;for (int i = 2; i <= n/2; i++) {if(isPrime[i] && isPrime[n-i]){// && n <= n-i){count++;}}System.out.println(count);}}private static void dabiao() {// Math.pow(2, 15)=32768isPrime = new boolean[32768];for (int i = 2; i < isPrime.length; i++) {isPrime[i] = true;}for (int i = 2; i < isPrime.length; i++) {if(isPrime[i]){for (int j = i+i; j < isPrime.length; j+=i) {isPrime[j] = false;}}}}}
阅读全文
0 0
- HDOJ1397 Goldbach's Conjecture(哥德巴赫猜想)
- Lightoj1259——Goldbach`s Conjecture(哥德巴赫猜想)
- HDU - 1397 Goldbach's Conjecture(哥德巴赫猜想)
- POJ 2262 Goldbach's Conjecture 哥德巴赫猜想
- POJ 2262 Goldbach's Conjecture哥德巴赫猜想
- POJ 2262 Goldbach's Conjecture(哥德巴赫猜想)
- 【哥德巴赫猜想】LightOJ Goldbach`s Conjecture 1259
- 【哥德巴赫猜想】POJ Goldbach's Conjecture 2262
- NUC1090 Goldbach's Conjecture【哥德巴赫猜想 】
- POJ2262,Goldbach's Conjecture,哥德巴赫猜想水过
- UVa 543 Goldbach's Conjecture (素数&哥德巴赫猜想)
- UVa 686 Goldbach's Conjecture (II) (哥德巴赫猜想)
- LightOJ 1259 Goldbach`s Conjecture(证明哥德巴赫猜想)
- poj 2262 Goldbach's Conjecture 筛法 哥德巴赫猜想
- 7-3 Goldbach's Conjecture (哥德巴赫猜想)(30 分)
- POJ-2262 Goldbach's Conjecture 解题报告(数论) 哥德巴赫的猜想
- poj_2262_Goldbach's Conjecture(哥德巴赫猜想)
- C语言:验证哥德巴赫猜想(Goldbach conjecture verification )
- ubuntu16.04环境用pip3安装dlib
- struts2-hibernate 总结
- 171208之Oracle总结(二)
- 省选专练[HNOI2010]BOUNCE 弹飞绵羊
- Hello Createjs
- HDOJ1397 Goldbach's Conjecture(哥德巴赫猜想)
- Ruby元编程
- Web前端开发精品课HTML CSS JavaScript基础教程第二十三章课后编程题答案
- 凯哥自媒体赚钱秘籍:百家号怎么快速做爆文?
- 根据省份证算年龄
- java.net.URISyntaxException: Illegal character in scheme name at index 0:
- 第十周项目1
- LeetCode 406. Queue Reconstruction by Height
- vue实战--SPA的实现思路