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 is a number that is the sum of its own digits each raised to the power of the number of digits.
Definition
Let be a natural number. We define the narcissistic function for base to be the following:
where is the number of digits in the number in base , and
is the value of each digit of the number. A natural number is a narcissistic number if it is a fixed point for , which occurs if . The natural numbers are trivial narcissistic numbers for all , all other narcissistic numbers are nontrivial narcissistic numbers.
For example, the number 153 in base is a narcissistic number, because and .
A natural number is a sociable narcissistic number if it is a periodic point for , where for a positive integer (here is the th iterate of ), and forms a cycle of period . A narcissistic number is a sociable narcissistic number with , and an amicable narcissistic number is a sociable narcissistic number with .
All natural numbers are preperiodic points for , regardless of the base. This is because for any given digit count , the minimum possible value of is , the maximum possible value of is , and the narcissistic function value is . Thus, any narcissistic number must satisfy the inequality . Multiplying all sides by , we get , or equivalently, . Since , this means that there will be a maximum value where , because of the exponential nature of and the linearity of . Beyond this value , 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 , making it a preperiodic point. Setting equal to 10 shows that the largest narcissistic number in base 10 must be less than .[1]
The number of iterations needed for to reach a fixed point is the narcissistic function's persistence of , and undefined if it never reaches a fixed point.
A base has at least one two-digit narcissistic number if and only if is not prime, and the number of two-digit narcissistic numbers in base equals , where is the number of positive divisors of .
Every base that is not a multiple of nine has at least one three-digit narcissistic number. The bases that do not are
The following Java program determines whether the integer entered is a Narcissistic / Armstrong number or not.
importjava.util.Scanner;publicclassArmstrongNumber{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);System.out.println("Enter a positive integer: ");intnumber=in.nextInt();if(isArmstrongNumber(number)){System.out.println(number+" is an Armstrong number.");}else{System.out.println(number+" is not an Armstrong number.");}}publicstaticbooleanisArmstrongNumber(intnumber){intsum=0;StringnumberString=Integer.toString(number);intnumberOfDigits=numberString.length();for(inti=0;i<numberOfDigits;i++){intdigit=Character.getNumericValue(numberString.charAt(i));sum+=Math.pow(digit,numberOfDigits);}returnsum==number;}}
C#
The following C# program determines whether the integer entered is a Narcissistic / Armstrong number or not.
usingSystem;publicclassProgram{publicstaticvoidMain(){Console.WriteLine("Enter the number:");intvalue=int.Parse(Console.ReadLine());if(value==RequiredSum(value)){Console.WriteLine("Armstrong Number");}else{Console.WriteLine("Not an Armstrong Number");}}privatestaticintCountDigits(intnum){inti=0;for(;num>0;++i)num/=10;returni;}privatestaticintRequiredSum(intnum){intcount=CountDigits(num);intsum=0;while(num>0){sum+=(int)Math.Pow(num%10,count);num/=10;}returnsum;}}
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>intgetNumberOfDigits(intn);boolisArmstrongNumber(intcandidate);intmain(){intuserNumber=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");return0;}boolisArmstrongNumber(intcandidate){intnumberOfDigits=getNumberOfDigits(candidate);intsum=0;for(inti=candidate;i!=0;i/=10){intnum=i%10;intn=1;for(intj=0;j<numberOfDigits;j++){n*=num;}sum+=n;}returnsum==candidate;}intgetNumberOfDigits(intn){intsum=0;while(n!=0){n/=10;++sum;}returnsum;}
C++
The following C++ program determines whether the Integer entered is a Narcissistic / Armstrong number or not.
#include<iostream>#include<cmath>boolisArmstrong(intn){//The floor function is redundant because log10(n) + 1 will always be an integer when n is positive. Simply using static_cast<int>(log10(n)) + 1 would suffice.//int digits = floor(log10(n) + 1); //math formula to find number of digits in a number with any baseintsum=0;if(n>=0){intdigits=static_cast<int>(log10(n))+1;for(inttmp=n;tmp;tmp/=10)sum+=pow(tmp%10,digits);}returnsum==n;}intmain(){intn=407;if(isArmstrong(n))std::cout<<n<<" is a narcissistic number\n";elsestd::cout<<n<<" is not a narcissistic number\n";}
Ruby
The following Ruby program determines whether the integer entered is a Narcissistic / Armstrong number or not.