VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-n-is-strong-prime/

⇱ Check if N is Strong Prime - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if N is Strong Prime

Last Updated : 27 Aug, 2022

Given a positive integer N, the task is to check if N is a strong prime or not. 
In number theory, a strong prime is a prime number that is greater than the arithmetic mean of nearest prime numbers i.e next and previous prime numbers. 
First few strong prime numbers are 11, 17, 29, 37, 41, 59, 67, 71, ... 
A strong prime Pn can be represented as- 
 

👁 Strong prime


where n is its index in the ordered set of prime numbers.

Examples: 

Input: N = 11 
Output: Yes 
11 is 5th prime number, the arithmetic mean of 4th and 6th prime number i.e. 7 and 13 is 10. 
11 is greater than 10 so 11 is a strong prime. 

Input: N = 13 
Output: No 
13 is 6th prime number, the arithmetic mean of 5th (11) and 7th (17) is (11 + 17) / 2 = 14. 
13 is smaller than 14 so 13 is not a strong prime. 
 

Approach: 

  • If N is not a prime number or it is the first prime number i.e. 2 then print No.
  • Else find the primes closest to N (one on the left and one on the right) and store their arithmetic mean in mean
    • If N > mean then print Yes.
    • Else print No.

Below is the implementation of the above approach: 


Output: 
Yes

 

Time Complexity: O(n1/2)

Auxiliary Space: O(1), since no extra space has been taken.

Comment