How to resolve the algorithm Truncatable primes step by step in the Ruby programming language
How to resolve the algorithm Truncatable primes step by step in the Ruby programming language
Table of Contents
Problem Statement
A truncatable prime is a prime number that when you successively remove digits from one end of the prime, you are left with a new prime number.
The number 997 is called a left-truncatable prime as the numbers 997, 97, and 7 are all prime. The number 7393 is a right-truncatable prime as the numbers 7393, 739, 73, and 7 formed by removing digits from its right are also prime. No zeroes are allowed in truncatable primes.
The task is to find the largest left-truncatable and right-truncatable primes less than one million (base 10 is implied).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Truncatable primes step by step in the Ruby programming language
The provided Ruby code defines methods to determine whether a given number is left-truncatable or right-truncatable and uses these methods to find the first left- and right-truncatable prime numbers within a range.
left_truncatable?
andright_truncatable?
Methods:
left_truncatable?
checks if a numbern
is left-truncatable, meaning all its truncations (removing digits from the left) are prime.right_truncatable?
does the same, but for truncations from the right.
-
truncatable?
Method:truncatable?
implements the core logic for checking whether a number is truncatable.- It takes a number
n
and a blocktrunc_func
that defines how to truncaten
. - It checks if
n
contains any zeros (which would make it non-prime), then iteratively truncatesn
usingtrunc_func
. - If
n
becomes zero, the number is truncatable. Ifn
is not prime at any point, it is not truncatable.
-
Usage:
- The code requires the
prime
library for prime number handling. - It generates a list of prime numbers up to 1 million in reverse order.
- It uses the
detect
method to find the first prime number that is left-truncatable and right-truncatable within the list.
- The code requires the
-
Expected Output:
- The first left-truncatable prime within the range is
3797
, which can be truncated from the left to yield797
,97
, and7
. - The first right-truncatable prime within the range is
53
, which can be truncated from the right to yield5
,3
, and0
.
- The first left-truncatable prime within the range is
Source code in the ruby programming language
def left_truncatable?(n)
truncatable?(n) {|i| i.to_s[1..-1].to_i}
end
def right_truncatable?(n)
truncatable?(n) {|i| i/10}
end
def truncatable?(n, &trunc_func)
return false if n.to_s.include? "0"
loop do
n = trunc_func.call(n)
return true if n.zero?
return false unless Prime.prime?(n)
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}
You may also check:How to resolve the algorithm Paraffins step by step in the Go programming language
You may also check:How to resolve the algorithm Function definition step by step in the Toka programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Pike programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the Go programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the Lasso programming language