Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Hendrix en 29 Septiembre 2007, 23:08 PM

Título: [C#] Criba de Eratóstenes
Publicado por: Hendrix en 29 Septiembre 2007, 23:08 PM
Aqui tienen, sirve para encontrar numeros primos hasta el numero que ustedes indiquen.

Código (csharp) [Seleccionar]
// ****************************************************************
// ****************************************************************
// **                                                            **
// **                                                            **
// **               Criba de Eratóstenes en C#                   **
// **                       By Hendrix                           **
// **                                                            **
// **                                                            **
// ****************************************************************
// ****************************************************************

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public static int max;

        static void Main(string[] args)
        {
            int nume;

            Console.WriteLine("Introduce el numero hasta donde se va a buscar");
            max = Convert.ToInt32(Console.ReadLine());

            int[,] num = new int[max, 2];
            string primos = "";

            for (int i = 2; i < max; i++) //Rellenamos el array
            {
                num[i, 0] = i;
                num[i, 1] = 0;
            }

            for (int i = 2; i < max; i++)
            {
                if (num[i, 1] == 0)
                {
                    primos = primos + i.ToString() + ",";
                    for (int e = 1; e < max + 1; e++)
                    {
                        nume = num[i, 0];
                        nume = nume * e;
                        if (nume < max)
                        {
                            num[nume, 1] = 1;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            primos = primos.Substring(0, primos.Length - 1);
            Console.WriteLine("Numeros primos encontrados: {0}", primos);
            Console.Read();

        }
    }
}


Para mas información sobre la Criba de Eratóstenes aquí (http://es.wikipedia.org/wiki/Criba_de_Erat%C3%B3stenes)

Un Saludo   :)

Título: Re: [C#] Criba de Eratóstenes
Publicado por: Meta en 30 Septiembre 2007, 12:49 PM
Es curioso, funciona muy bien, gracias.