Prime Numbers in C# QuickStart Sample
Illustrates working with prime numbers and the IntegerMath class in the Numerics.NET namespace in C#.
View this sample in: Visual Basic F# IronPython
using System;
// We use many classes from the Numerics.NET.SpecialFunctions
// namespace.
using Numerics.NET;
namespace Numerics.NET.QuickStart.CSharp
{
/// <summary>
/// Illustrates working with prime numbers using the
/// IntegerMath class in the Numerics.NET.SpecialFunctions
/// namespace of Numerics.NET.
/// </summary>
class PrimeNumbers
{
static void Main(string[] args)
{
// The license is verified at runtime. We're using
// a 30 day trial key here. For more information, see
// https://numerics.net/trial-key
Numerics.NET.License.Verify("64542-18980-57619-62268");
//
// Factoring numbers
//
// The Factor method returns a sequence of pairs of the prime factors
// and their multiplicity:
int index;
int n = 1001110110;
var factors = IntegerMath.Factor(n);
Console.Write($"{n} = ");
foreach (var factor in factors)
{
Console.Write($" * {factor.Item1}");
if (factor.Item2 > 1)
Console.Write($"^{factor.Item2}");
}
Console.WriteLine();
// Factors that occur multiple times is repeated as many times as necessary:
n = 256 * 6157413;
factors = IntegerMath.Factor(n);
Console.Write($"{n} = ");
foreach (var factor in factors)
{
Console.Write($" * {factor.Item1}");
if (factor.Item2 > 1)
Console.Write($"^{factor.Item2}");
}
Console.WriteLine();
// The 64bit version can safely factor numbers up to 48 bits long:
long n2 = 1296523 * 1177157L;
Console.Write($"{n2} = ");
foreach (var factor in factors)
{
Console.Write($" * {factor.Item1}");
if (factor.Item2 > 1)
Console.Write($"^{factor.Item2}");
}
Console.WriteLine();
//
// Prime numbers
//
// The IsPrime method verifies if a number is prime or not.
n = 801853937;
Console.WriteLine("{0} is prime? {1}!", n, IntegerMath.IsPrime(n));
n = 801853939;
Console.WriteLine("{0} is prime? {1}!", n, IntegerMath.IsPrime(n));
// MextPrime gets the first prime after a specified number.
// You can call it repeatedly to get successive primes.
// Let//s get the 10 smallest primes larger than one billion:
n = 1000000000;
Console.WriteLine("\nFirst 10 primes greater than 1 billion:");
for(index = 0; index < 10; index++)
{
n = IntegerMath.NextPrime(n);
Console.Write("{0,16}", n);
}
Console.WriteLine();
// PreviousPrime gets the last prime before a specified number.
n = 1000000000;
Console.WriteLine("Last 10 primes less than 1 billion:");
for(index = 0; index < 10; index++)
{
n = IntegerMath.PreviousPrime(n);
Console.Write("{0,16}", n);
}
Console.WriteLine();
Console.Write("Press Enter key to exit...");
Console.ReadLine();
}
}
}