Narcissistic number

Integer expressible as the sum of its own digits each raised to the power of the number of digits

In number theory, a narcissistic number[1][2] (also known as a pluperfect digital invariant (PPDI),[3] an Armstrong number[4] (after Michael F. Armstrong)[5] or a plus perfect number)[6] in a given number base b {\displaystyle b} is a number that is the sum of its own digits each raised to the power of the number of digits.

Definition

Let n {\displaystyle n} be a natural number. We define the narcissistic function for base b > 1 {\displaystyle b>1} F b : N N {\displaystyle F_{b}:\mathbb {N} \rightarrow \mathbb {N} } to be the following:

F b ( n ) = i = 0 k 1 d i k . {\displaystyle F_{b}(n)=\sum _{i=0}^{k-1}d_{i}^{k}.}

where k = log b n + 1 {\displaystyle k=\lfloor \log _{b}{n}\rfloor +1} is the number of digits in the number in base b {\displaystyle b} , and

d i = n mod b i + 1 n mod b i b i {\displaystyle d_{i}={\frac {n{\bmod {b^{i+1}}}-n{\bmod {b}}^{i}}{b^{i}}}}

is the value of each digit of the number. A natural number n {\displaystyle n} is a narcissistic number if it is a fixed point for F b {\displaystyle F_{b}} , which occurs if F b ( n ) = n {\displaystyle F_{b}(n)=n} . The natural numbers 0 n < b {\displaystyle 0\leq n<b} are trivial narcissistic numbers for all b {\displaystyle b} , all other narcissistic numbers are nontrivial narcissistic numbers.

For example, the number 153 in base b = 10 {\displaystyle b=10} is a narcissistic number, because k = 3 {\displaystyle k=3} and 153 = 1 3 + 5 3 + 3 3 {\displaystyle 153=1^{3}+5^{3}+3^{3}} .

A natural number n {\displaystyle n} is a sociable narcissistic number if it is a periodic point for F b {\displaystyle F_{b}} , where F b p ( n ) = n {\displaystyle F_{b}^{p}(n)=n} for a positive integer p {\displaystyle p} (here F b p {\displaystyle F_{b}^{p}} is the p {\displaystyle p} th iterate of F b {\displaystyle F_{b}} ), and forms a cycle of period p {\displaystyle p} . A narcissistic number is a sociable narcissistic number with p = 1 {\displaystyle p=1} , and an amicable narcissistic number is a sociable narcissistic number with p = 2 {\displaystyle p=2} .

All natural numbers n {\displaystyle n} are preperiodic points for F b {\displaystyle F_{b}} , regardless of the base. This is because for any given digit count k {\displaystyle k} , the minimum possible value of n {\displaystyle n} is b k 1 {\displaystyle b^{k-1}} , the maximum possible value of n {\displaystyle n} is b k 1 b k {\displaystyle b^{k}-1\leq b^{k}} , and the narcissistic function value is F b ( n ) = k ( b 1 ) k {\displaystyle F_{b}(n)=k(b-1)^{k}} . Thus, any narcissistic number must satisfy the inequality b k 1 k ( b 1 ) k b k {\displaystyle b^{k-1}\leq k(b-1)^{k}\leq b^{k}} . Multiplying all sides by b ( b 1 ) k {\displaystyle {\frac {b}{(b-1)^{k}}}} , we get ( b b 1 ) k b k b ( b b 1 ) k {\displaystyle {\left({\frac {b}{b-1}}\right)}^{k}\leq bk\leq b{\left({\frac {b}{b-1}}\right)}^{k}} , or equivalently, k ( b b 1 ) k b k {\displaystyle k\leq {\left({\frac {b}{b-1}}\right)}^{k}\leq bk} . Since b b 1 1 {\displaystyle {\frac {b}{b-1}}\geq 1} , this means that there will be a maximum value k {\displaystyle k} where ( b b 1 ) k b k {\displaystyle {\left({\frac {b}{b-1}}\right)}^{k}\leq bk} , because of the exponential nature of ( b b 1 ) k {\displaystyle {\left({\frac {b}{b-1}}\right)}^{k}} and the linearity of b k {\displaystyle bk} . Beyond this value k {\displaystyle k} , F b ( n ) n {\displaystyle F_{b}(n)\leq n} always. Thus, there are a finite number of narcissistic numbers, and any natural number is guaranteed to reach a periodic point or a fixed point less than b k 1 {\displaystyle b^{k}-1} , making it a preperiodic point. Setting b {\displaystyle b} equal to 10 shows that the largest narcissistic number in base 10 must be less than 10 60 {\displaystyle 10^{60}} .[1]

The number of iterations i {\displaystyle i} needed for F b i ( n ) {\displaystyle F_{b}^{i}(n)} to reach a fixed point is the narcissistic function's persistence of n {\displaystyle n} , and undefined if it never reaches a fixed point.

A base b {\displaystyle b} has at least one two-digit narcissistic number if and only if b 2 + 1 {\displaystyle b^{2}+1} is not prime, and the number of two-digit narcissistic numbers in base b {\displaystyle b} equals τ ( b 2 + 1 ) 2 {\displaystyle \tau (b^{2}+1)-2} , where τ ( n ) {\displaystyle \tau (n)} is the number of positive divisors of n {\displaystyle n} .

Every base b 3 {\displaystyle b\geq 3} that is not a multiple of nine has at least one three-digit narcissistic number. The bases that do not are

2, 72, 90, 108, 153, 270, 423, 450, 531, 558, 630, 648, 738, 1044, 1098, 1125, 1224, 1242, 1287, 1440, 1503, 1566, 1611, 1620, 1800, 1935, ... (sequence A248970 in the OEIS)

There are only 88 narcissistic numbers in base 10, of which the largest is

115,132,219,018,763,992,565,095,597,973,971,522,401

with 39 digits.[1]

Narcissistic numbers and cycles of Fb for specific b

All numbers are represented in base b {\displaystyle b} . '#' is the length of each known finite sequence.

b {\displaystyle b} Narcissistic numbers # Cycles OEIS sequence(s)
2 0, 1 2 {\displaystyle \varnothing }
3 0, 1, 2, 12, 22, 122 6 {\displaystyle \varnothing }
4 0, 1, 2, 3, 130, 131, 203, 223, 313, 332, 1103, 3303 12 {\displaystyle \varnothing } A010344 and A010343
5 0, 1, 2, 3, 4, 23, 33, 103, 433, 2124, 2403, 3134, 124030, 124031, 242423, 434434444, ... 18

1234 → 2404 → 4103 → 2323 → 1234

3424 → 4414 → 11034 → 20034 → 20144 → 31311 → 3424

1044302 → 2110314 → 1044302

1043300 → 1131014 → 1043300

A010346
6 0, 1, 2, 3, 4, 5, 243, 514, 14340, 14341, 14432, 23520, 23521, 44405, 435152, 5435254, 12222215, 555435035 ... 31

44 → 52 → 45 → 105 → 330 → 130 → 44

13345 → 33244 → 15514 → 53404 → 41024 → 13345

14523 → 32253 → 25003 → 23424 → 14523

2245352 → 3431045 → 2245352

12444435 → 22045351 → 30145020 → 13531231 → 12444435

115531430 → 230104215 → 115531430

225435342 → 235501040 → 225435342

A010348
7 0, 1, 2, 3, 4, 5, 6, 13, 34, 44, 63, 250, 251, 305, 505, 12205, 12252, 13350, 13351, 15124, 36034, 205145, 1424553, 1433554, 3126542, 4355653, 6515652, 125543055, ... 60 A010350
8 0, 1, 2, 3, 4, 5, 6, 7, 24, 64, 134, 205, 463, 660, 661, 40663, 42710, 42711, 60007, 62047, 636703, 3352072, 3352272, ... 63 A010354 and A010351
9 0, 1, 2, 3, 4, 5, 6, 7, 8, 45, 55, 150, 151, 570, 571, 2446, 12036, 12336, 14462, 2225764, 6275850, 6275851, 12742452, ... 59 A010353
10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, ... 89 A005188
11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, 56, 66, 105, 307, 708, 966, A06, A64, 8009, 11720, 11721, 12470, ... 135 A0161948
12 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, 25, A5, 577, 668, A83, 14765, 938A4, 369862, A2394A, ... 88 A161949
13 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, 14, 36, 67, 77, A6, C4, 490, 491, 509, B85, 3964, 22593, 5B350, ... 202 A0161950
14 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, 136, 409, 74AB5, 153A632, ... 103 A0161951
15 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, 78, 88, C3A, D87, 1774, E819, E829, 7995C, 829BB, A36BC, ... 203 A0161952
16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 156, 173, 208, 248, 285, 4A5, 5B0, 5B1, 60B, 64B, 8C0, 8C1, 99A, AA9, AC3, CA8, E69, EA0, EA1, B8D2, 13579, 2B702, 2B722, 5A07C, 5A47C, C00E0, C00E1, C04E0, C04E1, C60E7, C64E7, C80E0, C80E1, C84E0, C84E1, ... 294 A161953

Extension to negative integers

Narcissistic numbers can be extended to the negative integers by use of a signed-digit representation to represent each integer.

Programming example

Python

The example below implements the narcissistic function described in the definition above to search for narcissistic functions and cycles in Python.

def ppdif(x, b):
    y = x
    digit_count = 0
    while y > 0:
        digit_count = digit_count + 1
        y = y // b
    total = 0
    while x > 0:
        total = total + pow(x % b, digit_count)
        x = x // b
    return total

def ppdif_cycle(x, b):
    seen = []
    while x not in seen:
        seen.append(x)
        x = ppdif(x, b)
    cycle = []
    while x not in cycle:
        cycle.append(x)
        x = ppdif(x, b)
    return cycle

The following Python program determines whether the integer entered is a Narcissistic / Armstrong number or not.

def no_of_digits(num):
    i = 0
    while num > 0:
        num //= 10
        i+=1

    return i

def required_sum(num):
    i = no_of_digits(num)
    s = 0
    
    while num > 0:
        digit = num % 10
        num //= 10
        s += pow(digit, i)
        
    return s

num = int(input("Enter number:"))
s = required_sum(num)
     
if s == num:
    print("Armstrong Number")
else:
    print("Not Armstrong Number")

Java

The following Java program determines whether the integer entered is a Narcissistic / Armstrong number or not.

import java.util.Scanner;

public class ArmstrongNumber {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a positive integer: ");
        int number = in.nextInt();

        if (isArmstrongNumber(number)) {
            System.out.println(number + " is an Armstrong number.");
        } else {
            System.out.println(number + " is not an Armstrong number.");
        }
    }

    public static boolean isArmstrongNumber(int number) {
        int sum = 0;
        String numberString = Integer.toString(number);
        int numberOfDigits = numberString.length();

        for (int i = 0; i < numberOfDigits; i++) {
            int digit = Character.getNumericValue(numberString.charAt(i));
            sum += Math.pow(digit, numberOfDigits);
        }

        return sum == number;
    }
}

C#

The following C# program determines whether the integer entered is a Narcissistic / Armstrong number or not.

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Enter the number:");
        int value = int.Parse(Console.ReadLine());

        if (value == RequiredSum(value))
        {
            Console.WriteLine("Armstrong Number");
        }
        else 
        {
            Console.WriteLine("Not an Armstrong Number");
        }
    }

    private static int CountDigits(int num)
    {
        int i = 0; 
        for (;num > 0; ++i) num /= 10;

        return i;    
    }

    private static int RequiredSum(int num)
    {
        int count = CountDigits(num);

        int sum = 0;
        while (num > 0)
        {
            sum += (int)Math.Pow(num % 10, count);
            num /= 10;
        }

        return sum;
    }
}

C

The following C program determines whether the integer entered is a Narcissistic / Armstrong number or not.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int getNumberOfDigits(int n);
bool isArmstrongNumber(int candidate);

int main()
{
    int userNumber = 0;
    printf("Enter a number to verify if it is an Armstrong number: ");
    scanf("%d", &userNumber);
    printf("Is %d an Armstrong number?: %s\n", userNumber,  isArmstrongNumber(userNumber) ? "true" : "false");
    return 0;
}

bool isArmstrongNumber(int candidate)
{
    int numberOfDigits = getNumberOfDigits(candidate);
    int sum = 0;
    for (int i = candidate; i != 0; i /= 10)
    {
	    int num = i % 10;
	    int n = 1;
	    for (int j = 0; j < numberOfDigits; j++)
		{
			n *= num;
		}
	    sum += n;
    }
    return sum == candidate;
}

int getNumberOfDigits(int n)
{
    int sum = 0;
    while (n != 0)
    {
        n /= 10;
        ++sum;
    }
    return sum;
}

C++

The following C++ program determines whether the Integer entered is a Narcissistic / Armstrong number or not.

#include <iostream>
#include <cmath>

bool isNarcissistic(long n) {
    std::string s = std::to_string(n); // creating a string copy of n
    unsigned short l = s.length(); // getting the length of n
    unsigned short i = 0; long sum = 0; unsigned short base; // i will be used for iteration, sum is the sum of all of the digits to the power of the length of the number, and the base is the nth digit of n
    while (i<l) { // iterating over every digit of n
        base = s.at(i) - 48; // initializing the base of the number and subtracting 48 because the ascii for 0 is 48 and the ascii for 9 is 57 so if we subtract 48 it will give a integer version of that character.
        sum += pow(base,l); // adding base^length to the sum variable
        i++; // incrementing the iterator
    }
    return (n==sum) ? true : false; // if the n is equal to the sum return true, else, return false
}
int main() {
    unsigned int candidate; // declaring the candidate variable
    std::cout << "Enter a number to verify whether or not it is a narcissistic number: "; // printing the prompt to gather the input for the candidate variable.
    std::cin >> candidate; // taking the user's input
    std::cout << (std::string)((isNarcissistic(candidate)) ? "Yes" : "No") << std::endl; // printing "Yes" if it is an armstrong number and printing "No" if it isn't
    return 0; // returning 0 for a success
}

Ruby

The following Ruby program determines whether the integer entered is a Narcissistic / Armstrong number or not.

def narcissistic?(value) #1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
  nvalue = []
  nnum = value.to_s
  nnum.each_char do |num|
    nvalue << num.to_i
    end
  sum = 0
  i = 0
  while sum <= value
    nsum = 0
    nvalue.each_with_index do |num,idx|
      nsum += num ** i
    end
    if nsum == value
      return true
    else
      i += 1
      sum += nsum
    end
  end
return false
end

JavaScript

The following JavaScript program determines whether the integer entered is a Narcissistic / Armstrong number or not.

function narcissistic(number) {
  const numString = number.toString();
    const numDigits = numString.length;
    let sum = 0;

    for (let digit of numString) {
      sum += Math.pow(parseInt(digit), numDigits);
    }

    return sum === number;
}

Rust

The following Rust program prints all the Narcissistic / Armstrong numbers from 0 to 100 millions in base 10.

fn is_armstrong_number(num: u64) -> bool {
    let digits = num.to_string();
    digits
        .chars()
        .map(|x| (x as u64 - 0x30).pow(digits.len() as u32))
        .sum::<u64>()
        == num
}

fn main() {
    (0..100_000_000).for_each(|n| {
        if is_armstrong_number(n) {
            println!("{n}")
        }
    })
}


See also

References

  1. ^ a b c Weisstein, Eric W. "Narcissistic Number". MathWorld.
  2. ^ Perfect and PluPerfect Digital Invariants Archived 2007-10-10 at the Wayback Machine by Scott Moore
  3. ^ PPDI (Armstrong) Numbers by Harvey Heinz
  4. ^ Armstrong Numbers by Dik T. Winter
  5. ^ Lionel Deimel’s Web Log
  6. ^ (sequence A005188 in the OEIS)
  • Joseph S. Madachy, Mathematics on Vacation, Thomas Nelson & Sons Ltd. 1966, pages 163-175.
  • Rose, Colin (2005), Radical narcissistic numbers, Journal of Recreational Mathematics, 33(4), 2004-2005, pages 250-254.
  • Perfect Digital Invariants by Walter Schneider

External links

  • Digital Invariants
  • Armstrong Numbers
  • Armstrong Numbers in base 2 to 16
  • Armstrong numbers between 1-999 calculator
  • Symonds, Ria. "153 and Narcissistic Numbers". Numberphile. Brady Haran. Archived from the original on 2021-12-19.
  • v
  • t
  • e
Classes of natural numbers
Of the form a × 2b ± 1
Other polynomial numbers
Recursively defined numbers
Possessing a specific set of other numbers
Expressible via specific sums
2-dimensional
centered
non-centered
3-dimensional
centered
non-centered
pyramidal
4-dimensional
non-centered
Combinatorial numbers
Divisor functions
Prime omega functions
Euler's totient function
Aliquot sequences
Primorial
Numeral system-dependent numbers
Arithmetic functions
and dynamics
Digit sum
Digit product
Coding-related
Other
P-adic numbers-related
Digit-composition related
Digit-permutation related
Divisor-related
Other
Generated via a sieve
  • Mathematics portal