File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Filter options
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Original file line number Diff line number Diff line change
1
+ """
2
+ In mathematics, the greatest common divisor (gcd) of two or more integers,
3
+ which are not all zero, is the largest positive integer that divides each of the integers.
4
+ For example, the gcd of 8 and 12 is 4.
5
+ » https://en.wikipedia.org/wiki/Greatest_common_divisor
6
+
7
+ Due to limited recursion depth this algorithm is not suited for calculating the GCD of big integers.
8
+ """
9
+
10
+ def recGCD (x , y , div = 0 ):
11
+ # Detemine which integer is greater and set the divisor accordingly
12
+ if div == 0 :
13
+ if x > y :
14
+ div = x
15
+ else :
16
+ div = y
17
+ # If both integers can be divided without a remainder the gcd has been found
18
+ if x % div == 0 and y % div == 0 :
19
+ return div
20
+ # Decrease divisor by one and try again
21
+ else :
22
+ return recGCD (x , y , div - 1 )
23
+
24
+ x = int (input ("x = " ))
25
+ y = int (input ("y = " ))
26
+ print (f"gcd({ x } , { y } ) = { recGCD (x ,y )} " )
You can’t perform that action at this time.
0 commit comments