Calculating the Greatest Common Divisor for two positive integers (simple)

조회 수: 49 (최근 30일)
Musaddiq Sajjad
Musaddiq Sajjad 2017년 8월 11일
답변: Mai Aljneibi 2018년 11월 30일
Hi,
I am very new to Matlab so please excuse my obvious questions. I am trying to construct a simple function that takes two integers as input and returns the GCD. I have used the following functions:
function [A, B] = getData(x,y) %function to get data from the user
x = input('Enter x');
y = input ('Enter y');
A = x;
B = y;
end
function [M, N] = adjust(A, B) %sort the values. N is the bigger value, M is the smaller one
if (A < B)
M = A;
N = B;
end
end
function [GCD] = GCD(M, N) %function to calculate the Greatest Common Divisor
while(true)
R = mod(N, M);
if (R > 0)
N = M;
M = R;
end
if (R == 0)
GCD = M;
end
end
end
function [result] = printData(GCD) %function to print the result
result = GCD;
display(result);
end
I now want to call these functions so that when I open the script, it takes two inputs from me and displays the GCD. But I am stuck on this part. Please guide me on how to compile this jumble of code properly. Thank you!
  댓글 수: 2
Jan
Jan 2017년 8월 12일
편집: Jan 2017년 8월 12일
The question is not simple. It is not clear, what you want to achieve with the function. Then modifying the code is much harder then rewriting it completely. E.g. the adjust function can be solved easier using min and max. Take a look into the code of gcd:
type gcd
Is this a homework? Then posting a running solution will be avoided to give you a chance to submit your solution.
Musaddiq Sajjad
Musaddiq Sajjad 2017년 8월 12일
well it is a homework but we have to define these specific functions and then call them. I dont mind rewriting my function using min and max
But i just need a clue on how to put all these separate functions together. Does the logic work? Even if it is not the best way to go about it, i would like to know if it works.

댓글을 달려면 로그인하십시오.

답변 (5개)

MSP
MSP 2017년 8월 12일
This is how u call the function.Calling getData and printData is useless.
A= input('Enter x')
B= input ('Enter y')
[M,N]=adjust(A,B);
[gcd]=GCD_new(M,N)
And you have some problems in your functions.Check the modifications
function [M, N] = adjust(A, B) %sort the values. N is the bigger value, M is the smaller one
if (A < B)
M = A;
N = B;
else
M=B
N=A
end
end
The logic on your GCD doesn't seem right.Make sure to save the functions in the same folder or directory.
function [GCD] = GCD_new(M,N)
R = mod(N, M)
if (R==0)
GCD=M;
else
R = mod(N,M);
N = M;
M = R;
GCD=M;
end
end
And even easier,
type "help GCD in command window,does it all for you"
  댓글 수: 2
Musaddiq Sajjad
Musaddiq Sajjad 2017년 8월 12일
Thank you for your corrections. I see you're just using the function and assigning the result to the variables, but I am required to use the printData and getData functions and call them. I also thought they were useless, to be honest.
Here is the question:
Write a Matlab program to print the gcd of two positive integers as follows: The main script should call:
 a function getData that accepts and returns two positive integer numbers.
 a function adjust that puts the larger in N and smaller in M.
 a function GCD that takes M and N then returns the gcd.
 a function printResult to print the gcd.
Musaddiq Sajjad
Musaddiq Sajjad 2017년 8월 12일
In other language you just do something like
getData();
to call a function and it runs the code. That does not seem to be working here.

댓글을 달려면 로그인하십시오.


Jan
Jan 2017년 8월 12일
You need a main function to call the functions:
function main
[A, B] = getData;
G = GCD(A, B); % Do not call the output like the function
printData(G)
end
Now simplify your functions: getData does not get inputs, if you overwrite them immediately. Create A and B directly and omit x and y.
The output printGCD should not return anything. disp(G) is enough.
The sorting is easier by min(A,B) and max(A,B).
The main problem remains your GCD function: It does not work. It is not correct mathematically yet. Take a look at the algorithm again. And currently you have an infinite loop. When do you want to exit it?
Beside Matlab's gcd command, you find the algorithm at WikiPedia also.

Musaddiq Sajjad
Musaddiq Sajjad 2017년 8월 12일
How does this look?
function main
[A, B] = getData; %it shows an error here (too many output arguments)
adjust(A, B);
GCD(A, B);
printResult;
function getData %function to get data from the user
A = input('Enter x');
B = input('Enter y');
end
function [M, N] = adjust(A, B) %sort the values.
if (A < B)
M = A;
N = B;
else
M = B;
N = A;
end
end
function [GCD] = GCD(M,N) %i think I get the logic here now, my while loop was unnecessary
R = mod(N, M) %take the mod
if (R==0) %remainder = 0, then M is GCD
GCD=M;
else %otherwise
R = mod(N,M); %take the mod
N = M; %swap the values
M = R;
GCD = M; %GCD is M after loop ends
end
end
function printResult %function to print the result
display(GCD)
end
end
  댓글 수: 3
Musaddiq Sajjad
Musaddiq Sajjad 2017년 8월 12일
The GCD logic doesnt seem to be working. I'll read up some algorithms
Abdulrehman Samiullah
Abdulrehman Samiullah 2017년 8월 12일
편집: Abdulrehman Samiullah 2017년 8월 12일
Can we write all these functions in one .m file??

댓글을 달려면 로그인하십시오.


Musaddiq Sajjad
Musaddiq Sajjad 2017년 8월 12일
HEY IT WORKS NOW. Thank you everyone =)
just had to use the while loop properly.
function [GCD] = GCD(N,M)
while (M > 0)
Q = N / M;
R = mod(N, M);
N = M;
M = R;
end
GCD = N;
end

Mai Aljneibi
Mai Aljneibi 2018년 11월 30일
How can we write a recursive function that returns the greatest common divisor of two positive integers.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by