r/shittyprogramming • u/FritangadeLuka420 • Sep 14 '18
Beginning in C++
I'm working in a small homework and the last step is calculate the least common multiple but with easy steps like if or something like that, could someone help me?
14
Upvotes
View all comments
18
u/scooty14 Sep 14 '18 edited Sep 15 '18
Least common multiple of numbers A and B is A*B divided by greatest common divisor of these numbers.
Now you need to calculate greatest common divisor, should be pretty easy:
lcm(a,b) = (a*b) / gcd(a,b) ... *gcd(a,b)
gcd(a,b) * lcm(a,b) = a * b ... /lcm(a,b)
gcd(a,b) = (a*b) / lcm(a,b)
Lets write the function:
With both functions defined, you can just call your function:
this will print 12