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

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

主站蜘蛛池模板: 扎兰屯市| 庆云县| 安陆市| 石林| 神池县| 拉萨市| 荣昌县| 文成县| 黔西县| 永城市| 宝应县| 梓潼县| 木兰县| 正安县| 灵石县| 都昌县| 江陵县| 阿瓦提县| 徐汇区| 商南县| 玉环县| 江都市| 出国| 镇赉县| 灌南县| 常熟市| 油尖旺区| 台江县| 英德市| 安陆市| 新建县| 从江县| 南汇区| 固原市| 尼木县| 宁德市| 柏乡县| 神木县| 石城县| 和政县| 巫山县|