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
Filed under: OSN |
Leave a comment