- 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.
推薦閱讀
- Go語言高效編程:原理、可觀測性與優(yōu)化
- 青少年美育趣味課堂:XMind思維導(dǎo)圖制作
- Java Web程序設(shè)計
- PhoneGap Mobile Application Development Cookbook
- SQL Server 2016數(shù)據(jù)庫應(yīng)用與開發(fā)
- 機器學(xué)習(xí)微積分一本通(Python版)
- Unity 2018 Augmented Reality Projects
- 后臺開發(fā):核心技術(shù)與應(yīng)用實踐
- Java 9 Programming By Example
- Django Design Patterns and Best Practices
- Python編程快速上手2
- 面向物聯(lián)網(wǎng)的Android應(yīng)用開發(fā)與實踐
- Java無難事:詳解Java編程核心思想與技術(shù)
- Machine Learning for the Web
- Python數(shù)據(jù)分析實戰(zhàn)