Fácil 0 resolvidos 0% de aceitação

Armstrong Number

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

O que você vai aprender

  • 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 tradução

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.

Restrições

0 ≤ n ≤ 10¹⁸

Exemplos

Exemplo 1
Entrada:
n = 153

Saída esperada:
expected = True
Entrada:
long long n = 153;

Saída esperada:
bool expected = true;
Entrada:
long n = 153;

Saída esperada:
bool expected = true;

Explicação

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

Exemplo 2
Entrada:
n = 10

Saída esperada:
expected = False
Entrada:
long long n = 10;

Saída esperada:
bool expected = false;
Entrada:
long n = 10;

Saída esperada:
bool expected = false;

Explicação

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

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

Count how many digits the number has before summing powers.

Dica 2

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

Dica 3

Remember that 0 is a special one-digit case.

Complexidade esperada

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

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.