Skip to content

Instantly share code, notes, and snippets.

@giampaolo44
Last active October 1, 2016 16:24
Show Gist options
  • Save giampaolo44/2369faa4892b184013aa9202819f1cf5 to your computer and use it in GitHub Desktop.
Save giampaolo44/2369faa4892b184013aa9202819f1cf5 to your computer and use it in GitHub Desktop.
minimo comune denominatore
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 1 16:08:20 2016
@author: giampaolo
"""
def gcdRecur(a, b):
'''
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
'''
print("primo",a,b)
if a<b:
remainder=b
b=a
a=remainder
#remainder=a
if b==0 or a==b:
return a
else:
remainder=a
print("secondo",a,b,remainder-b)
while remainder-b>=b:
# remainder2=remainder
remainder-=b
print("terzo",a,b,remainder-b)
if remainder-b==0:
print("quarto",a,b,remainder-b)
return(b)
else:
print("quinto",a,b,remainder-b)
return gcdRecur(b,remainder-b)
print(gcdRecur(9,12))
@frafra
Copy link

frafra commented Oct 1, 2016

Standard version:

def divisori(a, b):
    for n in range(1, min(a, b)+1):
        if a%n == b%n == 0:
            yield n

if __name__ == '__main__':
    print(list(divisori(9, 12)))

Short version:

divisori = lambda a, b: [n for n in range(1, min(a, b)+1) if a%n == b%n == 0]

Generic version:

def divisori(*integers):
    for n in range(1, min(integers)+1):
        for integer in integers:
            if integer%n:
                break
        else:
            yield n

if __name__ == '__main__':
    print(list(divisori(24, 102, 66, 924)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment