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

后端 未结 15 1908
太阳男子
太阳男子 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:12

    If you look at this page, you can see a fairly simple algorithm you could use. :-)

    I'm not saying it's efficient or anything, mind, but it does conceptually scale to multiple numbers. You only need space for keeping track of your original numbers and a cloned set that you manipulate until you find the LCM.

提交回复
热议问题