sgarden-overflow in lcm

Is there any other way to avoid overflow in calculating lcm other than using factorisation method?
i tried lcm(a,b)=a*(b/gcd(a,b)). I first divided b by gcd,to avoid overflow,but this also gives wrong answer??

1 Like

You can decompose each number into prime factors and keep track of maximum count of each factor and later multiply all factors. You can find the same approach in many solutions for sgarden.

1 Like

One approach is given by mjbpl but you can use other languages like Python or JAVA which can handle big numbers, or If you are using C/C++, use Big Int Library to handle the overflow… :slight_smile:

1 Like

I think using python would be the best choice … thanks :slight_smile:


go for the last answer and view it in ideone

//