美文网首页
PAT-B 1007 素数对猜想(C语言)

PAT-B 1007 素数对猜想(C语言)

作者: dk_qi | 来源:发表于2018-12-15 11:12 被阅读0次

    题目

    链接:PAT (Basic Level) Practice 1007 素数对猜想

    让我们定义d​_n为:d_n​ =p​_{n+1}​​ − p​_n其中p​_i​​是第i个素数。显然有d​_1 =1,且对于n>1有d​_n是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
    现给定任意正整数N(<10^5),请计算不超过N的满足猜想的素数对的个数。

    输入格式:

    输入在一行给出正整数N。

    输出格式:

    在一行中输出不超过N的满足猜想的素数对的个数。

    输入样例:

    20

    输出样例:

    4


    思路

    当i(4 <= i <= n)和i-2均为素数时即满足猜想。


    代码

    #include<stdio.h>
    #include<math.h>
    #include<stdbool.h>
    
    bool IsPrime(int m);  //判断是否为素数
    
    int main()
    {
      int N;
      scanf("%d", &N);
      int count = 0;   //count用来计数
      for(int i = 4; i <= N; i++){
        if(IsPrime(i) && IsPrime(i-2)){
          count++;
        }
      }
      printf("%d", count);
      return 0;
    }
    
    bool IsPrime(int m){
      for(int j = 2; j <= sqrt(m); j++){
        if(m % j == 0){
          return false;
        }
      }
      return true;
    }
    

    END

    其它相关问题

    PAT-B 1006 换个格式输出整数(C语言)
    PAT-B 1008 数组元素循环右移问题(C语言)
    PAT-B 1009 说反话(C语言)
    PAT-B 1010 一元多项式求导(C语言)

    相关文章

      网友评论

          本文标题:PAT-B 1007 素数对猜想(C语言)

          本文链接:https://www.haomeiwen.com/subject/ktnohqtx.html