Javatpoint Logo
Javatpoint Logo

Moran Numbers in Java

In this tutorial, we will be discussing Moran numbers in Java. A moran number is a number, when divided by the sum of its digits, generates a prime number. Note that the sum of the digits of the number should be the factor of that number. Let's understand with the help of a few examples.

Example 1:

Input: 45

Output: 45 is a Moran number.

Explanation: The sum of the digits of the number 45 is 4 + 5 = 9, and when 45 is divided by 9, we get 45 / 9 = 5, and 5 is a prime number. Hence, 45 is a Moran number.

Example 2:

Input: 36

Output: 36 is not a Moran number.

Explanation: The sum of the digits of the number 36 is 3 + 6 = 9, and when 36 is divided by 9, we get 36 / 9 = 4, and 4 is not a prime number. Hence, 36 is not a Moran number.

Example 3:

Input: 22

Output: 22 is not a Moran number.

Explanation: The sum of the digits of the number 22 is 2 + 2 = 4, and 4 is not the factor of the number 22. Hence, 22 is not a Moran number.

Validation For the Moran Number

Observe the following algorithm.

Step 1: Take a number n.

Step 2: Compute the sum of the digits of the number n.

Step 3: Check whether the sum of the digits is the factor of the number n or not.

  • If the sum of the digit is not the factor, we can say that the number n is not the Moran number.
  • If the sum of the digit is the factor, then we check whether the answer that comes after dividing the number by the digits sum of the number is a prime number or not.
  • If the number that comes after the division is a prime number, then the given input number is a Moran number; otherwise, not.

Implementation

Observe the implementation of the above algorithm.

FileName: MoranNum.java

Output:

The number 45 is a Moran number.

The number 36 is not a Moran number.

The number 22 is not a Moran number.

Finding Moran Numbers Within A Range

In this approach, we will be using a sieve to find the moran numbers within the given range. We are using the sieve because we do not want to use a loop for doing the validation for a prime number. The rest of the other stuff remains the same as the above-written approach. Observe the program.

FileName: MoranNum1.java

Output:

The Moran numbers that lie within the range 1 to 200 are: 
18 21 27 42 45 63 84 111 114 117 133 152 153 156 171 190 195 198 






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA