diff --git a/LCM of n numbers.py b/LCM of n numbers.py index fd1494b..0281d17 100644 --- a/LCM of n numbers.py +++ b/LCM of n numbers.py @@ -1,3 +1,5 @@ +import time +from functools import reduce def gcd(x, y): while(y): x, y = y, x % y @@ -6,9 +8,40 @@ def gcd(x, y): def lcm(x, y): lcm = (x*y)//gcd(x,y) return lcm - +t1=time.time() def lcm_of_n(li): return reduce(lcm,li) li=[1,2,3,4,5,6,7,8,9,10] -print lcm_of_n(li) +print(lcm_of_n(li)) +t2=time.time() +print(t2-t1) + +t3=time.time() +li=[1,2,3,4,5,6,7,8,9,10] +gc=li[0] +prod=li[0] +for i in range(1,len(li)): + gc=gcd(li[i],gc) + prod*=li[i] +print(prod//gc) +t4=time.time() +print(t4-t3) + + +''' +Code not giving correct answer: + Providing=> + Correct, + Understandable, + Brute-Force Code + TC==>O(n) + With same execution time. + + O/P==> of the above code + 2520 + 0.016462326049804687 + 3628800 + 0.005449771881103516 +''' +