Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether N is a Dihedral Prime Number or not in Python
A Dihedral Prime Number is a number that remains prime when viewed on a 7-segment display in normal orientation and when rotated 180 degrees (upside down). The number must contain only digits that look the same when rotated: 0, 1, 2, 5, and 8.
For example, if we have n = 1181, it appears as 1881 when rotated upside down (since 2 becomes 5 and 5 becomes 2), and both numbers are prime.
Algorithm Steps
To check if a number is a dihedral prime ?
- Check if all digits are valid (0, 1, 2, 5, 8 only)
- Create the upside-down version by swapping 2?5
- Check if the original number is prime
- Check if the upside-down version is prime
- Check if the reverse is prime
- Check if the reverse of upside-down is prime
Implementation
def is_prime(n):
"""Check if a number is prime"""
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return True
def reverse(n):
"""Reverse the digits of a number"""
return int(str(n)[::-1])
def up_side_down(n):
"""Create upside-down version by swapping 2<->5"""
temp = n
total = 0
while temp > 0:
digit = temp % 10
if digit == 2:
digit = 5
elif digit == 5:
digit = 2
total = total * 10 + digit
temp //= 10
return total
def is_dihedral_prime(n):
"""Check if n is a dihedral prime number"""
# Check if all digits are valid for 7-segment display rotation
temp = n
while temp > 0:
digit = temp % 10
if digit in [3, 4, 6, 7, 9]:
return False
temp //= 10
# Check if all four variations are prime
upside_down_n = up_side_down(n)
reverse_n = reverse(n)
reverse_upside_down_n = reverse(upside_down_n)
return (is_prime(n) and
is_prime(upside_down_n) and
is_prime(reverse_n) and
is_prime(reverse_upside_down_n))
# Test the function
n = 1181
result = is_dihedral_prime(n)
print(f"Is {n} a dihedral prime? {result}")
# Test with a few more examples
test_cases = [11, 181, 1181]
for num in test_cases:
print(f"{num}: {is_dihedral_prime(num)}")
Is 1181 a dihedral prime? True 11: True 181: True 1181: True
How It Works
The algorithm first validates that the number contains only digits that remain recognizable when rotated 180° on a 7-segment display. These are: 0, 1, 2, 5, and 8. Digits 3, 4, 6, 7, and 9 would not be readable upside down.
Next, it creates the upside-down version by swapping digits 2 and 5, since these appear as each other when rotated. Finally, it checks that all four variations (original, upside-down, reverse, and reverse upside-down) are prime numbers.
Key Points
- Only digits 0, 1, 2, 5, 8 are valid for dihedral primes
- Digits 2 and 5 swap when rotated 180°
- All four number variations must be prime
- The algorithm handles both rotation and reflection
Conclusion
A dihedral prime must contain only 7-segment compatible digits and remain prime in all orientations. This creates a very restrictive set of numbers that are both mathematically interesting and visually symmetric.
