Tutte le sfide
Facile 0 risolte 0% di accettazione

Automorphic Number

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

Cosa imparerai

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

Problema

Mostra traduzione

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).

Vincoli

0 ≤ n ≤ 10^9

Esempi

Esempio 1
Input:
n = 25

Output atteso:
expected = True
Input:
long long n = 25;

Output atteso:
bool expected = true;
Input:
long n = 25;

Output atteso:
bool expected = true;

Spiegazione

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

Esempio 2
Input:
n = 7

Output atteso:
expected = False
Input:
long long n = 7;

Output atteso:
bool expected = false;
Input:
long n = 7;

Output atteso:
bool expected = false;

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Find how many digits n has.

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.