官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 常山县| 赤壁市| 阿坝| 井研县| 亚东县| 乐至县| 开化县| 安康市| 普安县| 咸宁市| 吴堡县| 山东省| 台州市| 霍城县| 岳池县| 黑山县| 衡阳县| 略阳县| 景谷| 宁都县| 涡阳县| 彩票| 合山市| 峨边| 固阳县| 昆明市| 依兰县| 大竹县| 福贡县| 剑阁县| 阳东县| 丁青县| 周至县| 天水市| 安庆市| 新源县| 洪雅县| 大埔县| 玉林市| 织金县| 南昌县|