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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

148 NÚMEROS PRIMOS Y FACTORIZACIÓN.<br />

Algoritmo 9.1: Criba <strong>de</strong> Eratóstenes<br />

Datos: n ∈ N<br />

Salida: Primos entre 2 y n<br />

máx= (n − 3)/2;<br />

boolean esPrimo[i], i = 1,2,...,máx;<br />

for i = 1,2,...,máx do<br />

esPrimo[i] =True;<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

i = 0;<br />

while (2i + 3)(2i + 3) ≤ n do<br />

k = i + 1;<br />

if esPrimo(i) then<br />

while (2k + 1)(2i + 3) ≤ n do<br />

esPrimo[((2k + 1)(2i + 3) − 3)/2] =False;<br />

k = k + 1;<br />

i = i + 1;<br />

Imprimir;<br />

for j = 1,2,...,máx do<br />

if esPrimo[j] =True then<br />

Imprima j<br />

import java.math.BigInteger;<br />

public c<strong>la</strong>ss Teoria_Numeros<br />

{ ...<br />

public static Vector Primos(int n)<br />

{<br />

Vector salida = new Vector(1);<br />

int k = 1;<br />

int max = (n-3)/2;<br />

boolean[] esPrimo = new boolean[max+1];<br />

for(int i = 0; i

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

Saved successfully!

Ooh no, something went wrong!