Narcissistic number

Narcissistic number
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 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 .
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
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, ...
There are only 89 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.
Narcissistic numbers and cycles of Fb for specific b
All numbers are represented in base . '#' is the length of each known finite sequence.
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 cycleThe 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
See also
Arithmetic dynamics
Dudeney number
Factorion
Happy number
Kaprekar's constant
Kaprekar number
Meertens number
Perfect digit-to-digit invariant
Perfect digital invariant
Sum-product number
References
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
Arithmetic dynamics
Base-dependent integer sequences