Check whether the given number is moran number or not
The Moran number problem involves determining whether a given number is a Moran number or not. A Moran number is a positive integer that meets a specific criterion involving its digits and prime factors. To check whether a number is a Moran number, we need to calculate the sum of its digits, and then check if the number is divisible by this sum. Additionally, the number divided by the sum of its digits should be a prime number.
Problem Statement
A Moran number is a positive integer where the following conditions hold:
- The number should be divisible by the sum of its digits.
- The number divided by the sum of its digits should be a prime number.
Example
Let's take the number 84 as an example to illustrate whether it's a Moran number or not.
- Sum of digits of 84: 8 + 4 = 12
- 84 is divisible by 12.
- 84 divided by 12 is 7, which is a prime number.
Since both conditions are satisfied, 84 is a Moran number.
Idea to Solve
To solve this problem, we need to implement two functions: one to check if a number is prime (isPrime
),
and another to check if a number is a Moran number (isMoranNo
). The isPrime
function will
determine whether a given number is prime or not using a primality testing algorithm. The isMoranNo
function will calculate the sum of digits of the number, check if the number is divisible by the sum, and then check
if the quotient is a prime number.
Standard Pseudocode
function isPrime(num)
if num is 2 or num is 3 or num is 5
return true
if num <= 1 or num is divisible by 2 or num is divisible by 3 or num is divisible by 5
return false
i = 11
while i squared is less than or equal to num
if num is divisible by i
return false
else if num is divisible by i + 2
return false
i = i + 6
return true
function isMoranNo(number)
n = number
sum = 0
while n is not 0
sum = sum + (n % 10)
n = n / 10
if number is divisible by sum and isPrime(number / sum)
return true
else
return false
Algorithm with Explanation
- Implement the
isPrime
function as described in the pseudocode. It checks for primality using trial division and skips multiples of 2 and 3. - Implement the
isMoranNo
function which calculates the sum of digits of the number and then checks the conditions for a Moran number:- Check if the number is divisible by the sum of its digits.
- Check if the quotient of number divided by the sum is a prime number.
- In the main function, call
isMoranNo
for each test case (numbers 27, 161, 111, 151, and 84). Print whether each number is a Moran number or not based on the returned value fromisMoranNo
.
Program Solution
// C program for
// Check whether the given number is moran number or not
#include <stdio.h>
// Check that whether given number is prime or not
int isPrime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
// Base case
return 1;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return 0;
}
int i = 11;
while ((i *i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return 0;
}
else if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return 0;
}
i = i + 6;
}
return 1;
}
void isMoranNo(int number)
{
int n = number;
int sum = 0;
// Sum of digit
while (n != 0)
{
sum = sum + (n % 10);
n = n / 10;
}
if ((number % sum) == 0 && isPrime(number / sum))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit sum is an prime number.
printf(" Number %d is moran number\n", number);
}
else
{
printf(" Number %d is not moran number\n", number);
}
}
int main(int argc, char
const *argv[])
{
// Test Cases
isMoranNo(27);
isMoranNo(161);
isMoranNo(111);
isMoranNo(151);
isMoranNo(84);
return 0;
}
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
/*
Java Program for
Check whether the given number is moran number or not
*/
public class MoranNumber
{
// Check that whether given number is prime or not
public boolean isPrime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
// Base case
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
public void isMoranNo(int number)
{
int n = number;
int sum = 0;
// Sum of digits in given number
while (n != 0)
{
sum = sum + (n % 10);
n = n / 10;
}
if ((number % sum) == 0 && isPrime(number / sum))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
System.out.println(" Number " + number + " is moran number");
}
else
{
System.out.println(" Number " + number + " is not moran number");
}
}
public static void main(String[] args)
{
MoranNumber task = new MoranNumber();
// Test Cases
task.isMoranNo(27);
task.isMoranNo(161);
task.isMoranNo(111);
task.isMoranNo(151);
task.isMoranNo(84);
}
}
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Check whether the given number is moran number or not
*/
class MoranNumber
{
public:
// Check that whether given number is prime or not
bool isPrime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
// Base case
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i *i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return false;
}
else
{
if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
}
i = i + 6;
}
return true;
}
void isMoranNo(int number)
{
int n = number;
int sum = 0;
// Sum of digits in given number
while (n != 0)
{
sum = sum + (n % 10);
n = n / 10;
}
if ((number % sum) == 0 && this->isPrime(number / sum))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
cout << " Number " << number << " is moran number" << endl;
}
else
{
cout << " Number " << number << " is not moran number" << endl;
}
}
};
int main()
{
MoranNumber *task = new MoranNumber();
// Test Cases
task->isMoranNo(27);
task->isMoranNo(161);
task->isMoranNo(111);
task->isMoranNo(151);
task->isMoranNo(84);
return 0;
}
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
// Include namespace system
using System;
/*
Csharp Program for
Check whether the given number is moran number or not
*/
public class MoranNumber
{
// Check that whether given number is prime or not
public Boolean isPrime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
// Base case
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return false;
}
else
{
if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
}
i = i + 6;
}
return true;
}
public void isMoranNo(int number)
{
int n = number;
int sum = 0;
// Sum of digits in given number
while (n != 0)
{
sum = sum + (n % 10);
n = n / 10;
}
if ((number % sum) == 0 && this.isPrime(number / sum))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
Console.WriteLine(" Number " + number + " is moran number");
}
else
{
Console.WriteLine(" Number " + number + " is not moran number");
}
}
public static void Main(String[] args)
{
MoranNumber task = new MoranNumber();
// Test Cases
task.isMoranNo(27);
task.isMoranNo(161);
task.isMoranNo(111);
task.isMoranNo(151);
task.isMoranNo(84);
}
}
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
<?php
/*
Php Program for
Check whether the given number is moran number or not
*/
class MoranNumber
{
// Check that whether given number is prime or not
public function isPrime($num)
{
if ($num == 2 || $num == 3 || $num == 5)
{
// Base case
return true;
}
if ($num <= 1 || ($num % 2 == 0) || ($num % 3 == 0) || ($num % 5 == 0))
{
return false;
}
$i = 11;
while (($i * $i) <= $num)
{
if ($num % $i == 0)
{
// When number is divisible of current i value
return false;
}
else
{
if ($num % ($i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
}
$i = $i + 6;
}
return true;
}
public function isMoranNo($number)
{
$n = $number;
$sum = 0;
// Sum of digits in given number
while ($n != 0)
{
$sum = $sum + ($n % 10);
$n = (int)($n / 10);
}
if (($number % $sum) == 0 && $this->isPrime((int)($number / $sum)))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
echo " Number ".$number.
" is moran number".
"\n";
}
else
{
echo " Number ".$number.
" is not moran number".
"\n";
}
}
}
function main()
{
$task = new MoranNumber();
// Test Cases
$task->isMoranNo(27);
$task->isMoranNo(161);
$task->isMoranNo(111);
$task->isMoranNo(151);
$task->isMoranNo(84);
}
main();
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
/*
Node JS Program for
Check whether the given number is moran number or not
*/
class MoranNumber
{
// Check that whether given number is prime or not
isPrime(num)
{
if (num == 2 || num == 3 || num == 5)
{
// Base case
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return false;
}
else
{
if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
}
i = i + 6;
}
return true;
}
isMoranNo(number)
{
var n = number;
var sum = 0;
// Sum of digits in given number
while (n != 0)
{
sum = sum + (n % 10);
n = parseInt(n / 10);
}
if ((number % sum) == 0 && this.isPrime(parseInt(number / sum)))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
console.log(" Number " + number + " is moran number");
}
else
{
console.log(" Number " + number + " is not moran number");
}
}
}
function main()
{
var task = new MoranNumber();
// Test Cases
task.isMoranNo(27);
task.isMoranNo(161);
task.isMoranNo(111);
task.isMoranNo(151);
task.isMoranNo(84);
}
main();
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
# Python 3 Program for
# Check whether the given number is moran number or not
class MoranNumber :
# Check that whether given number is prime or not
def isPrime(self, num) :
if (num == 2 or num == 3 or num == 5) :
# Base case
return True
if (num <= 1 or(num % 2 == 0) or(num % 3 == 0) or(num % 5 == 0)) :
return False
i = 11
while ((i * i) <= num) :
if (num % i == 0) :
# When number is divisible of current i value
return False
else :
if (num % (i + 2) == 0) :
# When number is divisible of current i + 2 value
return False
i = i + 6
return True
def isMoranNo(self, number) :
n = number
sum = 0
# Sum of digits in given number
while (n != 0) :
sum = sum + (n % 10)
n = int(n / 10)
if ((number % sum) == 0 and self.isPrime(int(number / sum))) :
# When sum of digits is divisible by number and its
# remainder is zero and number divisible by digit
# sum is an prime number.
print(" Number ", number ," is moran number")
else :
print(" Number ", number ," is not moran number")
def main() :
task = MoranNumber()
# Test Cases
task.isMoranNo(27)
task.isMoranNo(161)
task.isMoranNo(111)
task.isMoranNo(151)
task.isMoranNo(84)
if __name__ == "__main__": main()
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
# Ruby Program for
# Check whether the given number is moran number or not
class MoranNumber
# Check that whether given number is prime or not
def isPrime(num)
if (num == 2 || num == 3 || num == 5)
# Base case
return true
end
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
return false
end
i = 11
while ((i * i) <= num)
if (num % i == 0)
# When number is divisible of current i value
return false
else
if (num % (i + 2) == 0)
# When number is divisible of current i + 2 value
return false
end
end
i = i + 6
end
return true
end
def isMoranNo(number)
n = number
sum = 0
# Sum of digits in given number
while (n != 0)
sum = sum + (n % 10)
n = n / 10
end
if ((number % sum) == 0 && self.isPrime(number / sum))
# When sum of digits is divisible by number and its
# remainder is zero and number divisible by digit
# sum is an prime number.
print(" Number ", number ," is moran number", "\n")
else
print(" Number ", number ," is not moran number", "\n")
end
end
end
def main()
task = MoranNumber.new()
# Test Cases
task.isMoranNo(27)
task.isMoranNo(161)
task.isMoranNo(111)
task.isMoranNo(151)
task.isMoranNo(84)
end
main()
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
/*
Scala Program for
Check whether the given number is moran number or not
*/
class MoranNumber()
{
// Check that whether given number is prime or not
def isPrime(num: Int): Boolean = {
if (num == 2 || num == 3 || num == 5)
{
// Base case
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return false;
}
else
{
if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
}
i = i + 6;
}
return true;
}
def isMoranNo(number: Int): Unit = {
var n: Int = number;
var sum: Int = 0;
// Sum of digits in given number
while (n != 0)
{
sum = sum + (n % 10);
n = (n / 10).toInt;
}
if ((number % sum) == 0 && isPrime((number / sum).toInt))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
println(" Number " + number + " is moran number");
}
else
{
println(" Number " + number + " is not moran number");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MoranNumber = new MoranNumber();
// Test Cases
task.isMoranNo(27);
task.isMoranNo(161);
task.isMoranNo(111);
task.isMoranNo(151);
task.isMoranNo(84);
}
}
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
/*
Swift 4 Program for
Check whether the given number is moran number or not
*/
class MoranNumber
{
// Check that whether given number is prime or not
func isPrime(_ num: Int)->Bool
{
if (num == 2 || num == 3 || num == 5)
{
// Base case
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return false;
}
else
{
if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
}
i = i + 6;
}
return true;
}
func isMoranNo(_ number: Int)
{
var n: Int = number;
var sum: Int = 0;
// Sum of digits in given number
while (n != 0)
{
sum = sum + (n % 10);
n = n / 10;
}
if ((number % sum) == 0 && self.isPrime(number / sum))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
print(" Number ", number ," is moran number");
}
else
{
print(" Number ", number ," is not moran number");
}
}
}
func main()
{
let task: MoranNumber = MoranNumber();
// Test Cases
task.isMoranNo(27);
task.isMoranNo(161);
task.isMoranNo(111);
task.isMoranNo(151);
task.isMoranNo(84);
}
main();
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
/*
Kotlin Program for
Check whether the given number is moran number or not
*/
class MoranNumber
{
// Check that whether given number is prime or not
fun isPrime(num: Int): Boolean
{
if (num == 2 || num == 3 || num == 5)
{
// Base case
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
// When number is divisible of current i value
return false;
}
else
{
if (num % (i + 2) == 0)
{
// When number is divisible of current i + 2 value
return false;
}
}
i = i + 6;
}
return true;
}
fun isMoranNo(number: Int): Unit
{
var n: Int = number;
var sum: Int = 0;
while (n != 0)
{
sum = sum + (n % 10);
n = n / 10;
}
if ((number % sum) == 0 && this.isPrime(number / sum))
{
// When sum of digits is divisible by number and its
// remainder is zero and number divisible by digit
// sum is an prime number.
println(" Number " + number + " is moran number");
}
else
{
println(" Number " + number + " is not moran number");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: MoranNumber = MoranNumber();
// Test Cases
task.isMoranNo(27);
task.isMoranNo(161);
task.isMoranNo(111);
task.isMoranNo(151);
task.isMoranNo(84);
}
input
Number 27 is moran number
Number 161 is not moran number
Number 111 is moran number
Number 151 is not moran number
Number 84 is moran number
Time Complexity
- The
isPrime
function has a time complexity of approximately O(sqrt(n)), where n is the number being checked for primality. - The
isMoranNo
function has a time complexity of O(log10(n)) due to the digit sum calculation and a time complexity of approximately O(sqrt(n)) for theisPrime
check. - Overall, the time complexity of the code is dominated by the prime checking, making it approximately O(sqrt(n)) per test case.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment