3-digit Osiris number
Last Updated :
31 May, 2021
Given a 3-digit number N, the task is to find if N is an Osiris number or not. Osiris numbers are the numbers that are equal to the sum of permutations of sub-samples of their own digits. For example, 132 is an Osiris number as it is equal to 12 + 21 + 13 + 31 + 23 + 32.
Examples:
Input: N = 132
Output: Yes
12 + 21 + 13 + 31 + 23 + 32 = 132
Input: N = 154
Output: No
Approach:
If n = 132,
132 = 12 + 21 + 13 + 31 + 23 + 32
132 = 2 * 11 + 2 * 22 + 2 * 33
132 = 22 + 44 + 66
132 = (2 + 4 + 6) * 11
132 = 2 * (1 + 2 + 3) * 11, each digit of 132 occurs twice in the ones and tens position of the sums.
The same rule applies for every 3-digit Osiris number and can be reciprocated to check whether a number is an Osiris number or not.
For a 3-digit number N to be considered as an Osiris number, N must be equal to 2 * (sum of digits) * 11
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isOsiris( int n)
{
int a = n % 10;
int b = (n / 10) % 10;
int c = n / 100;
int digit_sum = a + b + c;
if (n == (2 * (digit_sum)*11)) {
return true ;
}
return false ;
}
int main()
{
int n = 132;
if (isOsiris(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isOsiris( int n)
{
int a = n % 10 ;
int b = (n / 10 ) % 10 ;
int c = n / 100 ;
int digit_sum = a + b + c;
if (n == ( 2 * (digit_sum)* 11 ))
{
return true ;
}
return false ;
}
public static void main(String args[])
{
int n = 132 ;
if (isOsiris(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isOsiris(n):
a = n % 10
b = (n / / 10 ) % 10
c = n / / 100
digit_sum = a + b + c
if (n = = ( 2 * (digit_sum) * 11 )):
return True
return False
if __name__ = = '__main__' :
n = 132
if isOsiris(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isOsiris( int n)
{
int a = n % 10;
int b = (n / 10) % 10;
int c = n / 100;
int digit_sum = a + b + c;
if (n == (2 * (digit_sum)*11))
{
return true ;
}
return false ;
}
static void Main()
{
int n = 132;
if (isOsiris(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isOsiris( $n )
{
$a = $n % 10;
$b = floor ( $n / 10) % 10;
$c = floor ( $n / 100);
$digit_sum = $a + $b + $c ;
if ( $n == (2 * ( $digit_sum ) * 11))
{
return true;
}
return false;
}
$n = 132;
if (isOsiris( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isOsiris(n)
{
let a = n % 10;
let b = parseInt((n / 10) % 10);
let c = parseInt(n / 100);
let digit_sum = a + b + c;
if (n == (2 * (digit_sum) * 11))
{
return true ;
}
return false ;
}
let n = 132;
if (isOsiris(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...