Given N, check whether it is a Narcissistic number or not.
Note:Narcissistic Number is a number that is the sum of its own digits each raised to the power of the number of digits
Examples :
Input : 153
Output : yes
Explanation: 1^3+5^3+3^3=153
Input : 1634
Output : yes
Explanation: 1^4+6^4+3^4+4^4=1634
The approach will be to count the number of digits and then extract every digit and then by using pow function we can get the power of that digit and then sum it up at the end and compare with the original number to check if it is a Narcissistic Number or not.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int countDigit( int n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
bool check( int n)
{
int l = countDigit(n);
int dup = n;
int sum = 0;
while (dup)
{
sum += pow (dup % 10, l);
dup /= 10;
}
return (n == sum);
}
int main()
{
int n = 1634;
if (check(n))
cout << "yes" ;
else
cout << "no" ;
return 0;
}
|
Java
import java.io.*;
import static java.lang.Math.*;
class narcissistic
{
int countDigit( int n)
{
if (n == 0 )
return 0 ;
return 1 + countDigit(n / 10 );
}
boolean check( int n)
{
int l = countDigit(n);
int dup = n;
int sum = 0 ;
while (dup > 0 )
{
sum += pow(dup % 10 , l);
dup /= 10 ;
}
return (n == sum);
}
public static void main(String args[])
{
narcissistic obj = new narcissistic();
int n = 1634 ;
if (obj.check(n))
System.out.println( "yes" );
else
System.out.println( "no" );
}
}
|
Python3
def countDigit(n) :
if (n = = 0 ) :
return 0
return ( 1 + countDigit(n / / 10 ))
def check(n) :
l = countDigit(n)
dup = n; sm = 0
while (dup) :
sm = sm + pow (dup % 10 , l)
dup = dup / / 10
return (n = = sm)
n = 1634
if (check(n)) :
print ( "yes" )
else :
print ( "no" )
|
C#
using System;
class narcissistic
{
int countDigit( int n)
{
if (n == 0)
return 0;
return 1 + countDigit(n / 10);
}
bool check( int n)
{
int l = countDigit(n);
int dup = n;
int sum = 0;
while (dup > 0)
{
sum += ( int )Math.Pow(dup % 10, l);
dup /= 10;
}
return (n == sum);
}
public static void Main()
{
narcissistic obj = new narcissistic();
int n = 1634;
if (obj.check(n))
Console.WriteLine( "yes" );
else
Console.WriteLine( "no" );
}
}
|
PHP
<?php
function countDigit( $n )
{
if ( $n == 0)
return 0;
return (1 + countDigit( $n / 10));
}
function check( $n )
{
$l = countDigit( $n );
$dup = $n ;
$sum = 0;
while ( $dup )
{
$sum += pow( $dup % 10, $l );
$dup = (int) $dup / 10;
}
return ( $n == $sum );
}
$n = 1634;
if (check(! $n ))
echo "yes" ;
else
echo "no" ;
?>
|
Javascript
<script>
function countDigit(n)
{
if (n == 0)
return 0;
return (1 + countDigit(n / 10));
}
function check( n)
{
let l = countDigit(n);
let dup = n;
let sum = 0;
while (dup)
{
sum += Math.pow(dup % 10, l);
dup = parseINT(dup / 10);
}
return (n == sum);
}
let n = 1634;
if (check(!n))
document.write( "yes" );
else
document.write( "no" );
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Method 2: Simplified Method using string
We have to take input as a string and traverse through string computing power of each character with the length of the string. Note: Here length of the string gives the number of digits of that number
Below is the implementation of the above approach
C++14
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
string getResult(string st)
{
int sum = 0;
int length = st.length();
for ( int i = 0; i < length; i++)
{
sum = sum + pow (st[i] - '0' , length);
}
int number = stoi(st);
if (number == sum)
return "yes" ;
else
return "no" ;
}
int main()
{
string st = "153" ;
cout << getResult(st);
return 0;
}
|
Java
class GFG
{
static String getResult(String st)
{
int sum = 0 ;
int length = st.length();
for ( int i = 0 ; i < length; i++)
{
sum = sum + ( int )Math.pow(st.charAt(i) - '0' , length);
}
int number = Integer.parseInt(st);
if (number == sum)
return "yes" ;
else
return "no" ;
}
public static void main(String []args)
{
String st = "153" ;
System.out.print(getResult(st));
}
}
|
Python3
def getResult(st):
sum = 0
length = len (st)
for i in st:
sum = sum + int (i) * * length
number = int (st)
if (number = = sum ):
return "true"
else :
return "false"
st = "153"
print (getResult(st))
|
C#
using System;
class GFG
{
static string getResult( string st)
{
int sum = 0;
int length = st.Length;
for ( int i = 0; i < length; i++)
{
sum = sum + ( int )Math.Pow(st[i] - '0' , length);
}
int number = int .Parse(st);
if (number == sum)
return "yes" ;
else
return "no" ;
}
public static void Main( string []args)
{
string st = "153" ;
Console.Write(getResult(st));
}
}
|
Javascript
<script>
function getResult(st)
{
let sum = 0;
let length = st.length;
for (let i = 0; i < length; i++)
{
sum = sum + Math.pow(st[i] - '0' , length);
}
let number = parseInt(st, 10);
if (number == sum)
return "yes" ;
else
return "no" ;
}
let st = "153" ;
document.write(getResult(st));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
References: http://mathandmultimedia.com/2012/01/16/narcissistic-numbers/
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Jul, 2022
Like Article
Save Article