Check if N is Strong Prime in Python

A strong prime is a prime number that is greater than the arithmetic mean of its nearest prime neighbors. In other words, if we have a prime number p, and its previous prime is p1 and next prime is p2, then p is strong if p > (p1 + p2) / 2.

For example, if we have num = 37, the nearest prime numbers are 31 and 41. The average is (31 + 41) / 2 = 36, and since 37 > 36, it is a strong prime.

Algorithm Steps

To check if a number is a strong prime, follow these steps:

  • If the number is not prime or equals 2, return False
  • Find the previous prime number (smaller than the given number)
  • Find the next prime number (greater than the given number)
  • Calculate the average of these two neighboring primes
  • If the given number is greater than this average, it's a strong prime

Implementation

Here's the complete implementation to check if a number is a strong prime:

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

# Test the function
num = 37
result = solve(num)
print(f"Is {num} a strong prime? {result}")

The output of the above code is:

Is 37 a strong prime? True

Testing with Multiple Examples

Let's test our function with several numbers to see which ones are strong primes:

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

# Test with multiple numbers
test_numbers = [11, 17, 29, 37, 41, 61, 67]

for num in test_numbers:
    if isPrime(num):
        result = solve(num)
        print(f"{num}: Strong Prime = {result}")
    else:
        print(f"{num}: Not a prime number")

The output shows which prime numbers are strong primes:

11: Strong Prime = True
17: Strong Prime = True
29: Strong Prime = True
37: Strong Prime = True
41: Strong Prime = False
61: Strong Prime = True
67: Strong Prime = True

How It Works

For the number 37:

  • Previous prime: 31 (checking 36, 35, 34, 33, 32, 31)
  • Next prime: 41 (checking 38, 39, 40, 41)
  • Average: (31 + 41) / 2 = 36
  • Since 37 > 36, it's a strong prime

Conclusion

A strong prime is identified by comparing it with the average of its neighboring primes. The algorithm first ensures the number is prime, then finds its prime neighbors and checks if it exceeds their average.

Updated on: 2026-03-25T15:21:00+05:30

757 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements