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);
                   }
            }
    }
  • 相关阅读:
    js多图上传展示和删除
    简单的下拉加载和上拉加载
    js实现放大镜效果
    js表格拖拽
    js表格上下移动添加删除
    js写的滑动解锁
    关于serialize() FormData serializeArray()表单序列化
    js日历
    js树状菜单
    Restful API官方文档
  • 原文地址:https://www.cnblogs.com/qdu-lkc/p/12193905.html
Copyright © 2011-2022 走看看