Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gcd and LCM/ Nth root in Python #67

Merged
merged 4 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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