
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if N is Strong Prime in Python
Suppose we have a number n. We have to check whether n is a strong prime or not. As we know a number said to be strong prime when it is a prime number that is greater than the average of nearest prime numbers.
So, if the input is like num = 37, then the output will be True as nearest prime numbers are 31 and 41, the average is (31+41)/2 = 36. And 37 > 36.
To solve this, we will follow these steps −
- if num is not prime or num is 2, then
- return False
- last := num - 1, next := num + 1
- while next is not prime, do
- next := next + 1
- while last is not prime, do
- last := last - 1
- avg :=(last + next) / 2
- if num > avg, then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
def isPrime(num): if num > 1: for i in range(2,num): if num % i == 0: return False return True return False def solve(num): if isPrime(num) == False or num == 2: return False last = num - 1 next = num + 1 while isPrime(next) == False: next += 1 while isPrime(last) == False: last -= 1 avg = (last + next) / 2 if num > avg: return True return False num = 37 print(solve(num))
Input
37
Output
True
- Related Articles
- Check if N is a Factorial Prime in Python
- Python Program to Check if a Number is a Strong Number
- Check whether N is a Dihedral Prime Number or not in Python
- Check if a number is Primorial Prime or not in Python
- Check if difference of areas of two squares is prime in Python
- Python program to check if a number is Prime or not
- Check if a number is Full Prime in C++
- Check if product of array containing prime numbers is a perfect square in Python
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- Check if a number is Quartan Prime or not in C++
- Check if a number is Primorial Prime or not in C++
- Check if LCM of array elements is divisible by a prime number or not in Python
- Python Program to Check Prime Number
- Check if a number is a Pythagorean Prime or not in C++
- How To Check If The Number Is A Prime Number In Excel?

Advertisements