Bimbingan Olimpiade Komputer : Pembahasan 2. OSK 2007

Soal 37 (OSK 2007)
4checks.com-Free Shipping and Handling on All Personal Checks with code DWF008

function apaini(a:integer; b: integer):integer;
var
x,y,r : integer;
begin
     x := a;
     y := b;
     while (y <> 0) do
     begin
          r := x mod y;
          x := y;
          y := r;
     end;
     apaini:=x;
end;

begin
writeln(apaini(414,662));
readln;
end.

Hasilnya adalah 2

Langkah-langkah penyelesaiannya :

while (y <> 0)         r = x mod y           x = y         y = r
        (662 <> 0)     r = 414 mod 662     x = 662      y = 414
                           r = 414
        (414 <> 0)     r = 662 mod 414     x = 414      y = 248
                           r = 248
        (248 <> 0)     r = 414 mod 248     x = 248      y = 166
                           r = 166
        ........
        ........
        (2 <> 0)        r = 81 mod 2          x = 2          y = 0
                           r = 0
        (0 <> 0)        Perulangan while berhenti...

Sehingga mendapatkan nilai x = 2

Link Yang berhubungan :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: