- 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.
推薦閱讀
- The Complete Rust Programming Reference Guide
- Spring Boot 2實戰(zhàn)之旅
- Java Web開發(fā)學(xué)習(xí)手冊
- JavaScript百煉成仙
- HTML5移動Web開發(fā)技術(shù)
- SoapUI Cookbook
- Python自然語言處理實戰(zhàn):核心技術(shù)與算法
- 匯編語言程序設(shè)計(第2版)
- Visual Basic程序設(shè)計教程
- 編程可以很簡單
- 深度學(xué)習(xí)程序設(shè)計實戰(zhàn)
- 你真的會寫代碼嗎
- Backbone.js Patterns and Best Practices
- Processing開發(fā)實戰(zhàn)
- Qt編程快速入門