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.

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

6

10

12

0

Sample Output

1

2

1

题意:

哥德巴赫猜想:任何偶数n大于或等于4,至少存在一对素数P1和P2,n=p1+p2。p1+p2和p2+p1是一样的。

本题是统计有多少对不同的素数和等于n.

注意用到素数筛选,然后打表就可以得出答案了。

import java.util.Scanner;

public class Main{
static int[] db = new int[65536];
public static void main(String[] args) {
dabiao();
//System.out.println(Math.pow(2, 15));-32768
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
if(n==0){
return ;
}
int tp = 0;
for(int i=1;i<=n/2;i++){ if(db[i]==1&&db[n-i]==1){
tp++;
}
}
System.out.println(tp); } } //快速筛选素数
private static void dabiao() {
for(int i=0;i<db.length;i++){
db[i]=1;
}
db[0]=0;
db[1]=0;
for(int i=2;i<=Math.sqrt(db.length);i++){
for(int j=i+i;j<db.length;j=j+i){
if(db[i]==1){
db[j]=0;
}
}
} // for(int i=0;i<1000;i++){
// if(db[i]==1){
// System.out.println(i);
// }
// }
} }

HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)的更多相关文章

  1. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  2. HDU 1397 Goldbach's Conjecture(二分,查找素数)

    题目意思很简单,就是找n=p1+p2的种类数,具体看题目吧. 次重点是查找一定范围内的素数: 重点是用二分查找,不然会超时!!! #include<stdio.h> #include< ...

  3. HDU 1397 Goldbach's Conjecture【素数打表】

    题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 #include<iostream> #include<cstdio> #include<c ...

  4. 埃氏筛法(快速筛选n以内素数的个数)

    给你一个数n,请问n以内有多少个素数?(n <= 10e7) 一般来说,要是对一个整数进行素数判断,首先想到的是写个函数判断是否为素数,然后调用这个函数,时间复杂度为O(n^(½)),但是要求n ...

  5. Light oj-1259 - Goldbach`s Conjecture

                                                                                    1259 - Goldbach`s Co ...

  6. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  7. HDOJ/HDU 2710 Max Factor(素数快速筛选~)

    Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...

  8. 【NYOJ-187】快速查找素数—— 枚举法、筛选法、打表法

    快速查找素数 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 现在给你一个正整数N,要你快速的找出在2.....N这些数里面所有的素数. 输入 给出一个正整数数N(N ...

  9. F - Goldbach`s Conjecture 对一个大于2的偶数n,找有多少种方法使两个素数的和为n;保证素数a<=b; a+b==n; a,b都为素数。

    /** 题目:F - Goldbach`s Conjecture 链接:https://vjudge.net/contest/154246#problem/F 题意:对一个大于2的偶数n,找有多少种方 ...

随机推荐

  1. Android中自定义Activity和Dialog的位置大小背景和透明度等

    1.自定义Activity显示样式 先在res/values下建colors.xml文件,写入: view plainprint? 1. <?xml version="1.0" ...

  2. db2 存储过程迁移方法

    大家在迁移数据库时,存储过程一般也要迁移过去,但一般有两个问题: 1. 非常多存储过程有先后关系(存储过程调用存储过程),假设存储过程数量少,还能手动操作.假设量大,那真是要疯了. 2. 存储过程过大 ...

  3. Opencv学习笔记(六)SURF学习笔记

    原创文章,转载请注明出处:http://blog.csdn.net/crzy_sparrow/article/details/7392345 本人挺菜的,肯定有非常多错误纰漏之处 ,希望大家不吝指正. ...

  4. Php面向对象 – 单例模式

    Php面向对象 – 单例模式 保证类仅仅有一个实例 1.    怎样能够解决一个类能够被无限地实例化? New,就能实例化一次,怎么去限制,用户不能无限次地new? 将构造方法私有化.全部外部的new ...

  5. Robotium API -- 除click/clickLong外的其他操作

    拖动操作 void drag (float fromX, float toX, float fromY, float toY, int stepCount) 选定两个位置,进行拖动操作(这里的拖动操作 ...

  6. XTU1199:Number Game

    题目描写叙述 给你一个有N个数的集合S和一个数X,推断是否存在S的一个子集,子集里的数的最小公倍数正好是X. 输入 第一行是数据组数T. 接下来有多组数据,每组数据包括两行: 第一行有2个数N和X,1 ...

  7. iOS应用内语言切换功能

    当我们的应用仅仅面向国内用户群,一般仅支持一种语言--中文就可以了.当面向国外用户时就需要进行国际化了,不仅仅是语言的转变,也可能包括设计风格,页面布局.交互效果的转变,如微信,微博,QQ这类应用都有 ...

  8. (转) [教程] Unity3D中角色的动画脚本的编写(一)

    ps: 这两天研究unity3d,对动画处理特别迷糊,不知FBX导入以后,接下来应该怎么操作,看到这篇文章,感觉非常好,讲解的很详细. 已有好些天没写什么了,今天想起来该写点东西了.这次我所介绍的内容 ...

  9. 使用Intent实现Activity的隐式跳转

    相比于显式Intent,隐式Intent 则含蓄了许多,它并不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的action 和category 等信息,然后交由系统去分析这个Intent,并 ...

  10. 字典与集合(Dictionary与Collection)

    Dictionary对象将替换Collection对象,并提供附加的语言从而使增加和删除记录的速度比以前提高三倍 虽然Visual Basic 6.0只有很少的新特点,但是具有某些功能强大的新的对象模 ...