I've been trying to solve this Codewars problem https://www.codewars.com/kata/56e56756404bb1c950000992
In this kata you need to create a function that takes a 2D array/list of non-negative integer pairs and returns the sum of all the "saving" that you can have getting the LCM of each couple of number compared to their simple product.
without importing any extra Python library/module.
My code passes al tests but it gets the "Execution Timed Out (12000 ms)" How can I solve this?
Thanks a lot in advance.
Here's my code:
def sum_differences_between_products_and_LCMs(pairs): result = 0 # calculating LCM for x, y in pairs: if x > y: greater = x else: greater = y while(True): if x == 0 or y == 0: lcm = 0 break else: if((greater % x == 0) and (greater % y == 0)): lcm = greater break greater += 1 result += ((x*y) - lcm) return result