Created
March 24, 2019 16:32
-
-
Save mcihad/05ce6a59b08d06b00bf71eb83885559f to your computer and use it in GitHub Desktop.
C# prime generator
This file contains hidden or 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
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