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

Published on 12 May 2024 09:40 PM
#D

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

Source code in the d programming language

import std.stdio, std.conv, std.range, std.typecons, std.bigInt;

auto rev(in BigInt n) {
    return n.text.retro.text.BigInt;
}

alias Res = Tuple!(bool, BigInt);

Res lychrel(BigInt n) {
    static Res[BigInt] cache;
    if (n in cache)
        return cache[n];
 
    auto r = n.rev;
    auto res = Res(true, n);
    immutable(BigInt)[] seen;
    foreach (immutable i; 0 .. 1_000) {
        n += r;
        r = n.rev;
        if (n == r) {
            res = Res(false, BigInt(0));
            break;
        }
        if (n in cache) {
            res = cache[n];
            break;
        }
        seen ~= n;
    }
    
    foreach (x; seen)
        cache[x] = res;
    return res;
}

void main() {
    BigInt[] seeds, related, palin;

    foreach (immutable i; BigInt(1) .. BigInt(10_000)) { // 1_000_000
        const tf_s = i.lychrel;
        if (!tf_s[0])
            continue;
        (i == tf_s[1] ? seeds : related) ~= i;
        if (i == i.rev)
            palin ~= i;
    }
    
    writeln(seeds.length, " Lychrel seeds: ", seeds);
    writeln(related.length, " Lychrel related");
    writeln(palin.length, " Lychrel palindromes: ", palin);
}


  

You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Battlestar programming language
You may also check:How to resolve the algorithm Binary search step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Perl programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the Phix programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the OCaml programming language