Problème
A number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number. For example 153 = 1³ + 5³ + 3³.
Implement is_armstrong(n).
Input
n: A non-negative integer.
Output
Return true if n is an Armstrong number.
Contraintes
0 ≤ n ≤ 10¹⁸
Exemples
Entrée :
n = 153
Sortie attendue :
expected = True
Entrée :
long long n = 153;
Sortie attendue :
bool expected = true;
Entrée :
long n = 153;
Sortie attendue :
bool expected = true;
Explication
1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
Entrée :
n = 10
Sortie attendue :
expected = False
Entrée :
long long n = 10;
Sortie attendue :
bool expected = false;
Entrée :
long n = 10;
Sortie attendue :
bool expected = false;
Explication
1² + 0² = 1, which is not 10.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Count how many digits the number has before summing powers.
Indice 2
Compare the original number with the sum of each digit raised to that digit count.
Indice 3
Remember that 0 is a special one-digit case.
Complexité attendue
O(k) time, O(1) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.