my solution link :CodeChef: Practical coding for everyone
please help
When you are finding lcm that time you are multiplying (a*b) which is resulting in integer overflow .You should use long long int instead of int to avoid the overflow.
See here i have changed int to long long int in your solution
Use long long int instead of int even for a and b. This is because (a*b) is stored in int data type and then divided by GCD which causes the integer overflow! in order to understand by yourself, please the test case
5
120 140
10213 312
10 30
1000000 1000000
1000000 999999
The output should be :
20 840
1 3186456
10 30
1000000 1000000
1 999999000000
You just have to change int to long long int in the gcd function as well!! Hope that was a help.
If I might intervene, there’s a quicker/shorter way to do this.
One C++ STL function many are unaware of, is the GCD one : __gcd(a,b)
Now, LCM will be a * b/ __gcd(a,b) and make sure a and b are long long ints. Hope it helps.
Yours
Tom Marvolo Riddle