Find the greatest common divisor function gcd of two numbers based on matlab

1. gcd function

In MATLAB, you can use the built-in function gcd() to find the greatest common divisor of two numbers.

2. Example

The following is an example code for using MATLAB to find the greatest common divisor of two numbers:

% 定义两个数
a = 36;
b = 48;
% 求最大公约数
gcd_result = gcd(a, b);
% 输出结果
fprintf('最大公约数为:%d\n', gcd_result);

Output result:

In this example, we define two numbers a and b. Then, use the gcd() function to find the greatest common divisor of a and b, and save the result in the gcd_result variable. Finally, use the fprintf() function to output the results to the command window. You can replace a and b in the code with the two numbers you need to find the greatest common divisor, and then run the code to get the result of the greatest common divisor.

Guess you like

Origin blog.csdn.net/weixin_45770896/article/details/132917455