Skip to content

Instantly share code, notes, and snippets.

@Measter
Last active December 11, 2015 08:29
Show Gist options
  • Save Measter/4573719 to your computer and use it in GitHub Desktop.
Save Measter/4573719 to your computer and use it in GitHub Desktop.
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;
}
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