執行 ❯
建立您
自己的
網站
×
更改方向
更改主題,深色/淺色
前往 Spaces
Python
C
Java
def gcd_division(a, b): while b != 0: remainder = a % b print(f"{a} = {a//b} * {b} + {remainder}") a = b b = remainder return a a = 120 b = 25 print("The Euclidean algorithm using division:\n") print(f"The GCD of {a} and {b} is: {gcd_division(a, b)}") #Python
#include <stdio.h> int gcdDivision(int a, int b) { while (b != 0) { int remainder = a % b; printf("%d = %d * %d + %d\n", a, a / b, b, remainder); a = b; b = remainder; } return a; } int main() { int a = 120; int b = 25; printf("The Euclidean algorithm using division:\n\n"); printf("The GCD of %d and %d is: %d\n", a, b, gcdDivision(a, b)); return 0; } //C
public class Main { public static int gcdDivision(int a, int b) { while (b != 0) { int remainder = a % b; System.out.println(a + " = " + (a / b) + " * " + b + " + " + remainder); a = b; b = remainder; } return a; } public static void main(String[] args) { int a = 120; int b = 25; System.out.println("The Euclidean algorithm using division:\n"); System.out.println("The GCD of " + a + " and " + b + " is: " + gcdDivision(a, b)); } } //Java
Python 結果
C 結果
Java 結果
使用除法的歐幾里得演算法
120 = 4 * 25 + 20
25 = 1 * 20 + 5
20 = 4 * 5 + 0
120 和 25 的最大公約數是:5
使用除法的歐幾里得演算法
120 = 4 * 25 + 20
25 = 1 * 20 + 5
20 = 4 * 5 + 0
120 和 25 的最大公約數是:5
使用除法的歐幾里得演算法
120 = 4 * 25 + 20
25 = 1 * 20 + 5
20 = 4 * 5 + 0
120 和 25 的最大公約數是:5