RosettaCodeData/Task/Truncatable-primes/Ruby/truncatable-primes.rb

24 lines
466 B
Ruby
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
def left_truncatable?(n)
2014-01-17 05:32:22 +00:00
truncatable?(n) {|i| i.to_s[1..-1].to_i}
2013-04-11 01:07:29 -07:00
end
def right_truncatable?(n)
2014-01-17 05:32:22 +00:00
truncatable?(n) {|i| i/10}
2013-04-11 01:07:29 -07:00
end
2014-01-17 05:32:22 +00:00
def truncatable?(n, &trunc_func)
return false if n.to_s.include? "0"
2013-04-11 01:07:29 -07:00
loop do
n = trunc_func.call(n)
2014-01-17 05:32:22 +00:00
return true if n.zero?
return false unless Prime.prime?(n)
2013-04-11 01:07:29 -07:00
end
end
require 'prime'
primes = Prime.each(1_000_000).to_a.reverse
p primes.detect {|p| left_truncatable? p}
p primes.detect {|p| right_truncatable? p}