簡単 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。