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.
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 implementation of the above approach:-
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Find Largest Special Prime which is less than or equal to a given number
- Smallest Special Prime which is greater than or equal to a given number
- Prime numbers after prime P with sum S
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of prime numbers without odd prime digits
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Permutation of first N positive integers such that prime numbers are at prime indices
- Count all prime numbers in a given range whose sum of digits is also prime
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Numbers less than N which are product of exactly two distinct prime numbers
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Count of numbers upto M divisible by given Prime Numbers
- Quick ways to check for Prime and find next Prime in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar