Last active
December 11, 2015 08:29
-
-
Save Measter/4573719 to your computer and use it in GitHub Desktop.
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
private static IEnumerable<int> FindFirstNumPrimes( int limit ) | |
{ | |
List<int> primes = new List<int>( new[] { 2 } ); | |
bool isPrime; | |
int i = 1; | |
while( primes.Count < limit ) | |
{ | |
i += 2; | |
isPrime = true; | |
foreach( int prime in primes ) | |
{ | |
if( i % prime == 0 ) | |
{ | |
isPrime = false; | |
break; | |
} | |
} | |
if( isPrime ) | |
primes.Add( i ); | |
} | |
return primes; | |
} |
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
private static IEnumerable<int> FindFirstNumPrimesFilter( int limit ) | |
{ | |
List<int> primes = new List<int>( new[] { 2 } ); | |
IEnumerable<int> tprimeList; | |
bool isPrime; | |
int i = 1; | |
while( primes.Count < limit ) | |
{ | |
i += 2; | |
isPrime = true; | |
double sqrtI = Math.Sqrt( i ); | |
if ( sqrtI == Math.Floor( sqrtI ) && primes.Contains( (int)sqrtI ) ) | |
continue; | |
tprimeList = primes.Where( j => j < sqrtI ); | |
foreach( int prime in tprimeList ) | |
{ | |
if( i % prime == 0 ) | |
{ | |
isPrime = false; | |
break; | |
} | |
} | |
if( isPrime ) | |
primes.Add( i ); | |
} | |
return primes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment