• POJ2909_Goldbach's Conjecture(线性欧拉筛)


    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. 

    InputAn 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. 
    OutputEach 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
    题意:给你一个大于等于4小于2^15的偶数,求有多少对素数满足n=p1+p2,不应将(p1,p2)和(p2,p1)分别计算为两个不同的对

    线性欧拉筛

    代码:
    import java.util.Scanner;
    public class Main {
            static final int max=34000;
            static int prime[]=new int[max];
            static boolean is_prime[]=new boolean[max];
            static int k=0;
            public static void Prime(){
                  is_prime[0]=is_prime[1]=true;
                  for(int i=2;i<max;i++){
                        if(!is_prime[i]) prime[k++]=i;
                        for(int j=0;j<k&&prime[j]*i<max;j++){
                              is_prime[i*prime[j]]=true;
                              if(i%prime[j]==0) break;
                        }
                  }
            }
            public static void main(String[] args) {
                   Prime();
                   Scanner scan=new Scanner(System.in);
                   while(scan.hasNext()){
                         int n=scan.nextInt();
                         if(n==0) break;
                         int cnt=0;
                         for(int i=0;i<k&&prime[i]<=n/2;i++){
                                if(!is_prime[n-prime[i]]) cnt++;
                         }
                         System.out.println(cnt);
                   }
            }
    }
  • 相关阅读:
    Ubuntu中安装XAMPP出错的解决方法
    sudo 后不用输入密码的配置
    javascript鼠标双击时触发事件大全
    PHP空值判断
    40音乐海报的创意例子
    35个令人印象深刻的创意404错误页面设计
    35个高分辨率创意苹果桌面壁纸
    38惊人的HD(高清晰度)壁纸七彩的例子
    25精心设计的联系页面例子
    pgpoolII中对 setsockopt 的利用
  • 原文地址:https://www.cnblogs.com/qdu-lkc/p/12193905.html
Copyright © 2020-2023  润新知