簡単 0件解決済み 正答率0%

Automorphic Number

Check whether the square of a number ends with the number itself.

このチャレンジで学べること

  • Count the digits of a number
  • Take the last d digits with a modulo
  • Handle 0 as a one-digit number

問題

翻訳を表示

A number is automorphic if its square ends in the same digits as the number. For example, 76² = 5776, which ends in 76, so 76 is automorphic.

Implement automorphic_number(n).

n² can be as large as 10^18, which still fits in a signed 64-bit integer. You can compare digits from the right, or compare n² modulo 10^d with n, where d is the number of digits of n.

Input

n: The number to test.

Output

Return true if the last digits of n² are exactly the digits of n, otherwise false. 0 and 1 are automorphic (0² = 0, 1² = 1).

制約

0 ≤ n ≤ 10^9

例

例 1
入力:
n = 25

期待される出力:
expected = True
入力:
long long n = 25;

期待される出力:
bool expected = true;
入力:
long n = 25;

期待される出力:
bool expected = true;

解説

25² = 625, which ends in 25, so the answer is true.

例 2
入力:
n = 7

期待される出力:
expected = False
入力:
long long n = 7;

期待される出力:
bool expected = false;
入力:
long n = 7;

期待される出力:
bool expected = false;

解説

7² = 49, which ends in 9, not 7, so the answer is false.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Find how many digits n has.

ヒント 2

The last d digits of a number are its remainder modulo 10^d.

ヒント 3

Make sure n = 0 counts as one digit, so 10^d is 10, not 1.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

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

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

他の人の解答から学ぶ

コミュニティの解答

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