How to resolve the algorithm Lychrel numbers step by step in the Ruby programming language
How to resolve the algorithm Lychrel numbers step by step in the Ruby programming language
Table of Contents
Problem Statement
The above recurrence relation when applied to most starting numbers n = 1, 2, ... terminates in a palindrome quite quickly.
If n0 = 12 we get And if n0 = 55 we get Notice that the check for a palindrome happens after an addition.
Some starting numbers seem to go on forever; the recurrence relation for 196 has been calculated for millions of repetitions forming numbers with millions of digits, without forming a palindrome. These numbers that do not end in a palindrome are called Lychrel numbers. For the purposes of this task a Lychrel number is any starting number that does not form a palindrome within 500 (or more) iterations.
Any integer produced in the sequence of a Lychrel number is also a Lychrel number. In general, any sequence from one Lychrel number might converge to join the sequence from a prior Lychrel number candidate; for example the sequences for the numbers 196 and then 689 begin: So we see that the sequence starting with 689 converges to, and continues with the same numbers as that for 196. Because of this we can further split the Lychrel numbers into true Seed Lychrel number candidates, and Related numbers that produce no palindromes but have integers in their sequence seen as part of the sequence generated from a lower Lychrel number.
Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Lychrel numbers step by step in the Ruby programming language
The provided Ruby code explores the concept of Lychrel numbers and their related numbers. Here's a detailed explanation:
-
add_reverse(num, max_iter):
- Adds the reverse of a given integer
num
to itself and repeats this process until a palindrome is reached or a maximum number of iterations (max_iter
) is exceeded. - The function returns a set containing all the intermediate numbers generated during this process.
- Adds the reverse of a given integer
-
palindrome?(num):
- Checks if a given integer
num
is a palindrome, meaning it reads the same forwards and backward.
- Checks if a given integer
-
reverse_int(num):
- Reverses the digits of a given integer
num
and returns the reversed integer.
- Reverses the digits of a given integer
-
split_roots_from_relateds(roots_and_relateds):
- Takes a list of sets of integers and splits them into two groups:
- Roots: A unique subset of numbers that are not part of any other set.
- Related: The remaining numbers that are related to one or more roots.
- Takes a list of sets of integers and splits them into two groups:
-
find_lychrel(maxn, max_reversions):
- Computes Lychrel numbers and their related numbers for integers in the range 1 to
maxn
. - For each number, it limits the reverse-digits-and-adds process to a maximum of
max_reversions*2
iterations. - Returns two lists:
- A list of roots that do not become palindromes within the iteration limit.
- A list of numbers related to the roots.
- Computes Lychrel numbers and their related numbers for integers in the range 1 to
-
Lychrel Calculations:
- The code sets
maxn
to 10,000 andreversion_limit
to 500. - It calculates Lychrel numbers and their related numbers for each integer in the range 1 to 10,000, limiting the number of reversals to 2 * 500 = 1000.
- The code sets
-
Output:
- Prints out the number of Lychrel numbers found, along with a list of these numbers.
- Prints out the number of Lychrel related numbers, along with a list of these numbers.
- Identifies and prints out Lychrel palindromes (Lychrel numbers that are also palindromes) and their count.
Overall, this code is designed to explore and identify Lychrel numbers and related concepts, providing a detailed analysis of their properties and characteristics.
Source code in the ruby programming language
require 'set'
def add_reverse(num, max_iter=1000)
(1..max_iter).each_with_object(Set.new([num])) do |_,nums|
num += reverse_int(num)
nums << num
return nums if palindrome?(num)
end
end
def palindrome?(num)
num == reverse_int(num)
end
def reverse_int(num)
num.to_s.reverse.to_i
end
def split_roots_from_relateds(roots_and_relateds)
roots = roots_and_relateds.dup
i = 1
while i < roots.length
this = roots[i]
if roots[0...i].any?{|prev| this.intersect?(prev)}
roots.delete_at(i)
else
i += 1
end
end
root = roots.map{|each_set| each_set.min}
related = roots_and_relateds.map{|each_set| each_set.min}
related = related.reject{|n| root.include?(n)}
return root, related
end
def find_lychrel(maxn, max_reversions)
series = (1..maxn).map{|n| add_reverse(n, max_reversions*2)}
roots_and_relateds = series.select{|s| s.length > max_reversions}
split_roots_from_relateds(roots_and_relateds)
end
maxn, reversion_limit = 10000, 500
puts "Calculations using n = 1..#{maxn} and limiting each search to 2*#{reversion_limit} reverse-digits-and-adds"
lychrel, l_related = find_lychrel(maxn, reversion_limit)
puts " Number of Lychrel numbers: #{lychrel.length}"
puts " Lychrel numbers: #{lychrel}"
puts " Number of Lychrel related: #{l_related.length}"
pals = (lychrel + l_related).select{|x| palindrome?(x)}.sort
puts " Number of Lychrel palindromes: #{pals.length}"
puts " Lychrel palindromes: #{pals}"
You may also check:How to resolve the algorithm Conditional structures step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Lua programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the GAP programming language
You may also check:How to resolve the algorithm Forward difference step by step in the RPL programming language