Numbers have magic in them. People have named different numbers based on their specialties. In this article, we will discuss what a Moran Number is. We will also implement a program in python to check if a number is a Moran number or not.
What Is A Moran Number?
A Moran number is a number, which when divided by the sum of its digits, gives a prime number as the result. In other words, if we take a Moran number, calculate the sum of its digits, and divide the number with the sum of the digits, the result will be a prime number. Moran numbers are a subset of Harshad numbers.
For example, 42 is a Moran number. If you calculate its sum of digits, it is 6. 42 divided by 6 gives the result 7 which is a prime number. Hence, 42 is a Moran number.
On the other hand, if we take 20, its sum of digits is 2. 20 divided by 2 gives the result 10 which is not a prime number. Hence, 20 is not a Moran number.
Check For Moran Number in Python
To check for a Moran number in Python, we will have to perform the following operation.
- Calculate the sum of the digits of the given number.
- Divide the number by the sum of digits.
- Check whether the result of division is a prime number.
Let us discuss how to find the sum of digits of the number first.
To find the sum of digits of the given number, we will divide the number by 10 until the number becomes 0. During each division, we will get the rightmost digit as a remainder. We will use the remainder to calculate the sum of digits by adding the remainders during each division as follows.
def calculate_sum_of_digits(N):
sumOfDigits = 0
while N > 0:
digit = N % 10
sumOfDigits = sumOfDigits + digit
N = N // 10
return sumOfDigits
After finding the sum of digits, you can divide the given number by the sum of digits to find the result. Now, we have to check if the number is a prime number or not. For this, we will divide the result by all the numbers from 2 to the square root of the result. If the result is divisible by any of the numbers in this range, the number will not be a prime number. The isPrime() function given below performs this operation. It takes a number as an input argument and returns True if the given number is a prime number. Otherwise, it returns False.
def isPrime(N):
count = 2
while count ** 2 <= N:
if N % count == 0:
return False
count = count + 1
return True
Program To Check For Moran Number in Python
After defining functions for calculating the sum of digits and checking for prime numbers, we can write a program to check for a Moran number in Python as follows.
def calculate_sum_of_digits(N):
sumOfDigits = 0
while N > 0:
digit = N % 10
sumOfDigits = sumOfDigits + digit
N = N // 10
return sumOfDigits
def isPrime(N):
count = 2
while count ** 2 <= N:
if N % count == 0:
return False
count = count + 1
return True
def check_for_moran_number(N):
sumOfDigits = calculate_sum_of_digits(N)
if N % sumOfDigits == 0:
result = N // sumOfDigits
return isPrime(result)
else:
return False
input_number = 42
output = check_for_moran_number(input_number)
print("{} is a Moran Number:{}".format(input_number, output))
input_number = 20
output = check_for_moran_number(input_number)
print("{} is a Moran Number:{}".format(input_number, output))
Output:
42 is a Moran Number:True
20 is a Moran Number:False
Conclusion
In this article, we have discussed what a Moran number is. We also discussed the steps to check for a Moran number and implemented it in Python. To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.