1007 素數(shù)對猜想(PAT)

讓我們定義 dn 為:dn = pn+1 - pn,其中 pi 是第i個素數(shù)。顯然有 d1=1 且對于n&gt1有 dn 是偶數(shù)?!八財?shù)對猜想”認(rèn)為“存在無窮多對相鄰且差為2的素數(shù)”。

現(xiàn)給定任意正整數(shù)N (< 105),請計算不超過N的滿足猜想的素數(shù)對的個數(shù)。

輸入格式:每個測試輸入包含1個測試用例,給出正整數(shù)N。

輸出格式:每個測試用例的輸出占一行,不超過N的滿足猜想的素數(shù)對的個數(shù)。

輸入樣例:

20
輸出樣例:

4

代碼1:

#include<stdio.h>
#include<stdlib.h>
//判斷素數(shù)的子函數(shù)
int issushu(int n)
{
    int i,falg=1;
    for(i=2; i<=n/2; ++i)
    {
        // 符合該條件不是素數(shù)
        if(n%i==0)
        {
            falg=0;
            break;
        }
    }
 
    if (falg==0)
        return 0;
    else
       return 1;
    
}

int  main()
{
    int n,count=0;
    
    scanf("%d",&n);
    while(n!=3)
    {
        if((issushu(n))&&(issushu(n-2)))
        {
            count++;
        }
        n--;
    }
    printf("%d",count);
    system("pause");
    return 0;

}

超時報錯
代碼2:
修改了判斷素數(shù)的子函數(shù)和主函數(shù)一些內(nèi)容

#include<stdio.h>
#include <math.h>
#include<stdlib.h>
//判斷素數(shù)的子函數(shù)

int issushu(int n)
{
    int x,y;
    x=(int)sqrt(n);
    for(y=2;y<=x;y++){
    if(n%y==0){
      return 0;
    }
  }
  if(y>x){
    return 1;
  }
}
int  main()
{
    int n,count=0;
    
    scanf("%d",&n);
    while(n>=5)
    {
        if((issushu(n))&&(issushu(n-2)))
        {
            count++;
        }
        n--;
    }
    printf("%d",count);
    system("pause");
    return 0;

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容