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

Disarium Number

Check whether a number equals the sum of its digits raised to their positions.

O que você vai aprender

  • Read digits from left to right
  • Raise a digit to its position with integer arithmetic
  • Add powers without overflow

Problema

Ver tradução

Number the digits of n from the left starting at 1, raise each digit to the power of its position, and add the results. If the total is n itself, the number is a disarium number. For example, 175 gives 1¹ + 7² + 5³ = 1 + 49 + 125 = 175.

Implement disarium_number(n).

Input

n: The number to test.

Output

Return true if n is a disarium number, otherwise false. Every single-digit number is a disarium number.

Restrições

1 ≤ n ≤ 10^9

Exemplos

Exemplo 1
Entrada:
n = 89

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

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

Saída esperada:
bool expected = true;

Explicação

8¹ + 9² = 8 + 81 = 89, so the answer is true.

Exemplo 2
Entrada:
n = 98

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

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

Saída esperada:
bool expected = false;

Explicação

9¹ + 8² = 9 + 64 = 73, not 98, so the answer is false.

Como abordar

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

Dica 1

Positions count from the left, so either turn n into a string or count the digits first.

Dica 2

Use integer multiplication for powers instead of a floating point function.

Dica 3

The largest term is 9^10, which fits easily in 64 bits.

Complexidade esperada

O(d^2) time for d digits, O(1) extra space

Referências

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

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

Ainda não há soluções aprovadas. Resolva e fique com o primeiro lugar.

Aprenda com outros

Soluções da comunidade

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