How to resolve the algorithm Lychrel numbers step by step in the zkl programming language
How to resolve the algorithm Lychrel numbers step by step in the zkl 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 zkl programming language
Source code in the zkl programming language
var BN=Import("zklBigNum");
// 192-->"887\01675\07436\013783\0..." ~60k bytes
fcn lychrel(n,returnListof=False){ n=BN(n); //-->Bool|Data(==StringBuf)
sink:=(if(returnListof) Data(0,String) else Void);
nls:=(500).pump(sink,'wrap(){
ns:=n.add(BN(n.toString().reverse())).toString();
if(ns==ns.reverse()) return(Void.Stop,False); // not a Lychrel number
ns
});
if(nls) if(returnListof) return(sink.mode(Int).howza(2)) else return(True);
False;
}
fcn isPalindrome(n){ n==n.toString().reverse().toInt() }
fcn findSeeds(lychrels){
seeds:=List(lychrels[0]);
foreach n,lnns in ([1..].zip(lychrels[1,*])){ ln,seq:=lnns;
foreach _,s2 in (seeds){
foreach s3 in (seq){
if(Void!=(z:=s2.findString(s3)) and 0==s2[z-1]) break(2);
}
}
fallthrough{ seeds.append(lnns) }
}
seeds.apply("get",0)
}
lychrels:=[1..0d10_000].filter(lychrel).apply(fcn(n){ T(n,lychrel(n,True)) });
println("[1..10,000] contains ",lychrels.len()," Lychrel numbers.");
lychrels.pump(List,fcn([(n,_)]){ isPalindrome(n) and n or Void.Skip })
.println("<-- Palindromic Lychrel numbers");
(n:=findSeeds(lychrels))
.println("<-- Lychrel seeds (%d) ==> %d related"
.fmt(n.len(),lychrels.len() - n.len()));
You may also check:How to resolve the algorithm String length step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the XQuery programming language
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the REXX programming language
You may also check:How to resolve the algorithm Exceptions step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Voronoi diagram step by step in the Haskell programming language