标签:blank 输入格式 ott isp eset return bin line nbsp
让我们定义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
注意,题目要求是相邻的两个素数相差为2,尽管1和3都是素数且相差2,但并不是相邻的,因为1 2 3都是素数,1和3并不相邻,所以从3 5开始算是第一个素数对
1 #include <iostream> 2 using namespace std; 3 4 bool isPrime(int n) 5 { 6 for (int i = 2; i * i <= n; ++i) 7 { 8 if (n%i == 0) 9 { 10 return false; 11 } 12 } 13 return true; 14 } 15 16 int main() 17 { 18 int N; 19 cin >> N; 20 int count = 0; 21 for (int i = 5; i <= N; ++i) 22 { 23 if (isPrime(i) && isPrime(i - 2)) 24 { 25 count++; 26 } 27 } 28 cout << count; 29 return 0; 30 }
标签:blank 输入格式 ott isp eset return bin line nbsp
原文地址:https://www.cnblogs.com/47Pineapple/p/11379273.html