php 求连续数的公倍数

WBOY
Release: 2016-07-06 13:51:56
Original
1308 people have browsed it

为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。 但也并非纯粹的偶然:60是个优秀的数字,它的因子比较多。
事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。

我们希望寻找到能除尽1至n的的每个数字的最小整数。

不要小看这个数字,它可能十分大,比如n=100, 则该数为:
69720375229712477164533808935312303556800

请编写程序,实现对用户输入的 n (n

例如: 用户输入: 6 程序输出: 60

用户输入: 10 程序输出: 2520

求用php实现此方法,最好能先讲下思路

回复内容:

为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。 但也并非纯粹的偶然:60是个优秀的数字,它的因子比较多。
事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。

我们希望寻找到能除尽1至n的的每个数字的最小整数。

不要小看这个数字,它可能十分大,比如n=100, 则该数为:
69720375229712477164533808935312303556800

请编写程序,实现对用户输入的 n (n

例如: 用户输入: 6 程序输出: 60

用户输入: 10 程序输出: 2520

求用php实现此方法,最好能先讲下思路

大概思路是:先求两个数的最小公倍数(其中用到辗转相除法求两个数的最大公约数,再根据公式来得到最小公倍数),再跟下一个数求最小公倍数,依次直到最后一个数。。

代码如下:(需要注意的是php 的int类型过大会溢出的问题)

<code>function xmzenger($n)
{
    //1和2的最小公倍数
    $res = 2;
    for ($i=1; $i </code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!