How to resolve the algorithm Lychrel numbers step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Lychrel numbers step by step in the jq 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 jq programming language

Source code in the jq programming language

# This workhorse function assumes its arguments are
# non-negative integers represented as decimal strings:
def add(num1;num2):
  if (num1|length) < (num2|length) then add(num2;num1)
  else  (num1 | explode | map(.-48) | reverse) as $a1
  | (num2 | explode | map(.-48) | reverse) as $a2
  | reduce range(0; num1|length) as $ix
      ($a2;  # result
       ( $a1[$ix] + .[$ix] ) as $r
         | if $r > 9 # carrying
           then
             .[$ix + 1] = ($r / 10 | floor) +  (if $ix + 1 >= length then 0 else .[$ix + 1] end )
             | .[$ix] = $r - ( $r / 10 | floor ) * 10
           else
             .[$ix] = $r
           end )
  | reverse | map(.+48) | implode
  end ;

# Input: an array
def is_palindrome:
  . as $in
  | (length -1) as $n
  | all( range(0; length/2); $in[.] == $in[$n - .]);

# Input: a string representing a decimal number.
# Output: a stream of such strings generated in accordance with the Lychrel rule, 
# subject to the limitation imposed by "limit", and ending in true if the previous item in the stream is a palindrome
def next_palindromes(limit):
   def toa: explode | map(.-48);
   def tos: map(.+48) | implode;
   def myadd(x;y): add(x|tos; y|tos) | toa;
   # input: [limit, n]
   def next:
     .[0] as $limit
     | .[1] as $n
     | if $limit <= 0 then empty
       else  myadd($n ; $n|reverse) as $sum
	| ($sum,
         if ($sum | is_palindrome) then true else [$limit - 1, $sum] | next end)
       end;
  [limit, toa] | next | if type == "boolean" then . else tos end;

# Consider integers in range(0;n) using maxiter as the maximum number
# of iterations in the search for palindromes.
# Emit a dictionary:
# { seed: _, palindromic_seed: _, related: _} + {($n): $n} for all related $n
# where .seed is an array of integers holding the potential Lychrel seeds, etc
def lychrel_dictionary(n; maxiter):
    reduce range(0; n) as $i ({};
        ($i | tostring) as $is
	| if .[$is] then .related += [$i]
	  else [$is | next_palindromes(maxiter)] as $seq
	  | . as $dict
          # | ([$i, $seq] | debug) as $debug
          | if $seq[-1] == true then .
	    else if ($is | explode | is_palindrome) then .palindromic_seed += [$i] else . end
            | if any($seq[]; $dict[.]) then .related += [$i]
              else .seed += [$i] 
              end
            | reduce $seq[] as $n (.; if .[$n] then . else .[$n] = $n end)
            end
          end ) ;

lychrel_dictionary(10001; 500)
| {seed, palindromic_seed, related: (.related | length) }

  

You may also check:How to resolve the algorithm String concatenation step by step in the Scilab programming language
You may also check:How to resolve the algorithm French Republican calendar step by step in the Nim programming language
You may also check:How to resolve the algorithm Parametric polymorphism step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Bitmap step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the Raku programming language