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); } } }
POJ2909_Goldbach's Conjecture(线性欧拉筛)
原文:https://www.cnblogs.com/qdu-lkc/p/12193905.html