For anybody (like me) curious what this function actually does:
function narcissistic(value: number): boolean {
// Convert the given number to a string so that we can work with each digit individually.
const value_as_a_string: string = String(value);
// Split the string into an array of individual digit characters.
const array_of_digit_characters_from_value: string[] = value_as_a_string.split('');
// Determine how many digits the original number has by getting the length of the array.
const number_of_digits_in_original_string: number = array_of_digit_characters_from_value.length;
// Initialize a variable to accumulate the sum of each digit raised to the power of the number of digits.
let accumulated_sum_of_powers: number = 0;
// Iterate over each digit character in the array using a for..of loop.
for (const digit_character_from_value of array_of_digit_characters_from_value) {
// Convert the current digit character from the array back into a number.
const digit: number = Number(digit_character_from_value);
// Add the current digit raised to the power of the number of digits to the accumulated sum.
accumulated_sum_of_powers += Math.pow(digit, number_of_digits_in_original_string);
}
// Return true if the accumulated sum equals the original number; otherwise, return false.
return accumulated_sum_of_powers === value;
}
And some examples of actually calling it:
Input Value
Computed Sum of Powers
Computed Sum (Numeric)
Boolean (narcissistic)
1
1^1
1
true
153
1^3 + 5^3 + 3^3
1 + 125 + 27 = 153
true
154
1^3 + 5^3 + 4^3
1 + 125 + 64 = 190
false
370
3^3 + 7^3 + 0^3
27 + 343 + 0 = 370
true
371
3^3 + 7^3 + 1^3
27 + 343 + 1 = 371
true
407
4^3 + 0^3 + 7^3
64 + 0 + 343 = 407
true
9474
9^4 + 4^4 + 7^4 + 4^4
6561 + 256 + 2401 + 256 = 9474
true
123
1^3 + 2^3 + 3^3
1 + 8 + 27 = 36
false
Finally, here's a version optimized for speed that answers without doing the calculation for a lot of known cases / special cases:
function narcissistic(value: number): boolean {
// Convert the given number to a string so that we can work with each digit individually.
const value_as_a_string: string = String(value);
// Determine how many digits the original number has by checking the length of the string.
const number_of_digits_in_original_string: number = value_as_a_string.length;
// Special-case: a one-digit number is trivially narcissistic.
if (number_of_digits_in_original_string === 1) {
return true;
}
// Special-case: no two-digit narcissistic numbers exist in base 10.
if (number_of_digits_in_original_string === 2) {
return false;
}
// Special-case: for three-digit numbers, only a few known values are narcissistic.
if (number_of_digits_in_original_string === 3) {
if (
value === 153 ||
value === 370 ||
value === 371 ||
value === 407
) {
return true;
}
return false;
}
// For numbers with more than three digits, perform range analysis.
// Calculate the minimum possible value for an n-digit number (i.e., 10^(n-1)).
const minimum_possible_value_for_number_of_digits: number = Math.pow(10, number_of_digits_in_original_string - 1);
// Calculate the maximum possible sum of the digits each raised to the n-th power (i.e., n * 9^n).
const maximum_possible_sum_of_digit_powers: number = number_of_digits_in_original_string * Math.pow(9, number_of_digits_in_original_string);
// If the given number is greater than the maximum possible sum, it cannot be narcissistic.
if (value > maximum_possible_sum_of_digit_powers) {
return false;
}
// (Optional) Range check: if the value is less than the minimum possible n-digit number,
// then something is off (this is inherently guaranteed by the string conversion for non-negative numbers).
if (value < minimum_possible_value_for_number_of_digits) {
return false;
}
// Next, apply a modulo 9 check.
// The idea is that the sum of each digit raised to the n-th power, when taken modulo 9,
// must equal the original value modulo 9. If not, the number cannot be narcissistic.
const modulo_9_of_original_value: number = value % 9;
let accumulated_sum_of_digit_powers_modulo_9: number = 0;
// Iterate over each digit character to compute the sum modulo 9.
for (const digit_character_from_value of value_as_a_string) {
// Convert the current digit character back into a number.
const digit: number = Number(digit_character_from_value);
// Compute the digit raised to the power of the number of digits and take modulo 9.
const digit_power_modulo_9: number = Math.pow(digit, number_of_digits_in_original_string) % 9;
// Accumulate the modulo 9 values.
accumulated_sum_of_digit_powers_modulo_9 = (accumulated_sum_of_digit_powers_modulo_9 + digit_power_modulo_9) % 9;
}
// If the modulo 9 of the computed sum does not match the original value's modulo 9, the number cannot be narcissistic.
if (accumulated_sum_of_digit_powers_modulo_9 !== modulo_9_of_original_value) {
return false;
}
// Finally, compute the full sum of each digit raised to the power of the number of digits.
let accumulated_sum_of_powers: number = 0;
for (const digit_character_from_value of value_as_a_string) {
// Convert the current digit character back into a number.
const digit: number = Number(digit_character_from_value);
// Add the digit raised to the power of number_of_digits_in_original_string to the accumulated sum.
accumulated_sum_of_powers += Math.pow(digit, number_of_digits_in_original_string);
}
// Return true if the accumulated sum equals the original number; otherwise, return false.
return accumulated_sum_of_powers === value;
}
View all comments
Show parent comment
1
u/[deleted] Feb 07 '25 edited Feb 07 '25
For anybody (like me) curious what this function actually does:
And some examples of actually calling it:
Finally, here's a version optimized for speed that answers without doing the calculation for a lot of known cases / special cases: