Advanced Ruby • Exercise 4/5

Lazy Enumeration

00:00
0
30 points

Instructions

Create a function first_n_primes that returns the first N prime numbers.

Use lazy enumerators to avoid computing unnecessary values:
ruby
(1..).lazy # Infinite sequence
.select { |n| condition } # Filter lazily
.take(5) # Take only 5
.to_a # Convert to array

The (1..) creates an infinite range. Without lazy, this would
run forever! With lazy, only the needed values are computed.

A prime number is greater than 1 and divisible only by 1 and itself.

Your Code

Results

Click "Run Tests" to see results