Problem Statement
John and Brus believe that the digits 4 and 7 are lucky and all others are not. According to them, a lucky number is a number that contains only lucky digits in its decimal representation.
Recently, they became interested in numeral systems with different bases. Normally, people use the numeral system with base 10 to represent numbers, however, there exist numeral systems with other bases. More exactly, for each integer B, B >= 2, there exists a numeral system with base B. In this system, there are B different digits, used to represent numbers from 0 to B-1, inclusive. In order to represent a positive integer A in such system, it's necessary to find such digits a[n], a[n-1], ..., a[0], that A = a[n] * B^n + a[n-1] * B^(n-1) + ... + a[0] * B^0 (here ^ denotes the power operator), and then write these digits from left to right, in this exact order. For example, 255 = 4 * 52 + 47, therefore representation of number 255 in the numeral system with base 52 consists of two digits, the leftmost digit is 4 and the rightmost digit is 47.
The base of numeral system B is called lucky for some integer number A if all digits of the number A represented in numeral system with base B are the lucky numbers (i.e. 4, 7, 44, 47, ...). For example, base 52 is lucky for the number 255.
You are given a
Definition
- Class:
- TheLuckyBasesDivOne
- Method:
- find
- Parameters:
- long
- Returns:
- long
- Method signature:
- long find(long n)
- (be sure your method is public)
Constraints
- n will be between 1 and 10^16, inclusive.
Examples
255
Returns: 2
There are two lucky bases for the number 255: 52 and 62.
474
Returns: -1
All bases greater than 474 are lucky.
13
Returns: 0
No lucky bases here.
4748
Returns: 5
17
Returns: 0
23
Returns: 0
52
Returns: 1
347723696
Returns: 150
925643275
Returns: 233
65038986
Returns: 108
644199585
Returns: 226
333393417
Returns: 148
255248223
Returns: 145
683104052
Returns: 234
517788483
Returns: 200
434727064
Returns: 211
610370488
Returns: 200
454765462
Returns: 203
643862778
Returns: 208
852497597
Returns: 211
7099619347723696
Returns: 27854
7883257925643275
Returns: 27884
8116120065038986
Returns: 29844
9656914644199585
Returns: 29004
5693912333393417
Returns: 25142
7572215255248223
Returns: 29116
5678191683104052
Returns: 25828
4768164517788483
Returns: 25566
6954493434727064
Returns: 28369
5033728610370488
Returns: 25732
9099619347697918
Returns: 27979
9883257925626617
Returns: 27641
9116120065015927
Returns: 27917
9656914644197045
Returns: 27828
9693912333378931
Returns: 29140
10000000000000000
Returns: 28302
7777777777777777
Returns: -1
7777777777777778
Returns: 28182
4774747477474774
Returns: -1
7
Returns: -1
7099619347723696
Returns: 27854
7883257925643275
Returns: 27884
8116120065038986
Returns: 29844
9656914644199585
Returns: 29004
5693912333393417
Returns: 25142
7572215255248223
Returns: 29116
5678191683104052
Returns: 25828
419546186871
Returns: 1588
64969059354121
Returns: 6951
32553584898247
Returns: 4661
45395562278
Returns: 786
37892149639977
Returns: 5602
21270484151041
Returns: 3611
69945385747222
Returns: 7288
73048617291088
Returns: 7219
100458529411078
Returns: 7036
124394104724
Returns: 892
5994467456372
Returns: 3566
4997301456
Returns: 397
10868313348334
Returns: 3474
4451382129015250
Returns: 26566
853639853734029
Returns: 14907
158532595550982
Returns: 6964
293296577135859
Returns: 9465
4267871896654374
Returns: 26081
2384185791015624
Returns: 17610
1286948916285415
Returns: 13751
2384185791015624
Returns: 17610
126267109331844
Returns: 7073
3566375989313287
Returns: 19815
1086735827140
Returns: 1736
631542762657
Returns: 1757
31316926201
Returns: 585
9999999800000004
Returns: 28629
9999999999999999
Returns: 28794
9999999999999998
Returns: 27988
9999999999999997
Returns: 29036
9999999999999996
Returns: 28612
9999999999999995
Returns: 27700
9999999999999994
Returns: 29058
9999999999999993
Returns: 29019
9999999999999992
Returns: 27683
9999999999999991
Returns: 28831
9598500211500004
Returns: 27782
9876542873161235
Returns: 27758
4704747470000774
Returns: 25365