Moran Numbers in Java
In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers.
Moran Number
A moran numbers are numbers that produces a prime number when its digits are added together and divided. Keep in mind that the number's factor should equal the sum of its digits.
Let us discuss an example to make it much clear.
Example1:
Consider a number 18.
The digits of 18 are 1 and 8. Their sum would be 1+8=9, and when 18 is divided by 9 that is
18/9, we get the result as 2. Hence 2 is a prime number that implies 18 is a moran number.
Example 2:
Now consider another integer 22.
The digits of 22 are 2 and 2 again. Their sum would be 2+2=4, and when divided by 4 that is,
22/4, we get the result as 5.5. Since 5.5 is not a prime number, it can be concluded that 22 cannot be a moran number.
For checking if the numbers is a moran number, there is only one approach and that is the same approach we discussed the above examples.
Approach
- Calculate the number n's digits added together.
- Verify whether or not the factor of a number n is represented by the summation of the digits.
- We can conclude that perhaps the number n cannot be a Moran number if the factor is not the sum of both the digits.
- If the factor is the addition of the digits, we then determine whether or not the result of dividing the original number by the sum of digits is a prime number.
- The provided number has become a Moran number if the result of the division is really a prime number; otherwise, it is not.
Let us discuss an example program for verifying if a number is a moran number or not.
File name: Moran.java
public class MoranNum
{
// a method that computes the sum of the digits of the number
// num
public int findSumDigits(int num)
{
int sum = 0;
while(num != 0)
{
sum = sum + (num % 10);
num = num / 10;
}
return sum;
}
// a method that checks whether the number num
// is a prime number or not
public boolean isPrime(int num)
{
for(int i = 2; i <= (int)Math.sqrt(num); i++)
{
if((num % i) == 0)
{
return false;
}
}
return true;
}
// a method that returns true if the number n
// is a moran number
public boolean isMoran(int n)
{
int sumDig = findSumDigits(n);
// if number n is not completely divided by the
// sumDig, then n can never be a moran number
if((n % sumDig) != 0)
{
return false;
}
// reaching here means sumDig a factor of
// the number n. Now we find the quotient or
// or answer of the division
int div = n / sumDig;
// if the answer of the division is a prime
// number, then n is a moran number;
if(isPrime(div))
{
return true;
}
// reaching here means div is not a prime number
// hence, n is not a moran number
return false;
}
//This is the main method
public static void main(String[] argvs)
{
//Defining an object for the class Moran
MoranNum obj = new MoranNum();
// Considering the 1st output
int num = 45;
boolean ans = obj.isMoran(num);
if(ans)
{
System.out.println("The number " + num + " is a Moran number.");
}
else
{
System.out.println("The number " + num + " is not a Moran number.");
}
System.out.println();
// Considering the 2nd input
num = 36;
ans = obj.isMoran(num);
if(ans)
{
System.out.println("The number " + num + " is a Moran number.");
}
else
{
System.out.println("The number " + num + " is not a Moran number.");
}
System.out.println();
// Considering the 3rd input
num = 22;
ans = obj.isMoran(num);
if(ans)
{
System.out.println("The number " + num + " is a Moran number.");
}
else
{
System.out.println("The number " + num + " is not a Moran number.");
}
}
}
Output
The number 18 is a Moran number.
The number 56 is not a Moran number.
The number 22 is not a Moran number.