using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace primeNumGen
{
class Program
{
static void Main(string[] args)
{
List<uint> primeList = new List<uint>();
Console.WriteLine("The first 1,000,000 prime numbers\nPress Enter to start:");
Console.ReadLine();
UInt32 index = 3;
primeList.Add(2);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
while(primeList.Count < 1000000)
{
if (index != 5 && index % 5 == 0)
{
index += 2;
continue;
}
if (isPrime(index))
{
primeList.Add(index);
}
index += 2;
}
stopWatch.Stop();
Console.WriteLine("It took " + (stopWatch.ElapsedMilliseconds * .001).ToString() + " seconds to calculate");
System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\PrimeNumbers.txt"); //Pick your path
foreach(uint listItem in primeList)
{
file.WriteLine(listItem.ToString());
}
file.Close();
Console.WriteLine("Check downloads folder for file!\nPress Enter to Exit");
Console.ReadLine();
}
public static bool isPrime(uint input)
{
if(input < 1)
{
return false;
}
else
{
UInt32 i;
for (i = 3; i * i <= input; i+=2)
{
if (input % i == 0)
{
return false;
}
}
}
return true;
}
}
}