Условие
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.
Ограничения
1 ≤ n ≤ 10^9
Примеры
Ввод:
n = 89
Ожидаемый вывод:
expected = True
Ввод:
long long n = 89;
Ожидаемый вывод:
bool expected = true;
Ввод:
long n = 89;
Ожидаемый вывод:
bool expected = true;
Пояснение
8¹ + 9² = 8 + 81 = 89, so the answer is true.
Ввод:
n = 98
Ожидаемый вывод:
expected = False
Ввод:
long long n = 98;
Ожидаемый вывод:
bool expected = false;
Ввод:
long n = 98;
Ожидаемый вывод:
bool expected = false;
Пояснение
9¹ + 8² = 9 + 64 = 73, not 98, so the answer is false.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
Positions count from the left, so either turn n into a string or count the digits first.
Подсказка 2
Use integer multiplication for powers instead of a floating point function.
Подсказка 3
The largest term is 9^10, which fits easily in 64 bits.
Ожидаемая сложность
O(d^2) time for d digits, O(1) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.