public static bool IsNarcissistic(int number)
{
var sum = 0;
var temp = number;
var numberOfDigits = 0;
while (temp != 0)
{
numberOfDigits++;
temp /= 10;
}
temp = number;
while (number > 0)
{
var remainder = number % 10;
var power = (int)Math.Pow(remainder, numberOfDigits);
sum += power;
number /= 10;
}
return sum == temp;
}
//153 = 1^3 + 5^3 + 3^3 = 1+125+27 = 153
//the above line shows the meaning of narcissistic,meaning sum of power of number should be
//equal to that number 153===153
const isNarcissistic = (num) => {
let m = 1, count = 0;
while(num / m > 1){
m *= 10;
count++;
};
let sum = 0, temp = num;
while(temp){
sum += Math.pow(temp % 10, count);
temp = Math.floor(temp / 10);
};
return sum === num;
};
Code Example |
---|
Csharp :: hashtable in c# |
:: |
Csharp :: |
Csharp :: |
:: |
Csharp :: static initializer |
Csharp :: |
Csharp :: |
Csharp :: |
:: Triangle perimeter |
Csharp :: |
:: |
:: |
:: |
:: |
Csharp :: |
:: |
Csharp :: read only variable in c# |
:: |
:: html inside razor |
Csharp :: how to assign 2d physics material through script |
:: |
Csharp :: |
Csharp :: mono cast |
:: |
:: |
:: |
Csharp :: binary search between two indexes |
:: |
Csharp :: |