Advertisement
Guest User

PrimeNumGen

a guest
Aug 20th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 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. using System.Diagnostics;
  7.  
  8. namespace primeNumGen
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<uint> primeList = new List<uint>();
  15.             Console.WriteLine("The first 1,000,000 prime numbers\nPress Enter to start:");
  16.             Console.ReadLine();
  17.             UInt32 index = 3;
  18.             primeList.Add(2);
  19.             Stopwatch stopWatch = new Stopwatch();
  20.             stopWatch.Start();
  21.             while(primeList.Count < 1000000)
  22.             {
  23.                 if (index != 5 && index % 5 == 0)
  24.                 {
  25.                     index += 2;
  26.                     continue;
  27.                 }
  28.                 if (isPrime(index))
  29.                 {
  30.                     primeList.Add(index);
  31.                 }
  32.                 index += 2;
  33.             }
  34.             stopWatch.Stop();
  35.             Console.WriteLine("It took " + (stopWatch.ElapsedMilliseconds * .001).ToString() + " seconds to calculate");
  36.             System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\PrimeNumbers.txt"); //Pick your path
  37.             foreach(uint listItem in primeList)
  38.             {
  39.                 file.WriteLine(listItem.ToString());
  40.             }
  41.             file.Close();
  42.             Console.WriteLine("Check downloads folder for file!\nPress Enter to Exit");
  43.             Console.ReadLine();
  44.         }
  45.  
  46.         public static bool isPrime(uint input)
  47.         {
  48.             if(input < 1)
  49.             {
  50.                 return false;
  51.             }
  52.             else
  53.             {
  54.                 UInt32 i;
  55.                 for (i = 3; i * i <= input; i+=2)
  56.                 {
  57.                     if (input % i == 0)
  58.                     {
  59.                         return false;
  60.                     }
  61.                 }
  62.             }
  63.             return true;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement