Skip to content

Instantly share code, notes, and snippets.

@lhuria94
Created January 2, 2018 13:16
Show Gist options
  • Save lhuria94/ac0f6fdae42d5ac7fe82dbbb8919bd38 to your computer and use it in GitHub Desktop.
Save lhuria94/ac0f6fdae42d5ac7fe82dbbb8919bd38 to your computer and use it in GitHub Desktop.
A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits.
// For example, take 153 (3 digits):
// 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
// and 1634 (4 digits):
// 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634
// The Challenge:
// Your code must return true or false depending upon whether the given number is a Narcissistic number.
// Error checking for text strings or other invalid inputs is not required, only valid integers will be passed into the function.
function narcissistic( value ) {
var getPow = value.toString().split('').map(function(x) {
return Math.pow(x, value.toString().length);
});
var getAccumValue = getPow.reduce(function(a, c) {
return a + c;
});
// Passing result.
return getAccumValue === value;
}
@Finyasy
Copy link

Finyasy commented Jun 11, 2019

def narcissistic( value ):
# Code away
total=[]
number=len(str(value))
power=int(number)
digits=[int(x) for x in str(value)]
for i in digits:
product=i**power
total.append(product)
sumtotal=sum(total);
if (sumtotal==value):
print (str(value)+"is a narcisstic number")
else:
print (str(value)+"is not a narcisstic number")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment