Problem Statement
Suppose you want to find a rule for dividing by some divisor in a certain numerationBase. Raising numerationBase to the i-th power and taking the result modulo divisor, you obtain a multiplier for the i-th digit of a number. For example, in base 10, dividing by 3, we get the multipliers:
- 100 % 3 = 1 % 3 = 1
- 101 % 3 = 10 % 3 = 1
- 102 % 3 = 100 % 3 = 1
- ...
When the same multiplier is used for digit j as for digit i, with j > i, a cycle has been detected and will repeat for the remainder of the rule.
Since both 3 and 9 have the same rule in base 10, namely, "multiply each digit by 1, sum them, and check to see if the result is divisible by 3 (or 9, respectively)", you wonder whether other digits have similar divisibility rules. Determine the number of digits in numerationBase which have the same divisibility rules as divisor. A number is considered a digit in a numerationBase if it is between 0 and numerationBase - 1, inclusive, but we will exclude 0 and 1 from consideration.
Definition
- Class:
- DivisibilityRules
- Method:
- similar
- Parameters:
- int, int
- Returns:
- int
- Method signature:
- int similar(int numerationBase, int divisor)
- (be sure your method is public)
Notes
- Here the 0-th digit in a number is the least significant digit, and so on.
- The multipliers in the divisibility rule for a divisor must be less than divisor. For example, though the rule "multiply every digit by 4, sum the results, and check for divisibility by 3" would work for numerationBase 10, divisor 3, we do not consider it valid since 4 >= 3.
- The result should always be at least 1: divisor has the same divisibility rules as divisor!
Constraints
- numerationBase will be between 3 and 1000, inclusive.
- divisor will be between 2 and numerationBase-1, inclusive.
Examples
10
3
Returns: 2
Both 3 and 9 have the same divisibility rules in base 10: multiply each digit by 1, sum the results, and check for divisibility by 3 or 9 respectively.
10
5
Returns: 2
2 and 5 have the same divisibility rules in base 10: add 1 times the 0-th digit and 0 times the other digits; see if the result is divisible by 2 or 5 respectively.
511
32
Returns: 10
The identical rules are for digits 32, 40, 60, 80, 96, 120, 160, 240, and 480. Each has the following rule: Multiply the 0th, 2nd, 4th, 6th, etc. digits by 1, multiply the 1st, 3rd, 5th, etc. digits by 31, add the results, and check for divisibility by 32, 40, 60, 80, 96, 120, 160, 240, or 480.
3
2
Returns: 1
1000
999
Returns: 7
528
382
Returns: 1
541
289
Returns: 1
319
190
Returns: 1
21
18
Returns: 1
384
210
Returns: 1
957
556
Returns: 1
424
223
Returns: 1
164
137
Returns: 1
169
69
Returns: 1
333
86
Returns: 1
65
46
Returns: 1
274
190
Returns: 1
424
248
Returns: 1
692
29
Returns: 1
45
44
Returns: 5
348
37
Returns: 1
566
218
Returns: 1
659
528
Returns: 1
523
323
Returns: 1
174
13
Returns: 1
97
96
Returns: 11
403
119
Returns: 1
119
17
Returns: 2
655
532
Returns: 1
716
246
Returns: 1
425
203
Returns: 1
333
166
Returns: 5
804
373
Returns: 1
531
193
Returns: 1
164
60
Returns: 1
14
3
Returns: 1
14
4
Returns: 1
14
3
Returns: 1
14
4
Returns: 1
15
4
Returns: 1
15
6
Returns: 1
21
6
Returns: 1
21
9
Returns: 1
26
3
Returns: 1
26
4
Returns: 1
26
6
Returns: 1
26
8
Returns: 1
1000
11
Returns: 3
1000
15
Returns: 5
1000
18
Returns: 5
1000
19
Returns: 1
1000
22
Returns: 1
1000
26
Returns: 1
1000
28
Returns: 1
1000
30
Returns: 5
1000
31
Returns: 1
1000
32
Returns: 2
998
45
Returns: 1
998
46
Returns: 1
998
48
Returns: 1
998
49
Returns: 1
998
54
Returns: 1
996
28
Returns: 1
996
30
Returns: 3
996
31
Returns: 1
996
48
Returns: 1
996
49
Returns: 1
996
51
Returns: 1
1000
500
Returns: 14
499
100
Returns: 2
1000
333
Returns: 7
1000
667
Returns: 1
1000
666
Returns: 1
999
333
Returns: 6
999
221
Returns: 1
998
100
Returns: 1
999
998
Returns: 3
965
934
Returns: 1
999
99
Returns: 1
720
2
Returns: 28
344
56
Returns: 3
15
4
Returns: 1
89
86
Returns: 1
366
270
Returns: 1
1000
6
Returns: 2
655
532
Returns: 1
20
9
Returns: 1
997
12
Returns: 11
398
296
Returns: 1
39
12
Returns: 1
900
400
Returns: 2
185
88
Returns: 1
258
32
Returns: 1
999
4
Returns: 1
7
3
Returns: 3
999
992
Returns: 1