Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
def gcd_subtraction(a, b): while a != b: if a > b: print(f"{a} - {b} = {a-b}") a = a - b else: print(f"{b} - {a} = {b-a}") b = b - a return a a = 120 b = 25 print("The Euclidean algorithm using subtraction:\n") print(f"The GCD of {a} and {b} is: {gcd_subtraction(a, b)}") #Python
#include
int gcdSubtraction(int a, int b) { while (a != b) { if (a > b) { printf("%d - %d = %d\n", a, b, a - b); a = a - b; } else { printf("%d - %d = %d\n", b, a, b - a); b = b - a; } } return a; } int main() { int a = 120; int b = 25; printf("The Euclidean algorithm using subtraction:\n\n"); printf("The GCD of %d and %d is: %d\n", a, b, gcdSubtraction(a, b)); return 0; } //C
public class Main { public static int gcdSubtraction(int a, int b) { while (a != b) { if (a > b) { System.out.println(a + " - " + b + " = " + (a - b)); a = a - b; } else { System.out.println(b + " - " + a + " = " + (b - a)); b = b - a; } } return a; } public static void main(String[] args) { int a = 120; int b = 25; System.out.println("The Euclidean algorithm using subtraction:\n"); System.out.println("The GCD of " + a + " and " + b + " is: " + gcdSubtraction(a, b)); } } //Java
Python result:
C result:
Java result:
The Euclidean algorithm using subtraction:
120 - 25 = 95
95 - 25 = 70
70 - 25 = 45
45 - 25 = 20
25 - 20 = 5
20 - 5 = 15
15 - 5 = 10
10 - 5 = 5
The GCD of 120 and 25 is: 5
The Euclidean algorithm using subtraction:
120 - 25 = 95
95 - 25 = 70
70 - 25 = 45
45 - 25 = 20
25 - 20 = 5
20 - 5 = 15
15 - 5 = 10
10 - 5 = 5
The GCD of 120 and 25 is: 5
The Euclidean algorithm using subtraction:
120 - 25 = 95
95 - 25 = 70
70 - 25 = 45
45 - 25 = 20
25 - 20 = 5
20 - 5 = 15
15 - 5 = 10
10 - 5 = 5
The GCD of 120 and 25 is: 5