Retour aux défis
Facile 0 résolus 0 % de réussite

Armstrong Number

Check whether a number equals the sum of its digits raised to the digit count.

Ce que vous allez apprendre

  • Count the digits of a number
  • Raise each digit to the digit count and add them up
  • Compare the result with the original number

Problème

Afficher la traduction

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

Exemple 1
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.

Exemple 2
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.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.