A Narcissistic Number
While solving this kata on CodeWars, I read about narcissistic numbers. It’s a quite fascinating concept.
Let’s hear what Wikipedia can tell us about it:
In number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong) or a plus perfect number) in a given number base B is a number that is the sum of its own digits each raised to the power of the number of digits.
The kata in question mentioned 153
as a narcissistic number, because:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
If you’re curious, here’s my solution:
function narcissistic(int $value): bool {
$digits = strlen( $value );
return array_sum(
array_map(
function( $number ) use ( $digits ) {
return pow( $number, $digits );
},
str_split( $value )
)
) === $value;
}
← Previous
Visual Studio Code keybindings
Next →
Going WordPress Again
Now what?
I’m genuinely thrilled that you’ve made it this far — thank you for reading!
If you’re up for more, here are a few things you can do:
- Reply to this post by email – I’d love to hear from you.
- Subscribe via RSS or email to stay in the loop.
- Reach out on Mastodon – I’m always happy to chat.