Optimizing the way of finding lcm