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(
      function( $number ) use ( $digits ) {
        return pow( $number, $digits );
      str_split( $value )
  ) === $value;

December 6, 2020