27.12.2012 Views

Introducción a la teoría de números. Ejemplos y - TEC-Digital

Introducción a la teoría de números. Ejemplos y - TEC-Digital

Introducción a la teoría de números. Ejemplos y - 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.

24 DIVISIBILIDAD<br />

2.4.1 Algoritmo e implementación.<br />

Recor<strong>de</strong>mos que mod(a,b) <strong>de</strong>nota el resto <strong>de</strong> <strong>la</strong> división <strong>de</strong> a por b.<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

Algoritmo 2.2: Máximo común divisor<br />

Datos: a, b ∈ Z.<br />

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

if a = 0 y b = 0 then<br />

return mcd(a,b) = 0<br />

c = |a|, d = |b|;<br />

while d �= 0 do<br />

r = rem(c,d);<br />

c = d;<br />

d = r;<br />

return mcd(a,b) = |c|;<br />

Implementación en Excel.<br />

1 Function mcd(a, b)<br />

2 Dim c As Long, d As Long, r As Long ’max = 2 147 483 647<br />

3 If a = 0 And b = 0 Then<br />

4 mcd = 0<br />

5 End If<br />

6 c = Abs(a)<br />

7 d = Abs(b)<br />

8 While d 0<br />

9 r = c Mod d ’residuo entre (c,d)<br />

10 c = d<br />

11 d = r<br />

12 Wend<br />

13 mcd = Abs(c)<br />

14 End Function<br />

Para Calcu<strong>la</strong>r el mcd <strong>de</strong> una lista en una columna <strong>de</strong> hoja <strong>de</strong> Excel, se <strong>de</strong>be seleccionar <strong>la</strong> lista.<br />

Private Sub mcmLista_Click()<br />

Dim n, i<br />

Dim Rango As Range<br />

Dim Lista() As Long<br />

Set Rango = Selection ’Seleccionar y convertir Range a Array<br />

n = Rango.Rows.Count ’Solo fi<strong>la</strong>s

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

Saved successfully!

Ooh no, something went wrong!