- 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.
推薦閱讀
- Objective-C Memory Management Essentials
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- C#編程入門指南(上下冊)
- jQuery開發基礎教程
- Mastering Ext JS
- PHP+Ajax+jQuery網站開發項目式教程
- Python大學實用教程
- Building Dynamics CRM 2015 Dashboards with Power BI
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- HTML5移動Web開發
- Java Web 從入門到項目實踐(超值版)
- 熱處理常見缺陷分析與解決方案
- 面向對象程序設計教程(C#版)
- 區塊鏈原理、架構與應用(第2版)