필터 지우기
필터 지우기

Trying to write a program to implement Euler's Algorithm????

조회 수: 1 (최근 30일)
Reelz
Reelz 2012년 4월 16일
I am trying to write a program to implement Euler's algorithm. I know that the greatest common divisor of (a,b,c) = gcd(gcd(a,b),c). I also want to write another program to compute the gcd of three natural numbers. I am having trouble doing this though. Here is what I know, that a = b*(quotient q) + remainder(r), where a>b and b>r, so that to find the gcd(a,b) I could replace a and b by b and r and repeat the process. The successive remainders get smaller until I eventually get r = 0. I just simply am confused on how to go about writing a program for this.
  댓글 수: 4
Reelz
Reelz 2012년 4월 23일
I have the code down, but now I am trying to compute three outputs, I keep running into trouble though. Help please?
clear; clc
% input, take only positive numbers into account
a = input('First number: ');
b = input('Second number: ');
c = input('Third number: ');
a = abs(a);
b = abs(b);
c= abs(c);
r = a - b*floor(a/b);
% Repeat the operation till updates of a = # of update b
while r ~= 0
a > b;
a = b;
b = r;
r = a - b*floor(a/b);
end
% Show result
GCD = b
Geoff
Geoff 2012년 4월 23일
Eh?? What are the other two outputs. I understand you want to compute GCD(a,b,c). Is that correct? If so, read my last comment. Make a function to compute GCD of two numbers. I don't see any function definitions in your code.

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

답변 (1개)

Geoff
Geoff 2012년 4월 18일
Have you considered doing a google for the Euclidean Algorithm?
Try the very first search result =P
Halfway down the page, there's very concise pseudocode.

카테고리

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