Sha256: d2a701fe0a249be27cc6773c91e8ee7d5f1c23af1f2d07ab8a1635fdfa2752f9

Contents?: true

Size: 923 Bytes

Versions: 62

Compression:

Stored size: 923 Bytes

Contents

class Sieve {
    static primes(limit: number): number[] {
        if (limit === 2) {
            return [limit]
        }

        const sieve: boolean[] = []
        for (let i = 0; i < limit; i++) {
            sieve[i] = true
        }

        const primes: number[] = []
        const maxCandidate = Math.floor(Math.sqrt(limit))
        for (let candidate = 2; candidate <= maxCandidate + 1; candidate++) {
            if (!sieve[candidate - 1]) {
                continue
            }

            primes.push(candidate)
            let multiple = candidate * candidate
            while (multiple <= limit) {
                sieve[multiple - 1] = false
                multiple += candidate
            }
        }

        for (let i = maxCandidate + 1; i <= limit; i++) {
            if (sieve[i - 1]) {
                primes.push(i)
            }
        }

        return primes
    }
}

export default Sieve

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/sieve/sieve.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/sieve/sieve.example.ts