Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4609

Ruby - finding the first N palindromic prime using lazy evaluation

$
0
0

i think my code is correct - yet i do not return an array in time for N = 200. Error is "Terminated due to timeout"what can i do to improve the performance of this code?

def is_palindrome? (n)   n.to_s==n.to_s.reverseenddef is_prime? (n)  return false if n< 2  (2..Math.sqrt(n)).none? {|f| n % f == 0}endprime_palindrome =-> (n) do   1.upto(Float::INFINITY).lazy.select { |n| is_prime?(n) && is_palindrome(n) }.first(n)endn = gets.to_i p prime_palindrome.call(n)

Viewing all articles
Browse latest Browse all 4609

Trending Articles