Advertisement
Guest User

Trabajo N°0 Ejercicio 7

a guest
Aug 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TP0_Ejerc7
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Los números primos son:");
  14.             DateTime inicio = DateTime.Now;
  15.            
  16.             int acumulador = 0;
  17.             int n = 2;
  18.             while (acumulador<=10000)
  19.             {
  20.                 if (n == 2)
  21.                 {
  22.                     Console.Write(n + ", ");
  23.                     n++;
  24.                 }
  25.                 else
  26.                 {
  27.                     if (isprime(n))
  28.                     {
  29.                         if (acumulador < 10000)
  30.                         {
  31.                             Console.Write(n + ", ");
  32.                             acumulador++;
  33.                             n += 2;
  34.                         }
  35.                         else
  36.                         {
  37.                             Console.WriteLine(n);
  38.                             acumulador++;
  39.                         }
  40.                     }
  41.                     else
  42.                     {
  43.                         n += 2;
  44.                     }
  45.                 }
  46.             }
  47.            
  48.             DateTime fin = DateTime.Now;
  49.             Console.WriteLine("El tiempo de ejecución del algortimo es: " + (fin - inicio));
  50.             Console.ReadKey();
  51.         }
  52.  
  53.         static bool isprime(int n)
  54.         {
  55.                  for (int i = 3; i < n; i += 2)
  56.                  {
  57.                     if (n % i == 0)
  58.                     {
  59.                        return false;
  60.                     }
  61.                  }
  62.                  return true;
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement