首页 > 试题广场 >

素数对

[编程题]素数对
  • 热度指数:28867 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000。
如,输入为10, 程序应该输出结果为2。(共有两对质数的和为10,分别为(5,5),(3,7))

输入描述:
输入包括一个整数n,(3 ≤ n < 1000)


输出描述:
输出对数
示例1

输入

10

输出

2

根据定义,质数是大于1的数字,同时这个数字只有1和其本身两个约数,除此之外再无约数。要统计和为输入值num的质数对个数,要做两件事。其一:遍历[2, num/2]区间内的所有数字i,因为这些数字是和为num的数字对中的其中,而num - i则为每对中的另一个;因为当i>num/2时,再出现的数字对均已出现过,因此遍历边界取到num/2即可。其二:探测每对数字对是否均为质数,如果是,则执行加一操作更新计数器count;否则不予更新。在判定数字j是否质数时,依然要遍历[2, Math.sqrt(j)]区间的数字k是否可以整除数字j,如果可以整除,即除了1和数字j之外还有其他数字,因此j就不是质数了;如果在此区间内,未出现k可以整除j,那么j就是个质数实锤了。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()) {
            int num = in.nextInt();

            System.out.println(pairsOfPrime(num));
        }
    }

    private static int pairsOfPrime(int num) {
        //统计和为num的质数对

        int count = 0;

        for (int i = 2; i <= num / 2; i++) {
            if (isPrime(i) && isPrime(num - i)) count++;
        }

        return count;
    }

    private static boolean isPrime(int num) {
        //num是否为质数

        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) return false;
        }

        return true;
    }
}
发表于 2022-01-25 09:11:32 回复(0)
import java.util.Scanner;
public class Main
{
    public static boolean isPrime(int i)//判断是否为质数
    {
        int j;
        for(j=2;j<i;j++)
        {
            if(i%j==0)break;
            else continue;
        }
        if(j<i)return false;
        else return true;
    }
    public static int getResult(int n)//获得输出结果
    {
        if(n<3||n>=1000)return 0;//判断所输入的数在不在范围内,不在返回0(没有)
        int count=0;//结果
        //在3-n/2这个区间遍历,先找到一个素数i,再利用这个素数去找下一个数t并判断t是不是素数,是则count++
        for(int i=3;i<=n/2;i++)
        {
            if(isPrime(i))
            {
                int t=n-i;
               if(isPrime(t))
                   count++;
                else continue;
            }
            else continue;
        }
        return count;
    }
    public static void main(String[] args)
    {
        Scanner reader=new Scanner(System.in);
        int n=reader.nextInt();
        System.out.println(getResult(n));
    }
}
发表于 2020-08-15 21:37:54 回复(0)
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n = scan.nextInt();     
        int sum=0;
        for (int i = 2; i <= n/2; i++) {
            boolean flag=true;
            for (int j = 2; j < i; j++) {
                if (i%j==0) {
                    flag=false;
                    break;
                }
            }
            if (flag) {
                int k=n-i;
                //flag=true;
                for (int j = 2; j < k; j++) {
                    if (k%j==0) {
                        flag=false;
                        break;
                    }
                }
                if (flag) {
                    sum++;
                }
            }
        }
        System.out.println(sum);
    }
}
发表于 2018-10-02 21:21:57 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int count = 0; //存取素数对个数
        for (int i = 1; i <= num/2; i++) {
            if(fun(i) && fun(num-i)){
                count++;
            }
        }
        System.out.println(count);
    }
    private static boolean fun(int n){ //提供一个判断是否是素数的方法
        if(n <= 1){
            return false;
        }
        if(n == 2 | n== 3){
            return true;
        }
        if(n > 3){
            for (int i = 2; i <= n/2; i++) {
                if(n%i == 0){
                    return false;
                }
            }
        }
        return true;
    }
}
发表于 2018-07-26 11:16:45 回复(0)
先构造素数数组,然后判断a - i 和 i 是否都为素数,是的话结果数加一。
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        System.out.println(couple(a));
    }
    public static int couple(int a) {
        int []arr = new int[1000];
        arr[2] = 1;
        for (int i = 3;i < a;i ++) {
            boolean flag = true;
            for (int j = 2; j < i && j <= 31; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                arr[i] = 1;
            }
        }

        int res = 0;
        for (int i = 2;i <= a/2;i ++) {
            if (arr[i] == 1 && arr[a - i] == 1) {
                res ++;
            }
        }
        return res;
    }
}

发表于 2018-06-05 22:56:11 回复(0)
import java.util.*;

public class Demo1 {
    static int x=0;
    public static void main(String []args) {
        Scanner in=new Scanner(System.in);
        while(in.hasNext()) {
            int num=in.nextInt();
            for(int j=3;j<num;j++) {
                if(isnum(j)) {
                     if(isnum(num-j)) {
                         x++;
                         if(num-j==j) x++;
                          }
                }
            }
            System.out.println(x/2);
            }
        }
public static boolean isnum(int num) {
        for(int i=2;i<=Math.sqrt(num);i++) {
            if(num%i==0) {
                return false;
            }
        }
        return true;
    }
}

发表于 2018-04-30 13:43:53 回复(0)
import java.util.Scanner;    public class Main { public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);  int count =0;  try { int m =scanner.nextInt();  for(int i=m-2;i>=m/2;i--)
                { if(testIsPrime(i)&&testIsPrime(m-i))
                    {   count++;  }
                }
            System.out.println(count);    } catch (Exception e) {
            System.out.println(-1);    }
    }  public static boolean testIsPrime(int n)
    { if (n <= 3) { return n > 1;  }  for(int i=2;i<=Math.sqrt(n);i++)
        { if(n%i == 0) return false;  } return true;    }
}

编辑于 2018-04-23 22:23:22 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<Integer> li = zhishu(n);
        int count=0;
        /*
        思路:在n以内的质数集合li中(集合已经是有序的)定义一个头指针i和一个尾指针j
             从两边遍历,如果找到一对这样的数,计数器自增,继续循环找;如果找到的数
             之和比n小,说明此次循环没必要继续找下去了(因为越往下找越小),所以直接
             break内循环;如果找到的数之和比n大,说明尾指针需要往左移(往左数越小)
             那么直接继续内循环就行。
         */
        for(int i=0;i<=li.size();i++){
           for(int j=li.size()-1;j>=i;j--){
               if(li.get(i)+li.get(j)==n){
                   count++;
               }else if (li.get(i)+li.get(j)<n){
                   break;
               }else {
                   continue;
               }
           }
        }
        System.out.println(count);
    }

    //输入正整数n;输出n以内所有的质数集合
    public static List<Integer> zhishu(int n){
        boolean flag ;
        List<Integer> li = new ArrayList();
        for(int i=2;i<=n;i++){
            flag =true;
            for(int j=2;j<=(int)Math.sqrt(i);j++){
                if(i%j==0)
                    flag = false;
            }
            if(flag){
                li.add(i);
            }
        }
        return  li;
    }
} 

编辑于 2018-04-09 14:17:43 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    static Integer[] primeList = new Integer[]{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997};

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        HashSet<Integer> primeSet = new HashSet<>();
        primeSet.addAll(Arrays.asList(primeList));
        HashSet<Integer> answer = new HashSet<>();
        int val = scanner.nextInt();
        for(Integer i:primeList){
            if(i>=val)break;
            Integer a = val - i;
            if(primeSet.contains(a)){
                answer.add(a<i?a:i);
            }
        }
        System.out.println(answer.size());
    }

}


打表直接上
发表于 2018-03-28 12:21:14 回复(1)
import java.util.Scanner;
public class Main{
    private static boolean isPrime( int n ){
        //两个较小数另外处理  
        if( n == 2 || n==3)
            return true;
        //不在6的倍数两侧的一定不是质数 
        if( n%6 != 1 && n%6 != 5 )
            return false;
        //在6的倍数两侧的也可能不是质数
        for( int i = 5; i * i <= n; i += 6 )
            if( n % i == 0 ||n%(i+2)==0 )
                return false;
        //排除所有,剩余的是质数
        return true;
    }   
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int count=0;
        for (int i =3;i<=n/2;i+=2){
            if(isPrime(i)&&isPrime(n-i))
                count++;
        }
        System.out.println(count);
    }
}
编辑于 2018-07-17 15:33:10 回复(0)

由于是要两个加数都是质数,则遍历一半就可以,注意1不是质数,2是

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int result = 0;
        for (int i = 1; i <= num / 2; i++) {
            if (isPri(i) && isPri(num - i))
                result++;
        }
        System.out.println(result);
    }

    public static boolean isPri(int n) {
        if (n == 1)
            return false;
        if (n == 2)
            return true;
        for (int i = 2; i < n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
}
发表于 2018-03-14 19:44:59 回复(0)
import java.util.*;

public class Main{
      public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Set<Integer> su = getSu();
        int count=0;
        for (int s: su){
            for (int s2: su){
                if (s+s2 == n){
                    if (s == s2)
                        count += 2;
                    else
                        count++;
                    break;
                }
            }
        }
        count /= 2;
        System.out.println(count);
    }
    public static Set<Integer> getSu(){
        Set<Integer> su = new HashSet<Integer>();
        for (int i=2; i<1000; i++){
            boolean flag = true;
            int j;
            for (j=2; j<i; j++){
                if (i%j == 0){
                    flag = false;
                    break;
                }
            }
            if (flag)
                su.add(j);
        }
        return su;
    }
}
发表于 2018-03-04 13:46:28 回复(0)
Java路过
public class Main {
    public static void main(String args[]) {
        int target = new Scanner(System.in).nextInt();
        System.out.println(IntStream.iterate(2, n->n+1)
                .limit(1000)    //生成  2-1001 的IntStream
                    .filter(Main::isPrim)    //只保留质数    
                        .map(m->target-m)        //target-m
                            .filter(Main::isPrim)    //将非质数的(target-m)过滤
                                .filter(m->m<=target/2).count());    
            //比如10 = 3 + 7  对于每一个质数对保留一大一小两个数而 10 = 5 + 5 这个质数对只保留一个数 
            //对于最终的IntStream,截取 (流头部,target/2] 或 [target/2,流尾部) 
    }                                                                
    public static boolean isPrim(int a){ //判断质数不解释
        if(a<=1) return false;
        return IntStream.rangeClosed(2, (int)Math.sqrt(a)).noneMatch(i->a%i==0);
    }
}

发表于 2018-03-02 11:44:40 回复(0)
import java.util.ArrayList;
import java.util.Scanner;

public class Main{
    
    private ArrayList<Integer> primeCollections = new ArrayList<Integer>();
    
    public static void main(String[] args) {
        Main m = new Main();
        m.primeCollections(2,1000);
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int count = 0;
        for (int j = 0;j<m.primeCollections.size();j++) {
            if (m.primeCollections.contains(n-m.primeCollections.get(j))) {
                if((n-m.primeCollections.get(j))>=m.primeCollections.get(j))
                count ++;
            }
        }
        System.out.println(count);
    }
    
    public boolean testIsPrime(int n){
        if (n <= 3) {
             return n > 1;
         }
         
        for(int i=2;i<=Math.sqrt(n);i++){
            if(n%i == 0)
                return false;
        }
        return true;
    }
    
    public void primeCollections(int a, int b) {
        for (int i=a;i<=b;i++) {
            if(testIsPrime(i)) {
                primeCollections.add(i);
            }
        }
    }

}      
只能使用蠢办法……
发表于 2018-02-24 22:21:30 回复(0)
//我这是最牛逼的回答,没有之一,其它的都是lj
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main{
    
    //素数对
    public static void SuShuDui(String str){
        
        //输入的数字
        int parseInt = Integer.parseInt(str);
        //奇数集合
        List<Integer> oddList = new ArrayList<Integer>();
        List<Integer> suList = new ArrayList<Integer>();
        //数量
        int count = 0;
        for(int i=3;i<parseInt;i++){
            //大于3的数只要是素数就肯定是奇数
            //先获取奇数
            if(i%2==1){
                oddList.add(i);
            }
        }
        
        //获取素数
        for(int j=0;j<oddList.size();j++){
            boolean flag = true;
            int odd = oddList.get(j);
            for(int k=odd-1;k>1;k--){
                //遍历除与比自己小的数,如果整除了就不是。
                if(odd%k==0){
                    flag = false;
                    break;
                }
            }
            
            if(flag){
                //是素数,加入集合
                suList.add(odd);
            }
        }
        
        for(int n=0;n<suList.size();n++){
            for(int m=n;m<suList.size();m++){
                if(suList.get(n)+suList.get(m)==parseInt){
                    count++;
                }
            }
        }
        
        System.out.println(count);
    }
    
    
    public static void main(String[] args) {
        
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String s = "";
        try {
            while((s=bufferedReader.readLine())!=null){
                SuShuDui(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
     }
}


发表于 2018-02-09 15:12:46 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num = in.nextInt();
            int count = 0;
            for (int i = 2; i < num; i++) {
                for (int j = i; j < num; j++) {
                    if (isPrime(i) && isPrime(j) && i + j == num)
                        count++;
                }
            }
            System.out.println(count);
        }
    }
    private static boolean isPrime(int num) {
        if (num < 2) return false;
        for (int i = 2; i <= Math.sqrt(num); i++)
            if (num % i == 0) return false;
        return true;
    }
}
-------------------------------------------------------------------------
//第一次优化
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num = in.nextInt();
            int count = 0;
            for (int i = 2; i <= num / 2; i++) {
                if (isPrime(i) && isPrime(num - i))
                    count++;
            }
            System.out.println(count);
        }
    }
    private static boolean isPrime(int num) {
        if (num < 2) return false;
        for (int i = 2; i <= Math.sqrt(num); i++)
            if (num % i == 0) return false;
        return true;
    }
}
//第二次优化:筛选法
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] arr = new int[1000];
        Arrays.fill(arr, 1);
        while (in.hasNext()) {
            int num = in.nextInt();
            int count = 0;
            for (int i = 2; i <= num / 2; i++) {
                if (arr[i] == 1) {
                    for (int j = 2; i * j < 1000; j++)
                        arr[i * j] = 0;
                }
            }
            for (int i = 2; i <= num / 2; i++) {
                if (arr[i] == 1 && arr[num - i] == 1)
                    count++;
            }
            System.out.println(count);
        }
    }
}

编辑于 2017-12-08 17:16:16 回复(0)
import java.util.LinkedList;  import java.util.Scanner;   public class Main
{  public static void main(String args[]) 
    {
        Scanner scanner = new Scanner(System.in);  LinkedList<Integer> sushu = new LinkedList<>();  int i;  int j;  for (i = 0; i <= 1000; i++) 
        {  for (j = 2; j < i; j++) 
            {  if (i % j == 0) 
                {  break;   }
            }  if (j == i) 
            {
                sushu.add(i);   }
        }  while (scanner.hasNext()) 
        {  int count = 0;  int number = Integer.parseInt(scanner.nextLine());  for (int m = 2; m <= (number + 1) / 2; m++) 
            {  if (sushu.contains(m) && sushu.contains(number - m))
                {
                    count++;   }
            }
            System.out.println(count);  }
    }
}
感觉因为范围不是很大,是1000以内,所以可以把所有的素数都列出来,判断两个数在不在素数链表里面即可

发表于 2017-11-21 21:39:36 回复(0)
    public static int test(int sum){
        
        int count=0;
        boolean[] notPrime = new boolean[sum];//notPrime[i]为true表示i不为素数,为false表示为素数
        for(int i=2;i<sum;i++){
            for(int j=2;i*j<sum;j++){
                notPrime[i*j]=true;
            }
        }
        
        for(int i=2;i<=sum/2;i++){
            if(!notPrime[i]&&!notPrime[sum-i]){
                count++;
            }
        }
        
        return count; 

    }
发表于 2017-10-26 22:30:55 回复(0)
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
int x, y;
int count = 0;
for (int i = 1; i <= n / 2; i++) {
x = i;
y = n - i;
if (isNum(x) && isNum(y)) {
count++;
}
}
System.out.println(count);
}
       //判斷素數
public static boolean isNum(int n) {
if (n > 0 && n != 1) {
int test = n - 1;
while (n % test != 0) {
test--;
}
if (test == 1)
return true;
} else {
return false;
}
return false;
}
}

发表于 2017-09-10 22:57:32 回复(0)