Fandom

Python Wiki

Greatest common denominator

43pages on
this wiki
Add New Page
Talk1 Share

This is a code sample to calculate the greatest common denominator of two integers.

 def gcd(a, b):
     while b != 0:
         (a, b) = (b, a%b)
     return a
 
 
 #For n>=2 integers
 
 def Euclid(*integers):
     if type(integers[0])==list or type(integers[0])==tuple:
 	integers = integers[0]
     if len(integers) == 2:
 	a,b = integers[0],integers[1]
 	while b != 0:
 		a,b = b, a%b
 	return a
     else:
 	return Euclid(integers[0],Euclid(integers[1:]))

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.