Skip to content

Instantly share code, notes, and snippets.

@mcihad
Created March 24, 2019 16:32
Show Gist options
  • Save mcihad/05ce6a59b08d06b00bf71eb83885559f to your computer and use it in GitHub Desktop.
Save mcihad/05ce6a59b08d06b00bf71eb83885559f to your computer and use it in GitHub Desktop.
C# prime generator
class Prime
{
static bool IsPrime(int num)
{
int to = (int)Math.Round(Math.Sqrt(num));
return !Enumerable.Range(2, to).Any(i => num % i == 0);
}
public static IEnumerable<int> GetPrimes(int count)
{
int current = 2;
int counter = 0;
while (counter < count)
{
if (IsPrime(current))
{
counter++;
yield return current;
}
current++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment