Python Program to check whether given number is a Moran Number or not
In this article, we will learn how to check whether the given number n is a Moran Number in Python. A number is said to be Moran Number if it is divisible by the sum of its digits gives a prime number. Moran numbers are a subset of Harshad Numbers. For knowing more about Harshad Number click here.
Example
Input: 21 Output: It is a Moran Number. Explanation: The given input is divisible by its sum of the digits (i.e. 2+1 = 3) and the ouput(i.e. 21/3 = 7) is prime. Input: 61 Output: It is a not Moran Number. Explanation: The given input is not divisible by its sum of the digits (i.e. 6+1 = 7).
Moran Number
1. Get the input.
2. Call the moran_number function with input as an argument.
3. Create a digits_sum function that returns the sum of the digits.
4. Create a function is_prime that checks the given number is prime or not.
5. Check the given number n is divisible by the sum of the digits of n.
6. Now, call the is_prime the quotient as an argument that returns True, if the given number is prime else return False.
def digits_sum(x): temp_sum = 0 while(x): temp_sum += x%10 x = x//10 return temp_sum def is_prime(x): i = 2 res = True while (i*i<=x): if(x%i == 0): res = False break i += 1 return res def moran_number(n): temp = n temp_sum = digits_sum(temp) if(n%temp_sum == 0): quot = n//temp_sum if(is_prime(quot)): print("The number is Moran Number") return print("The number is not Moran Number") n = int(input("Enter the number n: ")) moran_number(n)
Output
Enter the number n: 21 The number is Moran Number Enter the number n: 34 The number is not Moran Number Enter the number n: 45 The number is Moran Number
Leave a Reply