The problem of least common multiple of multiple numbers

Source: Internet
Author: User
Tags greatest common divisor

Problem description to find the number of n least common multiple. Input inputs contain multiple test instances, and each test instance starts with a positive integer n, followed by n positive integers. Output outputs their least common multiple for each set of test data, with one row for each test instance output. You can assume that the final output is a 32-bit integer. Sample Input2 4 6 3 2 5 7 sample Output12 70
#include <stdio.h>#include<stdlib.h>typedefLongunsigned lu;lu gcd (lu A,lu b) {intC; C=a%b;  while(c) {a=b; b=C; C=a%b; }    returnb;} Lu LCM (Lu A,lu b) {returna*b/gcd (A, b);}intMain () {LU t,n;  while(SCANF ("%lu", &t)! =EOF) {Lu re=1;  while(t--) {scanf ("%lu",&N); Re=LCM (re,n); } printf ("%lu\n", re); }    return 0;}

* Least common multiple and greatest common divisor: [a1,a2,.., an]=m/(m/a1,m/a2,.., m/an), where M is the product of A1,a2,.., an, and A1,a2,.., an is a positive integer.

For example: For 4,6,8,10, there are [4,6,8,10]=120, while m=4*6*8*10=1920,m/(m/a1,m/a2,.., m/an) =1920/(6*8*10,4*8*10,4*6*10,4*6*8) =1920/16 = 120.

* Algorithm implementation of multiple number greatest common divisor

The number of least common multiple can be converted into the greatest common divisor of multiple numbers. The traditional method of finding multiple numbers of greatest common divisor (a1,a2,.., an) is to seek two numbers of greatest common divisor multiple times, i.e.

(1) Calculating the greatest common divisor (A1,A2) of A1 and A2 by using the method [2]

(2) Calculating (A1,A2) and A3 greatest common divisor by the method of tossing and dividing (A1,A2,A3)

(3) Calculating (A1,A2,A3) and A4 greatest common divisor by the method of tossing and dividing (A1,A2,A3,A4)

(4) Repeat it until it is obtained (A1,A2,.., an)

The above method requires n-1 to divide the operation.

The key to this problem is to find out the relationship between least common multiple and greatest common divisor, that is, ABC's least common multiple =A*B*C/GCD (a,b,c), so this problem is converted into a multi-number of greatest common divisor, the use of the division of the final results obtained.

The problem of least common multiple of multiple numbers

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.