- The Modern C++ Challenge
- Marius Bancila
- 159字
- 2021-06-25 22:01:23
5. Sexy prime pairs
Sexy prime numbers are prime numbers that differ from each other by six (for example 5 and 11, or 13 and 19). There are also twin primes, which differ by two, and cousin primes, which differ by four.
In the previous challenge, we implemented a function that determines whether an integer is a prime number. We will reuse that function for this exercise. What you have to do is check that if a number n is prime, the number n+6 is also prime, and in this case print the pair to the console:
int main()
{
int limit = 0;
std::cout << "Upper limit:";
std::cin >> limit;
for (int n = 2; n <= limit; n++)
{
if (is_prime(n) && is_prime(n+6))
{
std::cout << n << "," << n+6 << std::endl;
}
}
}
You could take it as a further exercise to compute and displays the sexy prime triples, quadruplets, and quintuplets.
推薦閱讀
- JavaScript全程指南
- Maven Build Customization
- Pandas Cookbook
- C語言程序設(shè)計基礎(chǔ)與實驗指導(dǎo)
- R語言游戲數(shù)據(jù)分析與挖掘
- 深入理解Java7:核心技術(shù)與最佳實踐
- 用Flutter極速構(gòu)建原生應(yīng)用
- TypeScript 2.x By Example
- Visual C++程序設(shè)計與項目實踐
- Python編程:從入門到實踐(第2版)
- AngularJS by Example
- 計算機(jī)視覺實戰(zhàn):基于TensorFlow 2
- Java入門經(jīng)典
- 給產(chǎn)品經(jīng)理講技術(shù)
- C語言學(xué)習(xí)手冊