Skip to content

Instantly share code, notes, and snippets.

@garciparedes
Forked from endolith/gcd_and_lcm.py
Created May 4, 2019 10:09
Show Gist options
  • Select an option

  • Save garciparedes/5438b66512783f51a8006051ce1f52c0 to your computer and use it in GitHub Desktop.

Select an option

Save garciparedes/5438b66512783f51a8006051ce1f52c0 to your computer and use it in GitHub Desktop.

Revisions

  1. @endolith endolith revised this gist Mar 20, 2018. 1 changed file with 35 additions and 6 deletions.
    41 changes: 35 additions & 6 deletions gcd_and_lcm.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,45 @@
    # Greatest common divisor of more than 2 numbers. Am I terrible for doing it this way?
    # Greatest common divisor of 1 or more numbers.
    from functools import reduce


    def gcd(*numbers):
    """Return the greatest common divisor of the given integers"""
    from fractions import gcd
    """
    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:
    # Least common multiple is not in standard libraries? It's in gmpy, but this
    # is simple enough:


    def lcm(*numbers):
    """Return lowest common multiple."""
    """
    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...
    # Assuming numbers are positive integers...
  2. @endolith endolith revised this gist Jun 28, 2012. 2 changed files with 16 additions and 11 deletions.
    16 changes: 16 additions & 0 deletions gcd_and_lcm.py
    Original 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...
    11 changes: 0 additions & 11 deletions lcm.py
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    # 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...
  3. @endolith endolith renamed this gist Jun 14, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @endolith endolith revised this gist Jun 14, 2012. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions gcd and lcm.py
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,11 @@
    # GCD and LCM are not in math module. They are in gmpy, but these are simple enough:
    # LCM not in standard libraries? It's in gmpy, but this is 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)
    from fractions import gcd

    # Assuming a and b are positive integers...
    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...
  5. @endolith endolith revised this gist May 19, 2009. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gcd and lcm.py
    Original 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...
  6. @endolith endolith revised this gist May 19, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gcd and lcm.py
    Original 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"""
    """Return 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 the lowest common multiple of a and b"""
    return a * b / gcd(a, b)

  7. @endolith endolith revised this gist May 19, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gcd and lcm.py
    Original 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 '''
    """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 '''
    """Compute the lowest common multiple of a and b"""
    return a * b / gcd(a, b)

  8. @endolith endolith revised this gist May 19, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gcd and lcm.py
    Original 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'''
    ''' 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'''
    ''' Compute the lowest common multiple of a and b '''
    return a * b / gcd(a, b)

  9. @endolith endolith created this gist May 19, 2009.
    12 changes: 12 additions & 0 deletions gcd and lcm.py
    Original 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)