Python program to check for Moran numbers

Python program to check for Moran numbers:

In this post, we will learn how to find if a number is a Moran number or not in Python. This program will take one number as an input from the user, check if it is a Moran number and print the result.

A number is called a Moran number if the result of dividing the number by its sum of digits gives a prime number.

Example 1: Python program to check if a user input number is a Moran number or not:

This program will take one number as an input from the user, check if it is a Moran number or not and print one message to the user.

def is_prime(no):
    for i in range(2, no):
        if no % i == 0:
            return False
    return True


def sum_digits(no):
    sum = 0
    while(no):
        sum += no % 10
        no //= 10
    return sum


def is_moran(no):
    sum = sum_digits(no)
    if(no % sum == 0):
        q = no//sum
        return is_prime(q)
    return False


n = int(input('Enter the number: '))
if(is_moran(n)):
    print(f'{n} is a Moran number')
else:
    print(f'{n} is not a Moran number')

Here,

  • This program asks the user to enter a number. It reads the number and assigns it to the variable n.
  • It has three different methods.
    • The is_prime method checks if a number is a prime number or not. It takes one number as its parameter and returns one boolean value.
    • The sum_digits method finds the sum of all digits of a given number. It takes one number as its parameter and returns the sum of the digits of the number.
    • The is_moran method checks if a number is a Moran number or not. It finds the sum of all the digits of the number by using the sum_digits method. If the number is divisible by the sum, it finds the quotient and checks if the quotient is a prime number or not. If the number is not divisible by the sum, it returns False.
    • To check for prime, it uses the is_prime method.

If you run this program, it will print the below output:

Enter the number: 122
122 is not a Moran number

Enter the number: 123
123 is not a Moran number

Enter the number: 45
45 is a Moran number

Python example program to check for Moran number

Example 2: Python program to find all Moran numbers in a range:

In this example, we will find all Moran numbers in a given range. The program will take the range values as inputs from the user and print all Moran numbers in that range. We can use the same methods we used in the previous example to write this program.

def is_prime(no):
    for i in range(2, no):
        if no % i == 0:
            return False
    return True


def sum_digits(no):
    sum = 0
    while(no):
        sum += no % 10
        no //= 10
    return sum


def is_moran(no):
    sum = sum_digits(no)
    if(no % sum == 0):
        q = no//sum
        return is_prime(q)
    return False


start = int(input('Enter the start position: '))
end = int(input('Enter the end position: '))

print(f'Moran numbers in between {start} and {end - 1} are: ')
for i in range(start, end):
    if(is_moran(i)):
        print(i, end=' ')

This program reads the start and the end positions as inputs from the user to find the Moran numbers and uses a for loop in that range. It uses the is_moran method to find all Moran numbers in the range, i.e. from start to end - 1.

If you run this program, it will print output as below:

Enter the start position: 1
Enter the end position: 100
Moran numbers in between 1 and 99 are: 
1 2 3 4 5 6 7 8 9 18 21 27 42 45 63 84

You might also like: