• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Check For Moran Number In Python

Check For Moran Number In Python

Author: Aditya Raj
Last Updated: January 9, 2022

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.

  1. Calculate the sum of the digits of the given number.
  2. Divide the number by the sum of digits.
  3. 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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us