1
+ <?php
2
+ /**
3
+ * 13的可分性规则
4
+ * When you divide the successive powers of 10 by 13 you get the following remainders of the integer divisions:1, 10, 9, 12, 3, 4.
5
+ * Then the whole pattern repeats.
6
+ * Hence the following method: Multiply the right most digit of the number with the left most number in the sequence shown above,
7
+ * the second right most digit to the second left most digit of the number in the sequence. The cycle goes on and you sum all these products.
8
+ * Repeat this process until the sequence of sums is stationary.
9
+ * Example: What is the remainder when 1234567 is divided by 13?
10
+ * 7× ばつ かける 1 + たす 6× ばつ かける 10 + たす 5× ばつ かける 9 + たす 4× ばつ かける 12 + たす 3× ばつ かける 3 + たす 2× ばつ かける 4 + たす 1× ばつ かける 1 = わ 178
11
+ * We repeat the process with 178:
12
+ * 8x1 + 7x10 + 1x9 = 87 and again with 87: 7x1 + 8x10 = 87
13
+ */
14
+ function thirt ($ n ) {
15
+ $ sequence = [1 ,10 ,9 ,12 ,3 ,4 ];
16
+ $ sum = 0 ;
17
+ // 将字符串转换为数组, 再把数组相反
18
+ $ arr = array_reverse (str_split ($ n ));
19
+ // 循环数组
20
+ for ($ i = 0 ; $ i < count ($ arr ); $ i ++) {
21
+ // 通过余数来循环出$sequence数组的值 因为$sequence有6个所以除于6求余数.
22
+ $ sum += $ arr [$ i ] * $ sequence [$ i % 6 ];
23
+ }
24
+ // 判断$n 是否等于$sum, 第一次是不想等, 递归调用 thirt函数, 此时形参$n等于$sum.
25
+ return ($ n === $ sum ) ? $ n : thirt ($ sum );
26
+ }
0 commit comments