Skip to content

Sieve of Eratosthenes. #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Algorithms.Tests/Other/SieveOfEratosthenesTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Numerics;
using System.Numerics;
using Algorithms.Other;
using FluentAssertions;
using NUnit.Framework;

namespace Algorithms.Tests.Other
{
public static class SieveOfEratosthenesTests
{
private static readonly BigInteger[] First10000PrimeNumbers =
private static readonly long[] First10000PrimeNumbers =
{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103,
107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
Expand Down Expand Up @@ -667,7 +668,17 @@ public static class SieveOfEratosthenesTests
};

[Test]
public static void First10_000PrimesCorrect() =>
Assert.AreEqual(First10000PrimeNumbers, SieveOfEratosthenes.GetPrimeNumbers(10_000));
public static void First10_000PrimesCorrect() =>
Assert.AreEqual(First10000PrimeNumbers, new SieveOfEratosthenes(104729).GetPrimes());

[Test]
public static void TestMaxNumber() => Assert.AreEqual(new SieveOfEratosthenes(69).MaximumNumber, 69);

[TestCase(13, true)]
[TestCase(10, false)]
public static void TestIsPrime(int input, bool expected)
{
Assert.AreEqual(new SieveOfEratosthenes(100).IsPrime(input), expected);
}
}
}
62 changes: 51 additions & 11 deletions Algorithms/Other/SieveOfEratosthenes.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Algorithms.Other
{
/// <summary>
/// TODO.
/// Implements the Sieve of Eratosthenes.
/// </summary>
public static class SieveOfEratosthenes
public class SieveOfEratosthenes
{
private readonly bool[] primes;

/// <summary>
/// TODO.
/// Initializes a new instance of the <see cref="SieveOfEratosthenes"/> class.
/// Uses the Sieve of Eratosthenes to precalculate the primes from 0 up to maximumNumberToCheck.
/// Requires enough memory to allocate maximumNumberToCheck bytes.
/// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes .
/// </summary>
/// <param name="count">TODO. 2.</param>
/// <returns>TODO. 3.</returns>
public static List<BigInteger> GetPrimeNumbers(int count)
/// <param name="maximumNumberToCheck">long which specifies the largest number you wish to know if it is prime.</param>
public SieveOfEratosthenes(long maximumNumberToCheck)
{
var output = new List<BigInteger>();
for (BigInteger n = 2; output.Count < count; n++)
primes = new bool[maximumNumberToCheck + 1];

// initialize primes array
Array.Fill(this.primes, true, 2, primes.Length - 2);

for(long i = 2; i * i <= maximumNumberToCheck; i++)
{
if (output.All(x => n % x != 0))
if (!primes[i])
{
continue;
}

for(long composite = i * i; composite <= maximumNumberToCheck; composite += i)
{
output.Add(n);
primes[composite] = false;
}
}
}

/// <summary>
/// Gets the maximumNumberToCheck the class was instantiated with.
/// </summary>
public long MaximumNumber => primes.Length - 1;

return output;
/// <summary>
/// Returns a boolean indicating whether the number is prime.
/// </summary>
/// <param name="numberToCheck">The number you desire to know if it is prime or not.</param>
/// <returns>A boolean indicating whether the number is prime or not.</returns>
public bool IsPrime(long numberToCheck) => primes[numberToCheck];

/// <summary>
/// Returns an IEnumerable of long primes in asending order.
/// </summary>
/// <returns>Primes in ascending order.</returns>
public IEnumerable<long> GetPrimes()
{
for(long i = 2; i < primes.Length; i++)
{
if (primes[i])
{
yield return i;
}
}
}
}
}