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-
where n is is its index in the ordered set of prime numbers.
Input: N = 11
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
13 is 6th prime number, the arithmetic mean of 5th (13) and 7th (17) is (13 + 17) / 2 = 15.
13 is smaller than 15 so 13 is not a strong prime.
- 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:
# Python 3 program to check if given
# number is strong prime
from math import sqrt
# Utility function to check if a
# number is prime or not
# Corner cases
if (n <= 1): return False if (n <= 3): return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0): return False k = int(sqrt(n)) + 1 for i in range(5, k, 6): if (n % i == 0 or n % (i + 2) == 0): return False return True # Function that returns true if # n is a strong prime def isStrongPrime(n): # If n is not a prime number or # n is the first prime then return false if (isPrime(n) == False or n == 2): return False # Initialize previous_prime to n - 1 # and next_prime to n + 1 previous_prime = n - 1 next_prime = n + 1 # Find next prime number while (isPrime(next_prime) == False): next_prime += 1 # Find previous prime number while (isPrime(previous_prime) == False): previous_prime -= 1 # Arithmetic mean mean = (previous_prime + next_prime) / 2 # If n is a strong prime if (n > mean):
# Driver code
if __name__ == ‘__main__’:
n = 11
# This code is contributed by
- Program to check Strong Number
- Quick ways to check for Prime and find next Prime in Java
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Check n^2 - m^2 is prime or not
- Check if N is a Factorial Prime
- Check if two numbers are co-prime or not
- Check whether the given number is Wagstaff prime or not
- Check if a number is Full Prime
- Check whether the given numbers are Cousin prime or not
- Check whether a number is circular prime or not
- Check if a number is Quartan Prime or not
- Check if a number is a Pythagorean Prime or not
- C Program to Check Whether a Number is Prime or not
- Check if a number is Primorial Prime or not