Created
February 6, 2021 08:26
-
-
Save nikescar1/82e07f136a8b20a16a93a2615c1af702 to your computer and use it in GitHub Desktop.
FindingPrimes C# Console Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace FindingPrimes | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int runs = 10; | |
int startNumber = 1; | |
int endNumber = 200000; | |
for (int i = 0; i < runs; i++) | |
{ | |
List<int> primes = new List<int>(20000); | |
int numberOfPrimes = 0; | |
bool foundPrime = false; | |
var watch = System.Diagnostics.Stopwatch.StartNew(); | |
for (int candidateNumber = startNumber; candidateNumber <= endNumber; candidateNumber++) | |
{ | |
foundPrime = true; | |
for (int divNumber = 2; divNumber < candidateNumber; divNumber++) | |
{ | |
if (candidateNumber % divNumber == 0) | |
{ | |
foundPrime = false; | |
break; | |
} | |
} | |
if (foundPrime) | |
{ | |
primes.Add(candidateNumber); | |
numberOfPrimes++; | |
} | |
} | |
watch.Stop(); | |
Console.WriteLine($"Find all primes up to: {endNumber}"); | |
Console.WriteLine($"Time elasped: {String.Format("{0:0.00}", watch.ElapsedMilliseconds * .001f)} seconds"); | |
Console.WriteLine($"Number of primes found {primes.Count}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment