You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Good morning! Here's your coding interview problem for today.
This problem was asked by Square.
The Sieve of Eratosthenes is an algorithm used to generate all prime numbers smaller than N. The method is to take increasingly larger prime numbers, and mark their multiples as composite.
For example, to find all primes less than 100, we would first mark [4, 6, 8, ...] (multiples of two), then [6, 9, 12, ...] (multiples of three), and so on. Once we have done this for all primes less than N, the unmarked numbers that remain will be prime.
Implement this algorithm.
Bonus: Create a generator that produces primes indefinitely (that is, without taking N as an input).
Activity
vaskoz commentedon Apr 23, 2019
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Pseudocode
vaskoz commentedon Apr 23, 2019
Bonus question. Implemented as a read channel of ints.
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Incremental_sieve