Given two numbers n and k, find whether there exist at least k Special prime numbers or not from 2 to n inclusively.
A prime number is said to be Special prime number if it can be expressed as the sum of three integer numbers: two neighboring prime numbers and 1. For example, 19 = 7 + 11 + 1, or 13 = 5 + 7 + 1.
Note:- Two prime numbers are called neighboring if there are no other prime numbers between them.
Examples: Input : n = 27, k = 2 Output : YES In this sample the answer is YES since at least two numbers are Special 13(5 + 7 + 1) and 19(7 + 11 + 1). Input : n = 45, k = 7 Output : NO In this example, the Special prime numbers are 13(5 + 7 + 1), 19(7 + 11 + 1), 31(13 + 17 + 1), 37(17 + 19 + 1), 43(19 + 23 + 1). As the no. of Special prime numbers from 2 to 45 is less than k, the output is NO.
To solve this problem we need to find prime numbers in range [2..n]. So we us Sieve of Eratosthenes to generate all the prime numbers from 2 to n. Then, Take every pair of neighboring prime numbers and check if their sum increased by 1 is a prime number too. Count the number of these pairs, compare it to K and output the result.
Below is the CPP implementation of the above approach:-
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Sieve of Eratosthenes
- Find element in array that divides all array elements
- Number of visible boxes after putting one inside another
- Euler’s Totient Function
- Primality Test | Set 3 (Miller–Rabin)
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Check if the first and last digit of the smallest number forms a prime
- Print all substring of a number without any conversion
- Complement of a number with any base b
- Check if Decimal representation of an Octal number is divisible by 7