Problem Statement
Sequence (d1, d2, d3, ..., dt) is a digit representation of X (with no leading zeros) if the i-th digit of X is di. In particular, d1 is the most and dt is the least significant digit of X. For instance, the digit representation of X = 576 is (5, 7, 6).
We say that an integer X is magic-square iff:
- X is a square of an integer, i.e., X = Y2 for some integer Y.
- For the digit representation (d1, d2, d3, ..., dt) of X it holds d1 < d2 > d3 < d4, and so on. That is, for each odd i < t it holds di < di + 1, and for each even i < t it holds di > di + 1.
Given A and B, output how many magic-square integers between A and B there are.
Definition
- Class:
- MagicNumbersAgain
- Method:
- count
- Parameters:
- long, long
- Returns:
- int
- Method signature:
- int count(long A, long B)
- (be sure your method is public)
Constraints
- B will be between 1 and 10,000,000,000, inclusive.
- A will be between 1 and B, inclusive.
Examples
1
64
Returns: 7
The magic numbers between 1 and 64 are: 1, 4, 9, 16, 25, 36, and 49. Although 64 is a square of an integer (64=8*8), 64 is not a magic number as its first digit is not smaller than its second one.
50
60
Returns: 0
Between 50 and 60 there are no integers that are squares of other integers, hence in this range can not exist magic numbers neither.
121
121
Returns: 1
5679
1758030
Returns: 73
1304164
2000000
Returns: 14
1304164
1907161
Returns: 14
1304165
1907161
Returns: 13
1
1
Returns: 1
1
10000000000
Returns: 1149
10000000000
10000000000
Returns: 0
1000000000
10000000000
Returns: 589
10
1234567890
Returns: 566
1000000
9999999999
Returns: 1063
9999999999
10000000000
Returns: 0
2
4
Returns: 1
4
9
Returns: 2
24
26
Returns: 1
1501275177
8330329353
Returns: 533
17734730
6383220611
Returns: 909
2084048076
4060336726
Returns: 210
2284006856
9035470905
Returns: 413
231457451
7732700841
Returns: 735
8240370457
9854044437
Returns: 12
6589010881
9345242761
Returns: 69
6797853005
9619875765
Returns: 53
1555956841
8182526689
Returns: 519
3611916921
3682513801
Returns: 17
2377186301
5202542937
Returns: 282
367295822
4368245701
Returns: 493
4456734205
7142592909
Returns: 172
7096237713
9496639625
Returns: 31
4470460209
8391275841
Returns: 191
1
1000000000
Returns: 560
1000
10000000000
Returns: 1135
5
10000000000
Returns: 1147
400000099
10000000000
Returns: 679