From 10651fe1fe7b036d76574641f9a6034b82df0b7b Mon Sep 17 00:00:00 2001 From: LashaBuxo Date: Sat, 13 Jun 2020 15:48:56 +0400 Subject: [PATCH] Added Linear Sieve Algorithm --- .../Numeric/LinearSieveOfEratosthenes.cs | 51 +++++++++++++++++++ .../LinearSieveOfEratosthenesTests.cs | 46 +++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 Algorithms/Numeric/LinearSieveOfEratosthenes.cs create mode 100644 UnitTest/AlgorithmsTests/LinearSieveOfEratosthenesTests.cs diff --git a/Algorithms/Numeric/LinearSieveOfEratosthenes.cs b/Algorithms/Numeric/LinearSieveOfEratosthenes.cs new file mode 100644 index 00000000..5d6b104e --- /dev/null +++ b/Algorithms/Numeric/LinearSieveOfEratosthenes.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +/*** +* Generates all prime numbers up to a given number +* Wikipedia: http://e-maxx.ru/algo/prime_sieve_linear +*/ + + +namespace Algorithms.Numeric +{ + public static class LinearSieveOfEratosthenes + { + /// + /// Calculate primes up to a given number + /// Time Complexity: O(N) + /// Memory: O(N) + /// + public static List GeneratePrimesUpTo(int N) + { + //if N is negative, we should return empty list of primes + if (N < 0) return new List(); + + + int[] lp=new int[N+1]; + + //List of primes + var pr = new List(); + + int op = 0; + for (int i = 2; i <= N; ++i) + { + if (lp[i] == 0) + { + lp[i] = i; + pr.Add(i); + } + for (int j = 0; j < pr.Count && pr[j] <= lp[i] && i * pr[j] <= N; ++j) + { + lp[i * pr[j]] = pr[j]; + op++; + } + } + + return pr; + } + + } +} diff --git a/UnitTest/AlgorithmsTests/LinearSieveOfEratosthenesTests.cs b/UnitTest/AlgorithmsTests/LinearSieveOfEratosthenesTests.cs new file mode 100644 index 00000000..9ee8797b --- /dev/null +++ b/UnitTest/AlgorithmsTests/LinearSieveOfEratosthenesTests.cs @@ -0,0 +1,46 @@ +using Algorithms.Numeric; +using System.Linq; +using Xunit; +using Xunit.Abstractions; + +using System.Diagnostics; +namespace UnitTest.AlgorithmsTests +{ + public class LinearSieveOfEratosthenesTests + { + private readonly ITestOutputHelper _testOutputHelper; + public LinearSieveOfEratosthenesTests(ITestOutputHelper testOutputHelper) + { + _testOutputHelper = testOutputHelper; + } + private const int MaxNumber = 100; + + [Fact] + public void DoTests() + { + var results = LinearSieveOfEratosthenes.GeneratePrimesUpTo(MaxNumber); + Assert.NotNull(results); + Assert.True(results.Any()); + Assert.Equal(results.Count(), 25); + Assert.DoesNotContain(1, results); + Assert.Contains(2, results); + Assert.Contains(7, results); + Assert.Contains(23, results); + Assert.Contains(41, results); + Assert.Contains(97, results); + + } + + [Fact] + public void TestsForEmpty() + { + var results = LinearSieveOfEratosthenes.GeneratePrimesUpTo(0); + Assert.NotNull(results); + Assert.False(results.Any()); + + var results2 = LinearSieveOfEratosthenes.GeneratePrimesUpTo(-100); + Assert.NotNull(results2); + Assert.False(results2.Any()); + } + } +} \ No newline at end of file