Skip to content

Commit

Permalink
Merge pull request #67 from Garvit244/gcd
Browse files Browse the repository at this point in the history
Gcd and LCM/ Nth root in Python
  • Loading branch information
BaReinhard authored Oct 3, 2017
2 parents 7529140 + 3992cba commit 7dd7fde
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions algebra/gcd/python/gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
euclid_gcd(0 is used to find the gcd of two numbers using euclidean algorithm
@input: a, b numbers
@output gcd(a,b)
"""

def euclid_gcd(a, b):
if a < b:
return euclid_gcd(b, a)

while b != 0:
(a, b) = (b, a%b)

return a
23 changes: 23 additions & 0 deletions algebra/lcm/python/lcm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
euclid_gcd() is used to find the gcd of two numbers using euclidean algorithm
@input: a, b numbers
@output gcd(a,b)
"""

def euclid_gcd(a, b):
if a < b:
return euclid_gcd(b, a)

while b != 0:
(a, b) = (b, a%b)

return a
"""
lcm() is used to return the least common multiple between two numbers
@input: a, b numbers
@output: lcm(a, b)
"""

def lcm(a, b):
gcd = euclid_gcd(a, b)
return (a*b)/gcd
19 changes: 19 additions & 0 deletions algebra/nth_root/python/nth_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Find the nth root of a and return the integer component only
@input: number, root
@output: nth root of number
"""
def nth_root(a,n):
high = 1
while high ** n <= a:
high *= 2
low = high/2
while low < high:
mid = (low + high) // 2
if low < mid and mid**n < a:
low = mid
elif high > mid and mid**n > a:
high = mid
else:
return mid
return mid + 1

0 comments on commit 7dd7fde

Please sign in to comment.