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

Neon Number

Check whether the digit sum of a number's square equals the number.

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

  • Square a number without overflow
  • Sum the digits of the square
  • Compare the result with the input

Условие

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

A neon number is a number whose square has a digit sum equal to the number itself. For example, 9² = 81 and 8 + 1 = 9, so 9 is a neon number.

Implement neon_number(n).

n² can reach 10^18, which fits in a signed 64-bit integer.

Input

n: The number to test.

Output

Return true if the sum of the decimal digits of n² equals n, otherwise false.

Ограничения

0 ≤ n ≤ 10^9

Примеры

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

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

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

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

Пояснение

9² = 81 and 8 + 1 = 9, so the answer is true.

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

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

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

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

Пояснение

12² = 144 and 1 + 4 + 4 = 9, which is not 12, so the answer is false.

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

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

Подсказка 1

Compute n × n in a 64-bit integer.

Подсказка 2

Add up the digits of the square with % 10 and / 10.

Подсказка 3

The digit sum of a square below 10^18 is at most 162, so large n can never be neon.

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

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

Ссылки

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

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

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

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

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

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