Truncatable primes: Difference between revisions
Content deleted Content added
add Ruby |
m →{{header|Ruby}}: use Prime module from stdlib |
||
Line 961: | Line 961: | ||
n = trunc_func.call(n) |
n = trunc_func.call(n) |
||
return true if n == 0 |
return true if n == 0 |
||
return false if not prime?(n) |
return false if not Prime.prime?(n) |
||
end |
end |
||
end |
end |
||
############# |
############# |
||
require 'prime' |
|||
def eratosthenes(n) |
|||
primes = Prime.each(1_000_000).to_a.reverse |
|||
(2**2..n).step(2) {|m| nums[m] = 0} |
|||
(3..Math.sqrt(n).to_i).step(2) do |i| |
|||
if nums[i].nonzero? |
|||
(i**2..n).step(i) {|m| nums[m] = 0} |
|||
end |
|||
end |
|||
nums.find_all {|m| m.nonzero?} |
|||
end |
|||
def prime?(n) |
|||
$primes.include? n |
|||
end |
|||
############# |
|||
$primes = eratosthenes(1_000_000).reverse |
|||
p |
p primes.detect {|p| left_truncatable? p} |
||
p |
p primes.detect {|p| right_truncatable? p}</lang> |
||
returns |
returns |