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

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.

主站蜘蛛池模板: 龙门县| 达拉特旗| 郧西县| 宁强县| 汤原县| 南澳县| 仙桃市| 鹤山市| 莱西市| 宜城市| 孝昌县| 梅州市| 东阿县| 罗定市| 白城市| 香港 | 谢通门县| 明溪县| 桐庐县| 健康| 武胜县| 明水县| 郓城县| 巫山县| 松原市| 阿鲁科尔沁旗| 临潭县| 麻城市| 剑川县| 咸丰县| 呼图壁县| 北流市| 呼图壁县| 隆林| 海门市| 仙游县| 崇仁县| 古浪县| 五大连池市| 太康县| 冀州市|