Sha256: 599b39608216b814882217d97ca2141af525959e41c671db60f6495a64041e01

Contents?: true

Size: 594 Bytes

Versions: 231

Compression:

Stored size: 594 Bytes

Contents

using System.Collections.Generic;
using System.Linq;

public class Sieve
{
    public static int[] Primes(int limit)
    {
        return InitializePrimes(limit);
    }

    private static int[] InitializePrimes(int limit)
    {
        var candidates = new Queue<int>(Enumerable.Range(2, limit - 1));
        var primes = new List<int>();
        do
        {
            var prime = candidates.Dequeue();
            primes.Add(prime);
            candidates = new Queue<int>(candidates.Where(x => x % prime != 0));
        } while (candidates.Any());
        return primes.ToArray();
    }
}

Version data entries

231 entries across 231 versions & 1 rubygems

Version Path
trackler-2.2.1.13 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.12 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.11 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.10 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.9 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.8 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.7 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.6 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.5 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.4 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.3 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.2 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.1 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.1.0 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.0.6 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.0.5 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.0.4 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.0.3 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.0.2 tracks/csharp/exercises/sieve/Example.cs
trackler-2.2.0.1 tracks/csharp/exercises/sieve/Example.cs