題目
鏈接:PAT (Basic Level) Practice 1007 素數(shù)對猜想
讓我們定義為:其中??是第i個素數(shù)猖凛。顯然有毒坛,且對于n>1有是偶數(shù)蚪缀。“素數(shù)對猜想”認為“存在無窮多對相鄰且差為2的素數(shù)”贺辰。
現(xiàn)給定任意正整數(shù)户盯,請計算不超過N的滿足猜想的素數(shù)對的個數(shù)。輸入格式:
輸入在一行給出正整數(shù)N魂爪。
輸出格式:
在一行中輸出不超過N的滿足猜想的素數(shù)對的個數(shù)先舷。
輸入樣例:
20
輸出樣例:
4
思路
當(dāng)i(4 <= i <= n)和i-2均為素數(shù)時即滿足猜想。
代碼
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
bool IsPrime(int m); //判斷是否為素數(shù)
int main()
{
int N;
scanf("%d", &N);
int count = 0; //count用來計數(shù)
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
其它相關(guān)問題
PAT-B 1006 換個格式輸出整數(shù)(C語言)
PAT-B 1008 數(shù)組元素循環(huán)右移問題(C語言)
PAT-B 1009 說反話(C語言)
PAT-B 1010 一元多項式求導(dǎo)(C語言)