Todos los desafíos
Fácil 3 resueltos 60.0% de aceptación

Armstrong Number

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

Lo que aprenderás

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

Problema

Ver traducción

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.

Restricciones

0 ≤ n ≤ 10¹⁸

Ejemplos

Ejemplo 1
Entrada:
n = 153

Salida esperada:
expected = True
Entrada:
long long n = 153;

Salida esperada:
bool expected = true;
Entrada:
long n = 153;

Salida esperada:
bool expected = true;

Explicación

1³ + 5³ + 3³ = 1 + 125 + 27 = 153.

Ejemplo 2
Entrada:
n = 10

Salida esperada:
expected = False
Entrada:
long long n = 10;

Salida esperada:
bool expected = false;
Entrada:
long n = 10;

Salida esperada:
bool expected = false;

Explicación

1² + 0² = 1, which is not 10.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

Count how many digits the number has before summing powers.

Pista 2

Compare the original number with the sum of each digit raised to that digit count.

Pista 3

Remember that 0 is a special one-digit case.

Complejidad esperada

O(k) time, O(1) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Clasificación

Clasificación completa

La mejor solución aprobada de cada programador. Las más rápidas, ligeras y mejor valoradas van primero.

# Programador Lenguaje Tiempo Memoria Revisión de IA Solución
1 HJhettory Jurash Python 960 ms - 4/5 Ver código
2 KKKaneki Ken Python 980 ms - 4/5 Ver código
3 PTphúc thịnh phạm C++ 4,121 ms - 3/5 Ver código

Aprende de otros

Soluciones de la comunidad

Alerta de spoiler: 3 soluciones completas abajo Intenta resolverlo tú mismo primero. Igual puedes ver el código de otros desarrolladores y las reseñas de IA. Inicia sesión y resuelve el reto para unirte a la discusión.