Reviewed by Aditya Kumar Β· Last reviewed 2026-03-24
**Sieve of Eratosthenes:** O(n log log n). Mark composites. Simple check: (2 to sqrt(n)).forall(n % _ != 0). For large ranges, sieve wins. **Scala Style:** (2 to N).filter(n => (2 to math.sqrt(n).toInt).forall(n % _ != 0))....
Sieve of Eratosthenes: O(n log log n). Mark composites. Simple check: (2 to sqrt(n)).forall(n % _ != 0). For large ranges, sieve wins.
Scala Style: (2 to N).filter(n => (2 to math.sqrt(n).toInt).forall(n % _ != 0)). Or lazy Stream for infinite primes.
val primes = (2 to 100).filter(n => (2 to math.sqrt(n).toInt).forall(n % _ != 0))
primes.foreach(println)
Red Flag: Checking all divisors (O(n)). Pro-Move: 'Sieve for N=10^6; we use primes for partitioning strategy in Spark.'
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.