-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from Garvit244/gcd
Gcd and LCM/ Nth root in Python
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |