Bird
0
0

How can you combine lazy enumerators with infinite sequences to find the first 4 prime numbers greater than 50?

hard📝 Application Q9 of 15
Ruby - Functional Patterns in Ruby
How can you combine lazy enumerators with infinite sequences to find the first 4 prime numbers greater than 50?
A(1..50).lazy.select { |n| prime?(n) }.first(4)
B(51..Float::INFINITY).lazy.select { |n| prime?(n) }.first(4)
C(50..100).lazy.select { |n| prime?(n) }.first(4)
D(51..Float::INFINITY).select { |n| prime?(n) }.first(4)
Step-by-Step Solution
Solution:
  1. Step 1: Use infinite range starting after 50

    Range from 51 to infinity lazily filtered for primes.
  2. Step 2: Use lazy select and first(4)

    Lazy select filters primes without computing all; first(4) gets first 4 primes > 50.
  3. Step 3: Check other options

    (1..50).lazy.select { |n| prime?(n) }.first(4) searches below 50; C is limited range; D is not lazy and infinite select causes hang.
  4. Final Answer:

    (51..Float::INFINITY).lazy.select { |n| prime?(n) }.first(4) -> Option B
  5. Quick Check:

    Lazy infinite prime filter = (51..Float::INFINITY).lazy.select { |n| prime?(n) }.first(4) [OK]
Quick Trick: Combine lazy with infinite range for filtered sequences [OK]
Common Mistakes:
  • Using finite ranges missing primes > 50
  • Not using lazy on infinite range
  • Using non-lazy select on infinite range

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes