C++ algorithm to calculate least common multiple for multiple numbers

后端 未结 15 1752
太阳男子
太阳男子 2020-12-14 16:29

Is there a C++ algorithm to calculate the least common multiple for multiple numbers, like lcm(3,6,12) or lcm(5,7,9,12)?

15条回答
  •  囚心锁ツ
    2020-12-14 17:03

    • let the set of numbers whose lcm you wish to calculate be theta
    • let i, the multiplier, be = 1
    • let x = the largest number in theta
    • x * i
    • if for every element j in theta, (x*i)%j=0 then x*i is the least LCM
    • if not, loop, and increment i by 1

提交回复
热议问题