We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Ruby
  3. Methods
  4. Lazy Evaluation

Lazy Evaluation

Problem
Submissions
Leaderboard
Discussions

Lazy evaluation is an evaluation strategy that delays the assessment of an expression until its value is needed.

Ruby introduced a lazy enumeration feature. Lazy evaluation increases performance by avoiding needless calculations, and it has the ability to create potentially infinite data structures.

Example:

power_array = -> (power, array_size) do 
    1.upto(Float::INFINITY).lazy.map { |x| x**power }.first(array_size) 
end

puts power_array.(2 , 4)    #[1, 4, 9, 16]
puts power_array.(2 , 10)   #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
puts power_array.(3, 5)     #[1, 8, 27, 64, 125]

In this example, lazy avoids needless calculations to compute power_array.
If we remove lazy from the above code, then our code would try to compute all ranging from to Float::INFINITY.
To avoid timeouts and memory allocation exceptions, we use lazy. Now, our code will only compute up to first(array_size).


Task

Your task is to print an array of the first palindromic prime numbers.
For example, the first palindromic prime numbers are .

Input Format

A single line of input containing the integer .

Constraints

You are not given how big is.

Output Format

Print an array of the first palindromic primes.

Sample Input

5

Sample Output

[2, 3, 5, 7, 11]

Author

[deleted]

Difficulty

Medium

Max Score

30

Submitted By

7908

Need Help?


View discussions
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy