Php calculates the public multiple of the continuous number

Source: Internet
Author: User
Why does one hour have 60 minutes instead of 100 minutes? This is caused by historical habits. But it is not purely accidental: 60 is a good number, with many factors. In fact, it is a multiple of every number from 1 to 6. That is, can be divided into 60. We hope to find...

Why does one hour have 60 minutes instead of 100 minutes? This is caused by historical habits. But it is not purely accidental: 60 is a good number, with many factors.
In fact, it is a multiple of every number from 1 to 6. That is, can be divided into 60.

We want to find the minimum integer of each number that can divide by 1 to n.

Do not underestimate this number. it may be very large. for example, if n = 100, this number is:
69720375229712477164533808935312303556800

Write a program to obtain 1 ~ from the n (n <100) entered by the user ~ The minimum public multiple of n.

Example: User Input: 6 program output: 60

User input: 10 program output: 2520

If you want to use php to implement this method, you 'd better explain the idea first.

Reply content:

Why does one hour have 60 minutes instead of 100 minutes? This is caused by historical habits. But it is not purely accidental: 60 is a good number, with many factors.
In fact, it is a multiple of every number from 1 to 6. That is, can be divided into 60.

We want to find the minimum integer of each number that can divide by 1 to n.

Do not underestimate this number. it may be very large. for example, if n = 100, this number is:
69720375229712477164533808935312303556800

Write a program to obtain 1 ~ from the n (n <100) entered by the user ~ The minimum public multiple of n.

Example: User Input: 6 program output: 60

User input: 10 program output: 2520

If you want to use php to implement this method, you 'd better explain the idea first.

The general idea is to first calculate the least common multiple of two numbers (where the maximum common divisor of two numbers is obtained using the moving phase division, and then the least common multiple is obtained based on the formula), and then the next number is the least common multiple, until the last number ..

The code is as follows: (it is worth noting that the int type of php has exceeded the general overflow problem)

Function xmzenger ($ n) {// minimum common multiples of 1 and 2 $ res = 2; for ($ I = 1; $ I <= $ n; $ I ++) {// $ res is the minimum common approx. of the number before $ a, and $ B is given to continue and $ a to find the minimum common multiple $ a = $ I; $ B = $ res; // $ c is the product of two numbers $ c = $ a * $ B; // The exchange value makes $ a always greater than $ B if ($ a <$ B) {$ r = $ a; $ a = $ B; $ B = $ r;} // calculate the maximum common divisor of two natural numbers by division of the moving phase while (true) {$ r = $ a % $ B; // if $ r is 0, $ B is the maximum common approx. if ($ r = 0) {// formula used in primary school: "(a, B) [a, B] = AB (a, B are integers)" $ res = $ c/$ B; break ;} else {$ a = $ B; $ B = $ r ;}}return $ res;} echo xmzenger (10 );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.