21.12.2012 Views

1.5 Algoritmo de Euclides con menor resto. - TEC-Digital

1.5 Algoritmo de Euclides con menor resto. - TEC-Digital

1.5 Algoritmo de Euclides con menor resto. - TEC-Digital

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

Revista digital Matemática, Educación e I nternet (www.cidse.itcr.ac.cr/revistamate/). Vol 10, No 2. , Marzo 2010. 15<br />

Si llamamos a los nuevos divi<strong>de</strong>ndos d1 = b, d2, d3, etc., entonces si proce<strong>de</strong>mos por inducción,<br />

en el n−ésimo <strong>resto</strong> tendríamos<br />

0 ≤ rn < dn · 2 sn < dn−1 · 2 s n−1 < ... < b · 2 s 1<br />

Es <strong>de</strong>cir, cada nuevo <strong>resto</strong> r i está en un intervalo [0, d i · 2 s i] cada vez más pequeño. Como el<br />

número <strong>de</strong> intervalos es finito, la sucesión <strong>de</strong> <strong>resto</strong>s es finita y por tanto en algún momento<br />

rn+1 = 0.<br />

Este algoritmo no es tan rápido como el algoritmo binario, pero su versión extendida si es más<br />

eficiente que la versión extendida <strong>de</strong> Eucli<strong>de</strong>s y la versión extendida <strong>de</strong>l algoritmo binario.<br />

1.7.1 <strong>Algoritmo</strong> e Implementación.<br />

El algoritmo es como sigue,<br />

<strong>Algoritmo</strong> 1.3: <strong>Algoritmo</strong> LSBGCD<br />

Entrada: a, b ∈ Z + y a > b<br />

Resultado: mcd (a, b)<br />

while b �= 0 do<br />

Calcule s tal que b · 2s ≤ a < 2s+1b; t = Mín{a − b · 2s , b · 2s+1 − a};<br />

a = b; b = t;<br />

if a < b then<br />

Intercambiar(a, b)<br />

return a<br />

Implementación. Necesitamos una función Min(a,b). Aunque po<strong>de</strong>mos usar la función Mín <strong>de</strong><br />

Calc (vía createUnoService("com.sun.star.sheet.FunctionAccess")), aquí no vamos<br />

a usar esta posibilidad, más bien usamos una función sencilla.<br />

Function Min (a,b) As Long<br />

Dim m As Long<br />

m=a<br />

If a>b Then<br />

m=b<br />

End If<br />

Min= m<br />

End Function<br />

Function LSBMCD(u,v) As Long<br />

Dim a As Long, b As Long, t As Long, aux As Long<br />

Dim s As Integer<br />

a=Abs(u) : b=Abs(v) rem <strong>de</strong>be ser a>b<br />

While b0<br />

s=0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!