Why does 1 hours have 60 minutes instead of 100 minutes? This is the result of historical habits. But it is not purely accidental: 60 is a good number, it has more factors.
In fact, it is a multiple of each number from 1 to 6. That is, 1,2,3,4,5,6 can be 60 apart.
We want to find the smallest integer that can divide every number from 1 to N.
Do not underestimate this number, it may be very large, such as n=100, then the number is:
69720375229712477164533808935312303556800
Please write a program that implements the least common multiple of 1~n for user input n (n<100).
Example: User input: 6 program output: 60
User input: 10 program output: 2520
To use PHP to implement this method, it is better to first talk about the idea
Reply content:
Why does 1 hours have 60 minutes instead of 100 minutes? This is the result of historical habits. But it is not purely accidental: 60 is a good number, it has more factors.
In fact, it is a multiple of each number from 1 to 6. That is, 1,2,3,4,5,6 can be 60 apart.
We want to find the smallest integer that can divide every number from 1 to N.
Do not underestimate this number, it may be very large, such as n=100, then the number is:
69720375229712477164533808935312303556800
Please write a program that implements the least common multiple of 1~n for user input n (n<100).
Example: User input: 6 program output: 60
User input: 10 program output: 2520
To use PHP to implement this method, it is better to first talk about the idea
The approximate idea is: The first two number of least common multiple (which used to divide the method to find two numbers greatest common divisor, and then according to the formula to get least common multiple), and then the next number for least common multiple, and then to the last number.
The code is as follows: (Note that the int type of PHP is overrun by the Convention)
function xmzenger($n){ //1和2的最小公倍数 $res = 2; for ($i=1; $i <= $n; $i++) { //$res为$a之前的数的最小公约数,赋予$b继续和$a求最小公倍数 $a = $i; $b = $res; //$c为两数的乘积 $c = $a * $b; //交换值使$a总是比$b大 if($a < $b){ $r = $a; $a = $b; $b = $r; } //辗转相除法求两个自然数的最大公约数 while (true) { $r = $a % $b; //如果$r为0则$b为最大公约数 if($r == 0){ //小学学过的公式:“(a,b)[a,b]=ab(a,b均为整数)” $res = $c/$b; break; }else{ $a = $b; $b = $r; } } } return $res;}echo xmzenger(10);