HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)

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);
//          }
//      }
    }

}
时间: 2024-08-02 08:32:08

HDOJ 1397 Goldbach&#39;s Conjecture(快速筛选素数法)的相关文章

poj 2262 Goldbach&amp;#39;s Conjecture 【素数筛】

这题必然的筛选法,出现了2个问题:1.开始开了一个 result 数组(全局变量),想把素数挨个存进来,虽然还计数估算了的,一直的runtime error , 后来发现是多此一举,去掉之后就变成wrong answer,看来可以编译了.这么说来,一百万对于两个大数组还是有点吃不消的  2.筛的时候一定要筛完整,for(j=2*i;j<MAXN;j+=i)prime[j]=1;这里开始用的 j<(MAXN/i),明显就错了. 另外我很好奇题目中说的"Goldbach's conjec

POJ 2262 Goldbach&amp;#39;s Conjecture

Problem Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: Every even number greater than 4 can be written as the sum of two odd prime numbers. For examp

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

Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as be

如何利用win7系统中搜索功能的快速筛选器删除指定大小的文件?

  电脑使用的时间长了,自然会出现各种各样的垃圾文件,因为归类不一的原因,咱们若是要手动删除这些垃圾文件的话,便需要一层一层的进入到win7 64位旗舰版iso的磁盘中,然后一个一个的找到它们并将它们删除掉,但是无疑的,这是一个十分浩大的工程,需要花费很长的时间,需要耗费很多的心力才能完成,不过win7旗舰版之后,很多朋友在搜索功能中发现了一个快速筛选器,也许咱们可以利用这个功能帮我们快速的删除掉一些文件.下面,小编就来说说具体的操作技巧吧! 1.首先,咱们返回到win7旗舰版的桌面位置,然后找

《数学与泛型编程:高效编程的奥秘》一3.2 筛选素数

3.2 筛选素数 毕达哥拉斯学派的人还观察到一个现象,那就是有些数字没有办法表示成非平凡的矩形形状(nontrivial rectangular shape),也就是说,没有办法用两个边都大于1的矩形来表示.这种数叫做素数(prime number),它们不能够用比其更小且大于1的数之间的乘积来表示:2, 3, 5, 7, 11, 13,-(古希腊人所说的数都是指整数.)某些与素数有关的特征最早是由欧几里得观察到的.尽管大家通常都会把他与几何学联系起来,但是<几何原本>中有很多卷内容其实是在讲

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

在Windows 8.1中按不同条件快速筛选应用

Win8.1的开始屏幕有一个小小的改进--在左下角增加了一个小小的箭头按钮,这个按钮可以帮我们迅速带到Win8.1系统的应用列表中,在这里我们可以看到当前安装的所有应用. 点击Win8.1开始屏幕左下方的小箭头图标,触摸屏用户向上滑动即可,切换到应用列表界面. 图示:点击箭头按钮或者触控向上滑动切换到Win8.1应用视图 注意,在"应用"二字的右边有一个排序分类的选项,点击下图中红框所示的小箭头,在下拉菜单中选择分类方式为"按安装日期".瞬间所有Win8.1系统内安

HDOJ 1163 Eddy&amp;#39;s digital Roots(九余数定理的应用)

Problem Description The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits a

HDOJ 1164 Eddy&amp;#39;s research I(拆分成素数因子)

Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to h