PrimeNumberGenerator 1.0.0

dotnet add package PrimeNumberGenerator --version 1.0.0
NuGet\Install-Package PrimeNumberGenerator -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PrimeNumberGenerator" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PrimeNumberGenerator --version 1.0.0
#r "nuget: PrimeNumberGenerator, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PrimeNumberGenerator as a Cake Addin
#addin nuget:?package=PrimeNumberGenerator&version=1.0.0

// Install PrimeNumberGenerator as a Cake Tool
#tool nuget:?package=PrimeNumberGenerator&version=1.0.0

PrimeNumberGenerator

A lightning fast Prime Number generator created with C# in .NET 6.0.

About

Initially started as a casual project that fulfills the requirements of Prime Streaming (PG-13) on CodeWars, the project has since been better optimized and documented.

PrimeNumberGenerator is an (in)finite stream of prime numbers, with its upper limit defined by the user upon instantiation.

Simple to use and fast - this project is a great solution for those who need a constant stream of prime numbers with little downtime.

The default PrimeGenerator (based on Sieve of Atkin) is capable of generating a sequence of 50 Million primes in less than 3 seconds.

The ErastothenesPrimeGenerator (based on Sieve of Erastothenes) is significantly slower, and generates 50 Million primes in about 11 seconds.

Prerequisites

  • .NET 6.0

Usage

The limit of numbers to check for primes (NOT the number of primes in the stream) is passed to the constructor, with the default value being 982_451_654 - enough to generate 50 million primes.

Calling Stream() will return an IEnumerable<int> that is easy to manipulate using LINQ methods, such as Skip() or Take().

Due to the usage of yield, the method will return one element at a time.

Examples:

// Get the list of first 10_000 primes.
var primes = new PrimeGenerator(200_000).Take(10_000).ToList();

// Get the 50 millionth prime.
var prime = new PrimeGenerator().Skip(49_999_999).Take(1).First();

// Display all primes up to 100_000.
var generator = new PrimeGenerator(100_000);
foreach (var prime in generator.Stream())
{
  Console.WriteLine(prime);
}

The limit of numbers, passed as parameter to constructor, can be a great performance improvement, so if possible, you should only take as many numbers as you need.

Memory consumed

PrimeGenerator operates using BitArray, which only requires 1 bit per element. Therefore, the amount of memory to store the first 50 million primes is only about:

982_451_654 b / (8 * 1024) = 120_000 KB = 118 MB

Benchmarks

Generator Amount of primes Mean Error StdDev Allocated
Default 50 Million 2778 ms 10.44 ms 9.77 ms 11056 B
Erastothenes 50 Million 10.669 s 0.0465 s 0.0388 s 664 B
Default 5 Million 252 ms 0.54 ms 0.51 ms 868 B
Erastothenes 5 Million 9.009 s 0.0120 s 0.0106 s 664 B

Reference

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 200 9/28/2022