A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by rzuckerm
Welcome to the Prime Number in C# page! Here, you'll find the source code for this program as well as a description of how the program works.
if (args is not [var raw] || !ulong.TryParse(raw, out ulong number))
return ExitWithUsage();
Console.WriteLine(IsPrime(number) ? "Prime" : "Composite");
return 0;
static bool IsPrime(ulong value)
{
if (value < 2)
return false;
if (value == 2)
return true;
if (value % 2 == 0)
return false;
for (ulong divisor = 3; divisor * divisor <= value; divisor += 2)
{
if (value % divisor == 0)
return false;
}
return true;
}
static int ExitWithUsage()
{
Console.WriteLine("Usage: please input a non-negative integer");
return 1;
}
Prime Number in C# was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.