Skip to content

Instantly share code, notes, and snippets.

@opan
Last active February 19, 2017 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opan/7925ed05f192b563b270c9481ad2720a to your computer and use it in GitHub Desktop.
Save opan/7925ed05f192b563b270c9481ad2720a to your computer and use it in GitHub Desktop.
Generate palindromic prime numbers using Enum#lazy method
n = gets.to_i
# Get infinity list of number
2.upto(Float::INFINITY)
# Use Enum#lazy method
.lazy
# Find prime number
.select { |i| (2..Math.sqrt(i)).none? { |a| (i % a).zero? }}
# Limit infinity list of number by 200
.first(200)
# Find palindrome prime number
.select { |i| i.to_s == i.to_s.reverse }.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment