Example input
Code: Function that finds the greatest common divisor of two numbers.
PL: Python, C, Rust, Go, JavaScript
Example output
Sure! Here's the pseudocode for the function that finds the greatest common divisor of two numbers:
Function find_gcd(num1, num2):
while num2:
num1, num2 = num2, num1 % num2
return num1
And here's the code in Python:
def find_gcd(num1, num2):
while num2:
num1, num2 = num2, num1 % num2
return num1
And here's the code in C:
int find_gcd(int num1, int num2) {
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
And here's the code in Rust:
fn find_gcd(num1: i32, num2: i32) -> i32 {
let mut temp;
let mut a = num1;
let mut b = num2;
while b != 0 {
temp = b;
b = a % b;
a = temp;
}
return a;
}
And here's the code in Go:
func findGcd(num1 int, num2 int) int {
var temp int
var a, b int = num1, num2
for b != 0 {
temp = b
b = a % b
a = temp
}
return a
}
And here's the code in JavaScript:
function findGcd(num1, num2) {
while (num2) {
let temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
In all the programming languages, we use the same logic to find the greatest common divisor (GCD) of two numbers, which is the Euclidean algorithm. We repeatedly take the remainder of the larger number divided by the smaller number until the remainder is zero. The final non-zero remainder is the GCD of the two numbers.