How to resolve the algorithm Lychrel numbers step by step in the Java programming language
How to resolve the algorithm Lychrel numbers step by step in the Java 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 Java programming language
The provided Java code investigates the Lychrel numbers, integers that do not become palindromes after a finite number of iterations of the sum of the number and its reverse. The code uses dynamic programming with memoization to efficiently determine whether a given number is a Lychrel number.
Key Concepts:
-
Lychrel Numbers: Numbers that never become palindromes after a finite number of iterations of adding the number and its reverse.
-
Memoization: A technique used to improve the performance of a function by storing its results for later reuse.
Implementation Details:
1. Class Definition:
- The
Lychrel
class encapsulates the logic for determining if a given number is a Lychrel number.
2. Tuple Class:
- The
Tuple
class represents a pair of values: a flag and aBigInteger
. - The flag indicates whether the number is a Lychrel number, and the
BigInteger
represents the number itself.
3. rev() Method:
- The
rev()
method calculates the reverse of aBigInteger
by converting it to aString
, reversing the string, and converting it back to aBigInteger
.
4. lychrel() Method:
- The
lychrel()
method is the core of the algorithm and performs the following steps:- Checks if the number is already in the
cache
map, avoiding redundant calculations. - Calculates the reverse of the number.
- Initializes a tuple and a list to track seen numbers.
- Executes a loop that iteratively adds the number to its reverse and checks if either the sum becomes a palindrome or the number was previously seen.
- Updates the cache based on the outcome of the loop.
- Checks if the number is already in the
5. Main Method:
- Sets up lists to collect seeds (numbers that become Lychrel numbers), related (numbers that become palindromes after joining other Lychrel numbers), and palindromes (numbers that become palindromes after adding to themselves).
- Loops through numbers from 1 to 10,000 and determines their Lychrel status.
- Prints statistics on the number of Lychrel seeds, related numbers, and palindromes found.
By leveraging memoization, the code efficiently determines Lychrel numbers, providing insights into this fascinating mathematical concept.
Source code in the java programming language
import java.math.BigInteger;
import java.util.*;
public class Lychrel {
static Map<BigInteger, Tuple> cache = new HashMap<>();
static class Tuple {
final Boolean flag;
final BigInteger bi;
Tuple(boolean f, BigInteger b) {
flag = f;
bi = b;
}
}
static BigInteger rev(BigInteger bi) {
String s = new StringBuilder(bi.toString()).reverse().toString();
return new BigInteger(s);
}
static Tuple lychrel(BigInteger n) {
Tuple res;
if ((res = cache.get(n)) != null)
return res;
BigInteger r = rev(n);
res = new Tuple(true, n);
List<BigInteger> seen = new ArrayList<>();
for (int i = 0; i < 500; i++) {
n = n.add(r);
r = rev(n);
if (n.equals(r)) {
res = new Tuple(false, BigInteger.ZERO);
break;
}
if (cache.containsKey(n)) {
res = cache.get(n);
break;
}
seen.add(n);
}
for (BigInteger bi : seen)
cache.put(bi, res);
return res;
}
public static void main(String[] args) {
List<BigInteger> seeds = new ArrayList<>();
List<BigInteger> related = new ArrayList<>();
List<BigInteger> palin = new ArrayList<>();
for (int i = 1; i <= 10_000; i++) {
BigInteger n = BigInteger.valueOf(i);
Tuple t = lychrel(n);
if (!t.flag)
continue;
if (n.equals(t.bi))
seeds.add(t.bi);
else
related.add(t.bi);
if (n.equals(t.bi))
palin.add(t.bi);
}
System.out.printf("%d Lychrel seeds: %s%n", seeds.size(), seeds);
System.out.printf("%d Lychrel related%n", related.size());
System.out.printf("%d Lychrel palindromes: %s%n", palin.size(), palin);
}
}
You may also check:How to resolve the algorithm Balanced ternary step by step in the Scala programming language
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the zkl programming language
You may also check:How to resolve the algorithm Rosetta Code/Find unimplemented tasks step by step in the Tcl programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Factor programming language
You may also check:How to resolve the algorithm Empty string step by step in the FreeBASIC programming language