Foro de elhacker.net

Programación => .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
  1. // ****************************************************************
  2. // ****************************************************************
  3. // **                                                            **
  4. // **                                                            **
  5. // **               Criba de Eratóstenes en C#                   **
  6. // **                       By Hendrix                           **
  7. // **                                                            **
  8. // **                                                            **
  9. // ****************************************************************
  10. // ****************************************************************
  11.  
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Text;
  15.  
  16. namespace ConsoleApplication1
  17. {
  18.    class Program
  19.    {
  20.        public static int max;
  21.  
  22.        static void Main(string[] args)
  23.        {
  24.            int nume;
  25.  
  26.            Console.WriteLine("Introduce el numero hasta donde se va a buscar");
  27.            max = Convert.ToInt32(Console.ReadLine());
  28.  
  29.            int[,] num = new int[max, 2];
  30.            string primos = "";
  31.  
  32.            for (int i = 2; i < max; i++) //Rellenamos el array
  33.            {
  34.                num[i, 0] = i;
  35.                num[i, 1] = 0;
  36.            }
  37.  
  38.            for (int i = 2; i < max; i++)
  39.            {
  40.                if (num[i, 1] == 0)
  41.                {
  42.                    primos = primos + i.ToString() + ",";
  43.                    for (int e = 1; e < max + 1; e++)
  44.                    {
  45.                        nume = num[i, 0];
  46.                        nume = nume * e;
  47.                        if (nume < max)
  48.                        {
  49.                            num[nume, 1] = 1;
  50.                        }
  51.                        else
  52.                        {
  53.                            break;
  54.                        }
  55.                    }
  56.                }
  57.            }
  58.  
  59.            primos = primos.Substring(0, primos.Length - 1);
  60.            Console.WriteLine("Numeros primos encontrados: {0}", primos);
  61.            Console.Read();
  62.  
  63.        }
  64.    }
  65. }

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.