# Special prime numbers

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 = 2Output : YESIn this sample the answer is YES since at least two numbers are Special 13(5 + 7 + 1) and19(7 + 11 + 1).Input : n = 45, k = 7Output : NOIn 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 thank, 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:-

## C++

 `// CPP program to check whether there``// exist at least k or not in range [2..n]``#include ``using` `namespace` `std;` `vector<``int``> primes;` `// Generating all the prime numbers``// from 2 to n.``void` `SieveofEratosthenes(``int` `n)``{``    ``bool` `visited[n];``    ``for` `(``int` `i = 2; i <= n + 1; i++)``        ``if` `(!visited[i]) {``            ``for` `(``int` `j = i * i; j <= n + 1; j += i)``                ``visited[j] = ``true``;``            ``primes.push_back(i);``        ``}``}` `bool` `specialPrimeNumbers(``int` `n, ``int` `k)``{``    ``SieveofEratosthenes(n);``    ``int` `count = 0;``    ``for` `(``int` `i = 0; i < primes.size(); i++) {``        ``for` `(``int` `j = 0; j < i - 1; j++) {` `            ``// If a prime number is Special prime``            ``// number, then we increments the``            ``// value of k.``            ``if` `(primes[j] + primes[j + 1] + 1``                ``== primes[i]) {``                ``count++;``                ``break``;``            ``}``        ``}` `        ``// If at least k Special prime numbers``        ``// are present, then we return 1.``        ``// else we return 0 from outside of``        ``// the outer loop.``        ``if` `(count == k)``            ``return` `true``;``    ``}``    ``return` `false``;``}` `// Driver function``int` `main()``{``    ``int` `n = 27, k = 2;``    ``if` `(specialPrimeNumbers(n, k))``        ``cout << ``"YES"` `<< endl;``    ``else``        ``cout << ``"NO"` `<< endl;``    ``return` `0;``}`

## Java

 `// Java program to check whether there``// exist at least k or not in range [2..n]``import` `java.util.*; ``class` `GFG{``static` `ArrayList primes = ``new` `ArrayList();``// Generating all the prime numbers``// from 2 to n.``static` `void` `SieveofEratosthenes(``int` `n)``{``    ``boolean``[] visited=``new` `boolean``[n*n+``2``];``    ``for` `(``int` `i = ``2``; i <= n + ``1``; i++)``        ``if` `(!visited[i]) {``            ``for` `(``int` `j = i * i; j <= n + ``1``; j += i)``                ``visited[j] = ``true``;``            ``primes.add(i);``        ``}``}` `static` `boolean` `specialPrimeNumbers(``int` `n, ``int` `k)``{``    ``SieveofEratosthenes(n);``    ``int` `count = ``0``;``    ``for` `(``int` `i = ``0``; i < primes.size(); i++) {``        ``for` `(``int` `j = ``0``; j < i - ``1``; j++) {` `            ``// If a prime number is Special prime``            ``// number, then we increments the``            ``// value of k.``            ``if` `(primes.get(j) + primes.get(j + ``1``) + ``1``                ``== primes.get(i)) {``                ``count++;``                ``break``;``            ``}``        ``}` `        ``// If at least k Special prime numbers``        ``// are present, then we return 1.``        ``// else we return 0 from outside of``        ``// the outer loop.``        ``if` `(count == k)``            ``return` `true``;``    ``}``    ``return` `false``;``}` `// Driver function``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``27``, k = ``2``;``    ``if` `(specialPrimeNumbers(n, k))``        ``System.out.println(``"YES"``);``    ``else``        ``System.out.println(``"NO"``);``}``}``// This code is contributed by mits`

## Python3

 `# Python3 program to check whether there``# exist at least k or not in range [2..n]``primes ``=` `[];` `# Generating all the prime numbers``# from 2 to n.``def` `SieveofEratosthenes(n):` `    ``visited ``=` `[``False``] ``*` `(n ``+` `2``);``    ``for` `i ``in` `range``(``2``, n ``+` `2``):``        ``if` `(visited[i] ``=``=` `False``): ``            ``for` `j ``in` `range``(i ``*` `i, n ``+` `2``, i):``                ``visited[j] ``=` `True``;``            ``primes.append(i);` `def` `specialPrimeNumbers(n, k):` `    ``SieveofEratosthenes(n);``    ``count ``=` `0``;``    ``for` `i ``in` `range``(``len``(primes)):``        ``for` `j ``in` `range``(i ``-` `1``): ` `            ``# If a prime number is Special ``            ``# prime number, then we increments ``            ``# the value of k.``            ``if` `(primes[j] ``+``                ``primes[j ``+` `1``] ``+` `1` `=``=` `primes[i]): ``                ``count ``+``=` `1``;``                ``break``;` `        ``# If at least k Special prime numbers``        ``# are present, then we return 1.``        ``# else we return 0 from outside of``        ``# the outer loop.``        ``if` `(count ``=``=` `k):``            ``return` `True``;` `    ``return` `False``;` `# Driver Code``n ``=` `27``;``k ``=` `2``;``if` `(specialPrimeNumbers(n, k)):``    ``print``(``"YES"``);``else``:``    ``print``(``"NO"``);` `# This code is contributed by mits`

## C#

 `// C# program to check whether there``// exist at least k or not in range [2..n]``using` `System;``using` `System.Collections; ` `class` `GFG{``static` `ArrayList primes = ``new` `ArrayList();``// Generating all the prime numbers``// from 2 to n.``static` `void` `SieveofEratosthenes(``int` `n)``{``    ``bool``[] visited=``new` `bool``[n*n+2];``    ``for` `(``int` `i = 2; i <= n + 1; i++)``        ``if` `(!visited[i]) {``            ``for` `(``int` `j = i * i; j <= n + 1; j += i)``                ``visited[j] = ``true``;``            ``primes.Add(i);``        ``}``}` `static` `bool` `specialPrimeNumbers(``int` `n, ``int` `k)``{``    ``SieveofEratosthenes(n);``    ``int` `count = 0;``    ``for` `(``int` `i = 0; i < primes.Count; i++) {``        ``for` `(``int` `j = 0; j < i - 1; j++) {` `            ``// If a prime number is Special prime``            ``// number, then we increments the``            ``// value of k.``            ``if` `((``int``)primes[j] + (``int``)primes[j + 1] + 1``                ``== (``int``)primes[i]) {``                ``count++;``                ``break``;``            ``}``        ``}` `        ``// If at least k Special prime numbers``        ``// are present, then we return 1.``        ``// else we return 0 from outside of``        ``// the outer loop.``        ``if` `(count == k)``            ``return` `true``;``    ``}``    ``return` `false``;``}` `// Driver function``public` `static` `void` `Main()``{``    ``int` `n = 27, k = 2;``    ``if` `(specialPrimeNumbers(n, k))``        ``Console.WriteLine(``"YES"``);``    ``else``        ``Console.WriteLine(``"NO"``);``}``}``// This code is contributed by mits`

## Javascript

 ``

## PHP

 ``

Output:-

` YES`

#### Approach#2: Using brute force

In this approach, we will iterate from 2 to n and check for each number if it is a special prime number or not. We will keep a count of the special prime numbers we have found so far and return “YES” if the count reaches k, otherwise “NO”.

#### Algorithm

1. Initialize count to 0.
2. For each number i from 2 to n:
a. Check if i is prime.
b. If i is prime, calculate the sum of i’s largest two prime factors and add 1 to it.
c. Check if the sum obtained in step b is a prime number or not.
d. If the sum is prime, increment the count.
e. If count is equal to k, return “YES”.
3. If we have not found k special prime numbers, return “NO”.

## C++

 `// C++ code implementation``#include ``#include ``using` `namespace` `std;` `bool` `is_prime(``int` `x)``{``    ``if` `(x < 2) {``        ``return` `false``;``    ``}``    ``for` `(``int` `i = 2; i <= ``sqrt``(x); i++) {``        ``if` `(x % i == 0) {``            ``return` `false``;``        ``}``    ``}``    ``return` `true``;``}` `bool` `is_special_prime(``int` `x)``{``    ``int` `s = 0;``    ``for` `(``int` `i = 2; i <= x; i++) {``        ``if` `(is_prime(i)) {``            ``s += i;``        ``}``    ``}``    ``return` `is_prime(s);``}` `string count_special_primes(``int` `n, ``int` `k)``{``    ``int` `count = 0;``    ``for` `(``int` `i = 2; i <= n; i++) {``        ``if` `(is_special_prime(i)) {``            ``count++;``        ``}``        ``if` `(count >= k) {``            ``return` `"YES"``;``        ``}``    ``}``    ``return` `"NO"``;``}` `// Driver code``int` `main()``{``    ``int` `n = 27;``    ``int` `k = 2;``    ``cout << count_special_primes(n, k) << std::endl;``    ``return` `0;``}`

## Java

 `//Java code for the above approach``import` `java.util.*;` `public` `class` `SpecialPrime {` `    ``// Function to check if a number is prime``    ``public` `static` `boolean` `isPrime(``int` `x) {``        ``if` `(x < ``2``) {``            ``return` `false``;``        ``}``        ``for` `(``int` `i = ``2``; i <= Math.sqrt(x); i++) {``            ``if` `(x % i == ``0``) {``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}` `    ``// Function to check if a number is a special prime``    ``public` `static` `boolean` `isSpecialPrime(``int` `x) {``        ``int` `s = ``0``;``        ``for` `(``int` `i = ``2``; i <= x; i++) {``            ``if` `(isPrime(i)) {``                ``s += i;``            ``}``        ``}``        ``return` `isPrime(s);``    ``}` `    ``// Function to count special primes up to 'n' and determine if there are at least 'k' of them``    ``public` `static` `String countSpecialPrimes(``int` `n, ``int` `k) {``        ``int` `count = ``0``;``        ``for` `(``int` `i = ``2``; i <= n; i++) {``            ``if` `(isSpecialPrime(i)) {``                ``count++;``            ``}``            ``if` `(count >= k) {``                ``return` `"YES"``;``            ``}``        ``}``        ``return` `"NO"``;``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``int` `n = ``27``;``        ``int` `k = ``2``;``        ` `        ``// Call the function to count special primes and output the result``        ``System.out.println(countSpecialPrimes(n, k));``    ``}``}`

## Python3

 `def` `count_special_primes(n, k):``    ``def` `is_prime(x):``        ``if` `x < ``2``:``            ``return` `False``        ``for` `i ``in` `range``(``2``, ``int``(x ``*``*` `0.5``) ``+` `1``):``            ``if` `x ``%` `i ``=``=` `0``:``                ``return` `False``        ``return` `True` `    ``def` `is_special_prime(x):``        ``s ``=` `0``        ``for` `i ``in` `range``(``2``, x ``+` `1``):``            ``if` `is_prime(i):``                ``s ``+``=` `i``        ``return` `is_prime(s)` `    ``count ``=` `0``    ``for` `i ``in` `range``(``2``, n ``+` `1``):``        ``if` `is_special_prime(i):``            ``count ``+``=` `1``        ``if` `count >``=` `k:``            ``return` `"YES"``    ``return` `"NO"``n``=``27``k``=``2``print``(count_special_primes(n, k))`

## C#

 `using` `System;` `class` `GFG``{``    ``static` `bool` `IsPrime(``int` `x)``    ``{``        ``if` `(x < 2)``        ``{``            ``return` `false``;``        ``}``        ``for` `(``int` `i = 2; i <= Math.Sqrt(x); i++)``        ``{``            ``if` `(x % i == 0)``            ``{``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}` `    ``static` `bool` `IsSpecialPrime(``int` `x)``    ``{``        ``int` `s = 0;``        ``for` `(``int` `i = 2; i <= x; i++)``        ``{``            ``if` `(IsPrime(i))``            ``{``                ``s += i;``            ``}``        ``}``        ``return` `IsPrime(s);``    ``}` `    ``static` `string` `CountSpecialPrimes(``int` `n, ``int` `k)``    ``{``        ``int` `count = 0;``        ``for` `(``int` `i = 2; i <= n; i++)``        ``{``            ``if` `(IsSpecialPrime(i))``            ``{``                ``count++;``            ``}``            ``if` `(count >= k)``            ``{``                ``return` `"YES"``;``            ``}``        ``}``        ``return` `"NO"``;``    ``}` `    ``static` `void` `Main(``string``[] args)``    ``{``        ``int` `n = 27;``        ``int` `k = 2;``        ``Console.WriteLine(CountSpecialPrimes(n, k));``    ``}``}`

## Javascript

 `// Javascript code addition ``function` `count_special_primes(n, k) {``    ``function` `is_prime(x) {``        ``if` `(x < 2) {``            ``return` `false``;``        ``}``        ``for` `(let i = 2; i <= Math.sqrt(x); i++) {``            ``if` `(x % i === 0) {``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}` `    ``function` `is_special_prime(x) {``        ``let s = 0;``        ``for` `(let i = 2; i <= x; i++) {``            ``if` `(is_prime(i)) {``                ``s += i;``            ``}``        ``}``        ``return` `is_prime(s);``    ``}` `    ``let count = 0;``    ``for` `(let i = 2; i <= n; i++) {``        ``if` `(is_special_prime(i)) {``            ``count += 1;``        ``}``        ``if` `(count >= k) {``            ``return` `"YES"``;``        ``}``    ``}``    ``return` `"NO"``;``}` `let n = 27;``let k = 2;``console.log(count_special_primes(n, k));` `// The code is contributed by Anjali goel.`

Output
```YES

```

Time Complexity: O(n^2loglogn) (iterating from 2 to n and then checking for each number if it is prime or not)

Auxiliary Space: O(1) (no extra space used)

Previous
Next