Advertisement
jules0707

lcm

Jun 6th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. # Uses python3
  2. import sys
  3. from gcd.gcd import gcd_one_liner
  4.  
  5.  
  6. def lcm_naive(n, p):
  7.     for l in range(1, n*p + 1):
  8.         if l % n == 0 and l % p == 0:
  9.             return l
  10.  
  11.     return n * p
  12.  
  13. # we can obtain the least common multiple of (n,p) by dividing its product by their greatest
  14. # common divisor
  15.  
  16.  
  17. def lcm_faster(n, p):
  18.     return int(n * p / gcd_one_liner(n, p))
  19.  
  20. if __name__ == '__main__':
  21.     user_input = sys.stdin.read()
  22.     a, b = map(int, user_input.split())
  23.     print(lcm_faster(a, b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement