PROBLEM STATEMENT
A number N is called an automorphic number if N^2 (square of N in decimal
notation) ends with N. These numbers are also called curious numbers. For
example, 25^2 = 625, which ends with 25; hence 25 is an automorphic number. On
the other hand 16^2 = 256, which doesn't end with 16 and hence 16 is not an
automorphic number. We would like to find for any number, how far it is from
being automorphic. We define this distance, in the following manner: for a
number N, calculate N^2, find the longest sequence of digits such that the
number N and the number N^2 both end with these digits (in the same order),
then subtract the number of digits in the sequence from the number of digits in
the number N.
Your task is, given an integer N, calculate the distance of N to being an
automorphic number described above.
DEFINITION
Class: Automorphic
Method: distance
Parameters: int
Returns: int
Method signature (be sure your method is public): int distance(int num);
TopCoder will ensure the validity of the inputs. Inputs are valid if all of the
following criteria are met:
- num is between 1 and 10000 inclusive
EXAMPLES
1. num = 25, 25^2=625, all the digits of N appear at the end of N^2, thus the
number N is automorphic. The length of the longest common sequence is 2, the
number of digits in N is 2; hence the difference is 0,
returns 0
2. num = 7, 7 has 1 digit, and 0 digits at the end coincide for 7 and 7^2,
returns 1
3. num = 10000, 10000 has 5 digits, and 4 digits at the end coincide for 10000
and 10000^2,
returns 1
4. num = 2255, 2255^2 = 5085025, 2255 has 4 digits, and 1 digit at the end
coincides for 2255 and 5085025,
returns 3