ACM Number Theory Tour 3---Greatest common divisor gcd and least common multiple LCM (struck, rebuild ( ̄? ̄))

Source: Internet
Author: User
Tags greatest common divisor

GCD (A, B) is the greatest common divisor of A and B

LCM (A, b), which is the least common multiple of a and B

And then there's a formula.

A*b = gcd * LCM (GCD is gcd (A, B), (?∀?) shorthand do you understand?)

Explain (skip if you don't want to see) {

First, ask for a gcd, then ...

A/GCD and B/GCD These two numbers coprime, namely gcd (A/GCD, B/GCD) = 1, then ...

LCM = gcd * (A/GCD) * (B/GCD)

LCM = (A * b)/GCD

So.. A*b = GCD * LCM

}

So request LCM, ask GCD first

Spicy, the problem, gcd how to Beg

Euclidean method

While loop

1 ll GCD (ll A, ll b) {2    ll t; 3      while (b) {4         t = b; 5         b = a% b; 6         A = t; 7     }8     return  A; 9 }

and a recursive notation.

1 ll GCD (ll A, ll b) {2     if(b = =0)returnA;3     Else returnGCD (b, a%b);4 }5 6 ll GCD (ll A, ll b) {7     returnB? GCD (b, a%b): A;8 }9 //two kinds are available

Spicy, LCM = A * B/GCD

(Note that this may be wrong, because A * b may be too large to exceed int or exceed Longlong)

So the recommendation is written as: LCM = A/GCD * b

And then a few formulas to prove it yourself.

GCD (ka, KB) = k * GCD (A, B)

LCM (ka, KB) = k * LCM (A, B)

Last time I hit this formula.

LCM (s/a, s/b) = S/GCD (A, B)

S = 9,a = 4,b = 6, fractional does not LCM, had to retain fractional form to-pass numerator.

When I see the formula on the right ....

(╯°д°) ╯┻━┻

This TM I think of, to prove to me is a certificate. t_t

ACM number Theory Tour 3---Greatest common divisor gcd and least common multiple LCM (struck, rebuild ( ̄? ̄))

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.