1.素數對猜想
阿新 • • 發佈:2017-10-13
baseline fonts using ext -o fontsize 素數 include pts
讓我們定義d?n??為:d?n??=p?n+1???p?n??,其中p?i??是第i個素數。顯然有d?1??=1,且對於n>1有d?n??是偶數。“素數對猜想”認為“存在無窮多對相鄰且差為2的素數”。
現給定任意正整數N
(<),請計算不超過N
的滿足猜想的素數對的個數。
輸入格式:
輸入在一行給出正整數N
。
輸出格式:
在一行中輸出不超過N
的滿足猜想的素數對的個數。
輸入樣例:
20
輸出樣例:
4
#include<cmath> #include<iostream> using namespace std; bool isPrime(int n) { if(n == 1 || n == 0) return false; if (n == 2) return true; int tmp = (int)sqrt((double)n); for (int i = 2; i <= tmp; i++) { if (n%i == 0) return false; } return true; } int main() { int num, count = 0; cin >> num; for (int i = 3; i <= num - 2; i++) { if (isPrime(i) && isPrime(i + 2)) count++; } cout << count << endl; return 0; }
1.素數對猜想