Problema
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).
Restrições
0 ≤ n ≤ 10^9
Exemplos
Entrada:
n = 25
Saída esperada:
expected = True
Entrada:
long long n = 25;
Saída esperada:
bool expected = true;
Entrada:
long n = 25;
Saída esperada:
bool expected = true;
Explicação
25² = 625, which ends in 25, so the answer is true.
Entrada:
n = 7
Saída esperada:
expected = False
Entrada:
long long n = 7;
Saída esperada:
bool expected = false;
Entrada:
long n = 7;
Saída esperada:
bool expected = false;
Explicação
7² = 49, which ends in 9, not 7, so the answer is false.
Como abordar
Tente sozinho primeiro e depois abra uma dica de cada vez.
Dica 1
Find how many digits n has.
Dica 2
The last d digits of a number are its remainder modulo 10^d.
Dica 3
Make sure n = 0 counts as one digit, so 10^d is 10, not 1.
Complexidade esperada
O(d) time for d digits, O(1) extra space
Referências
Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.