-
-
Save garciparedes/5438b66512783f51a8006051ce1f52c0 to your computer and use it in GitHub Desktop.
Revisions
-
endolith revised this gist
Mar 20, 2018 . 1 changed file with 35 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,45 @@ # Greatest common divisor of 1 or more numbers. from functools import reduce def gcd(*numbers): """ Return the greatest common divisor of 1 or more integers Examples -------- >>> gcd(5) 5 >>> gcd(30, 40) 10 >>> gcd(120, 40, 60) 20 """ # Am I terrible for doing it this way? from math import gcd return reduce(gcd, numbers) # Least common multiple is not in standard libraries? It's in gmpy, but this # is simple enough: def lcm(*numbers): """ Return lowest common multiple of 1 or more integers. Examples -------- >>> lcm(5) 5 >>> lcm(30, 40) 120 >>> lcm(120, 40, 60) 120 """ def lcm(a, b): return (a * b) // gcd(a, b) return reduce(lcm, numbers, 1) # Assuming numbers are positive integers... -
endolith revised this gist
Jun 28, 2012 . 2 changed files with 16 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ # Greatest common divisor of more than 2 numbers. Am I terrible for doing it this way? def gcd(*numbers): """Return the greatest common divisor of the given integers""" from fractions import gcd return reduce(gcd, numbers) # Least common multiple is not in standard libraries? It's in gmpy, but this is simple enough: def lcm(*numbers): """Return lowest common multiple.""" def lcm(a, b): return (a * b) // gcd(a, b) return reduce(lcm, numbers, 1) # Assuming numbers are positive integers... This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +0,0 @@ -
endolith renamed this gist
Jun 14, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
endolith revised this gist
Jun 14, 2012 . 1 changed file with 9 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,11 @@ # LCM not in standard libraries? It's in gmpy, but this is simple enough: from fractions import gcd def lcm(*numbers): """Return lowest common multiple.""" def lcm(a, b): return (a * b) // gcd(a, b) return reduce(lcm, numbers, 1) # Assuming numbers are positive integers... -
endolith revised this gist
May 19, 2009 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,3 +10,4 @@ def lcm(a, b): """Return the lowest common multiple of a and b""" return a * b / gcd(a, b) # Assuming a and b are positive integers... -
endolith revised this gist
May 19, 2009 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,12 @@ # GCD and LCM are not in math module. They are in gmpy, but these are simple enough: def gcd(a,b): """Return the greatest common divisor of a and b""" while b > 0: a, b = b, a % b return a def lcm(a, b): """Return the lowest common multiple of a and b""" return a * b / gcd(a, b) -
endolith revised this gist
May 19, 2009 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,12 @@ # GCD and LCM are not in math module. They are in gmpy, but these are simple enough: def gcd(a,b): """Compute the greatest common divisor of a and b""" while b > 0: a, b = b, a % b return a def lcm(a, b): """Compute the lowest common multiple of a and b""" return a * b / gcd(a, b) -
endolith revised this gist
May 19, 2009 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,12 @@ # GCD and LCM are not in math module. They are in gmpy, but these are simple enough: def gcd(a,b): ''' Compute the greatest common divisor of a and b ''' while b > 0: a, b = b, a % b return a def lcm(a, b): ''' Compute the lowest common multiple of a and b ''' return a * b / gcd(a, b) -
endolith created this gist
May 19, 2009 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ # GCD and LCM are not in math module. They are in gmpy, but these are simple enough: def gcd(a,b): ''' Compute the greatest common divisor of a and b''' while b > 0: a, b = b, a % b return a def lcm(a, b): ''' Compute the lowest common multiple of a and b''' return a * b / gcd(a, b)