Skip to content

Commit 7930a64

Browse files
author
uuk020
committed
13的可分性规则 thirt
1 parent 434745b commit 7930a64

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

thirt/thirt.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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

Comments
 (0)