Легко Решено: 0 Процент решений: 0%

Armstrong Number

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

Чему вы научитесь

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

Условие

Показать перевод

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.

Ограничения

0 ≤ n ≤ 10¹⁸

Примеры

Пример 1
Ввод:
n = 153

Ожидаемый вывод:
expected = True
Ввод:
long long n = 153;

Ожидаемый вывод:
bool expected = true;
Ввод:
long n = 153;

Ожидаемый вывод:
bool expected = true;

Пояснение

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

Пример 2
Ввод:
n = 10

Ожидаемый вывод:
expected = False
Ввод:
long long n = 10;

Ожидаемый вывод:
bool expected = false;
Ввод:
long n = 10;

Ожидаемый вывод:
bool expected = false;

Пояснение

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

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

Count how many digits the number has before summing powers.

Подсказка 2

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

Подсказка 3

Remember that 0 is a special one-digit case.

Ожидаемая сложность

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

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.