zoukankan      html  css  js  c++  java
  • 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);
                   }
            }
    }
  • 相关阅读:
    python新建以时间命名的目录
    selenium跳过https的问题
    selenium修改控件属性
    selenium遍历控件集合
    知识库系统confluence5.8.10 安装与破解
    python3 遍历文件
    mysql更新密码为空
    CentOS7下安装配置vncserver
    Centos7搭建php+mysql环境(整理篇)
    centos7上安装与配置Tomcat7(整理篇)
  • 原文地址:https://www.cnblogs.com/qdu-lkc/p/12193905.html
Copyright © 2011-2022 走看看