Given an integer N, check whether the given number is a Moran Number or not. Moran numbers are a subset of Harshad numbers.
A number N is a Moran number if N divided by the sum of its digits gives a prime number. For example some Moran numbers are 18, 21, 27, 42, 45 and so on.
Examples:
Input: N = 34
Output: No
Explanation:
34 is not a moran number because it is not completely divisible 7 (sum of its digits).
Input: N = 21
Output: Yes
Explanation:
21 is a moran number because 21 divided by the sum of its digits gives a prime number.
Approach: To solve the problem mentioned above we have to find the sum of digits of that number. Then find the quotient by dividing the number by the sum of its digits and check if the quotient is a prime then the given number is a Moran Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int digSum( int a)
{
int sum = 0;
while (a) {
sum += a % 10;
a = a / 10;
}
return sum;
}
bool isPrime( int r)
{
bool s = true ;
for ( int i = 2; i * i <= r; i++) {
if (r % i == 0) {
s = false ;
break ;
}
}
return s;
}
void moranNo( int n)
{
int dup = n;
int sum = digSum(dup);
if (n % sum == 0) {
int c = n / sum;
if (isPrime(c)) {
cout << "Yes" ;
return ;
}
}
cout << "No" << endl;
}
int main()
{
int n = 21;
moranNo(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int digSum( int a)
{
int sum = 0 ;
while (a != 0 )
{
sum += a % 10 ;
a = a / 10 ;
}
return sum;
}
static boolean isPrime( int r)
{
boolean s = true ;
for ( int i = 2 ; i * i <= r; i++)
{
if (r % i == 0 )
{
s = false ;
break ;
}
}
return s;
}
static void moranNo( int n)
{
int dup = n;
int sum = digSum(dup);
if (n % sum == 0 )
{
int c = n / sum;
if (isPrime(c))
{
System.out.println( "Yes" );
return ;
}
}
System.out.println( "No" );
}
public static void main(String[] args)
{
int n = 21 ;
moranNo(n);
}
}
|
Python3
def digSum(a):
_sum = 0
while (a):
_sum + = a % 10
a = a / / 10
return _sum
def isPrime(r):
s = True
i = 2
while i * i < = r:
if (r % i = = 0 ):
s = False
break
i + = 1
return s
def moranNo(n):
dup = n
_sum = digSum(dup)
if (n % _sum = = 0 ):
c = n / / _sum
if (isPrime(c)):
print ( "Yes" )
return
print ( "No" )
n = 21
moranNo(n)
|
C#
using System;
class GFG{
static int digSum( int a)
{
int sum = 0;
while (a != 0)
{
sum += a % 10;
a = a / 10;
}
return sum;
}
static bool isPrime( int r)
{
bool s = true ;
for ( int i = 2; i * i <= r; i++)
{
if (r % i == 0)
{
s = false ;
break ;
}
}
return s;
}
static void moranNo( int n)
{
int dup = n;
int sum = digSum(dup);
if (n % sum == 0)
{
int c = n / sum;
if (isPrime(c))
{
Console.Write( "Yes" );
return ;
}
}
Console.Write( "No" );
}
public static void Main()
{
int n = 21;
moranNo(n);
}
}
|
Javascript
<script>
function digSum(a)
{
let sum = 0;
while (a) {
sum += a % 10;
a = Math.floor(a / 10);
}
return sum;
}
function isPrime(r)
{
let s = true ;
for (let i = 2; i * i <= r; i++) {
if (r % i == 0) {
s = false ;
break ;
}
}
return s;
}
function moranNo(n)
{
let dup = n;
let sum = digSum(dup);
if (n % sum == 0) {
let c = n / sum;
if (isPrime(c)) {
document.write( "Yes" );
return ;
}
}
document.write( "No" + "<br>" );
}
let n = 21;
moranNo(n);
</script>
|
Time complexity: O(sqrt(n))
Auxiliary Space: O(1)