Prime numbers. A prime number has no divisors (other than itself and 1). This method checks for prime numbers fast. Prime numbers are computed in the .NET Framework.
Primes are used in many programs—they are used in the Dictionary class. They help with hashing. The .NET Framework provides a method for testing primes.
An example. The algorithm here determines if a specific number is a prime number. It can be found in the System.Core assembly in the .NET Framework, in the HashHelpers class.
Note IsPrime is defined in the PrimeTool class, and it is a public static method. It does not save state.
using System;
class Program
{
static void Main()
{
//// Write prime numbers between 0 and 100.//
Console.WriteLine("--- Primes between 0 and 100 ---");
for (int i = 0; i < 100; i++)
{
bool prime = PrimeTool.IsPrime(i);
if (prime)
{
Console.Write("Prime: ");
Console.WriteLine(i);
}
}
//// Write prime numbers between 10000 and 10100//
Console.WriteLine("--- Primes between 10000 and 10100 ---");
for (int i = 10000; i < 10100; i++)
{
if (PrimeTool.IsPrime(i))
{
Console.Write("Prime: ");
Console.WriteLine(i);
}
}
}
}using System;
public static class PrimeTool
{
public static bool IsPrime(int candidate)
{
// Test whether the parameter is a prime number.
if ((candidate & 1) == 0)
{
if (candidate == 2)
{
return true;
}
else
{
return false;
}
}
// Note:// ... This version was changed to test the square.// ... Original version tested against the square root.// ... Also we exclude 1 at the end.
for (int i = 3; (i * i) <= candidate; i += 2)
{
if ((candidate % i) == 0)
{
return false;
}
}
return candidate != 1;
}
}--- Primes between 0 and 100 ---
Prime: 2
Prime: 3
Prime: 5
Prime: 7
Prime: 11
Prime: 13
Prime: 17
Prime: 19
Prime: 23
Prime: 29
Prime: 31
Prime: 37
Prime: 41
Prime: 43
Prime: 47
Prime: 53
Prime: 59
Prime: 61
Prime: 67
Prime: 71
Prime: 73
Prime: 79
Prime: 83
Prime: 89
Prime: 97
--- Primes between 10000 and 10100 ---
Prime: 10007
Prime: 10009
Prime: 10037
Prime: 10039
Prime: 10061
Prime: 10067
Prime: 10069
Prime: 10079
Prime: 10091
Prime: 10093
Prime: 10099
HashHelpers. This is used in Dictionary. This class contains an integer array of prime numbers that are preferred for hashtables. These numbers were selected for performance.
And This method uses the HashHelpers class, which contains the GetPrime method.
Tip When the Dictionary must resize to have a capacity of more than 7199369 buckets, the IsPrime method is used in a loop.
Tip 2 This code is not run on smaller Dictionaries but is part of all Dictionary instances.
private void Initialize(int capacity)
{
int prime = HashHelpers.GetPrime(capacity);
this.buckets = new int[prime];
// ...// ...
}
Update. The IsPrime method previously here had some problems. It considered 1 a prime number, and the Math.Sqrt method was slower than squaring the induction variable (i).
Important Please notice that the method is no longer the same version found in the .NET Framework.
A summary. This logic determines whether a number is a prime number. We described the algorithmic design of the IsPrime method, which provides optimized logic for testing number factors.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 17, 2023 (edit).