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

Published on 22 June 2024 08:30 PM

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

Code Overview:

This Kotlin program determines Lychrel numbers and their properties within a given range. Lychrel numbers are numbers that never produce a palindrome after a finite number of iterations by adding the reverse of their digits.

Constants:

  • ITERATIONS: Maximum number of iterations to check if a number is Lychrel.
  • LIMIT: Upper limit of the range to search for Lychrel numbers.

Variable Declarations:

  • bigLimit: BigInteger representation of LIMIT.
  • lychrelSieve: Array used as a sieve to track Lychrel properties (0: not Lychrel, 1: Seed Lychrel, 2: Related Lychrel).
  • seedLychrels: Mutable list to store Seed Lychrel numbers.
  • relatedLychrels: Mutable set to store Related Lychrel numbers.

Functions:

  • isPalindrome: Checks if a BigInteger is a palindrome.
  • lychrelTest: Performs the Lychrel test on a given integer i and updates the lychrelSieve, seedLychrels, and relatedLychrels accordingly.

Main Function:

  • Iterates through the range [1, LIMIT] and performs the Lychrel test on each number.
  • Counts the number of related Lychrel numbers.

Output:

  • Prints the number of Seed Lychrel numbers and their values.
  • Prints the number of Related Lychrel numbers.
  • Prints the number of palindromic Lychrel numbers and their values.

Algorithm:

The program uses a sieve approach to efficiently determine Lychrel numbers:

  1. Initialize the lychrelSieve array to all zeros.
  2. For each number i in the range [1, LIMIT]:
    • If i has already been processed (i.e., lychrelSieve[i] != 0), skip it.
    • Otherwise, perform the Lychrel test on i using the lychrelTest function.
    • Update the lychrelSieve array based on the test results.
    • If i is a Seed Lychrel number, add it to the seedLychrels list.
    • If i is a Related Lychrel number, add it to the relatedLychrels set.
  3. Count the number of related Lychrel numbers.
  4. Find and print the palindromic Lychrel numbers by checking if the Lychrel numbers are also palindromes.

By using this approach, the program efficiently determines and summarizes the properties of Lychrel numbers within the specified range.

Source code in the kotlin programming language

// version 1.0.6

import java.math.BigInteger

const val ITERATIONS = 500
const val LIMIT = 10000

val bigLimit = BigInteger.valueOf(LIMIT.toLong())

// In the sieve,  0 = not Lychrel, 1 = Seed Lychrel, 2 = Related Lychrel
val lychrelSieve    = IntArray(LIMIT + 1)  // all zero by default
val seedLychrels    = mutableListOf<Int>()
val relatedLychrels = mutableSetOf<BigInteger>()

fun isPalindrome(bi: BigInteger): Boolean {
    val s = bi.toString()
    return s == s.reversed()
}

fun lychrelTest(i: Int, seq: MutableList<BigInteger>){
    if (i < 1) return
    var bi = BigInteger.valueOf(i.toLong())
    (1 .. ITERATIONS).forEach {
        bi += BigInteger(bi.toString().reversed())
        seq.add(bi)
        if (isPalindrome(bi)) return
    }
    for (j in 0 until seq.size) {
        if (seq[j] <= bigLimit) lychrelSieve[seq[j].toInt()] = 2 
        else break
    } 
    val sizeBefore = relatedLychrels.size
    relatedLychrels.addAll(seq)  // if all of these can be added 'i' must be a seed Lychrel
    if (relatedLychrels.size - sizeBefore == seq.size) {
        seedLychrels.add(i)
        lychrelSieve[i] = 1 
    }
    else {
        relatedLychrels.add(BigInteger.valueOf(i.toLong()))
        lychrelSieve[i] = 2
    }        
}

fun main(args: Array<String>) {   
    val seq  = mutableListOf<BigInteger>()
    for (i in 1 .. LIMIT) 
        if (lychrelSieve[i] == 0) { 
           seq.clear() 
           lychrelTest(i, seq)
        } 
    var related = lychrelSieve.count { it == 2 }
    println("Lychrel numbers in the range [1, $LIMIT]")
    println("Maximum iterations = $ITERATIONS")
    println("\nThere are ${seedLychrels.size} seed Lychrel numbers, namely")
    println(seedLychrels)
    println("\nThere are also $related related Lychrel numbers in this range")    
    val palindromes = mutableListOf<Int>()
    for (i in 1 .. LIMIT)
        if (lychrelSieve[i] > 0 && isPalindrome(BigInteger.valueOf(i.toLong()))) palindromes.add(i)
    println("\nThere are ${palindromes.size} palindromic Lychrel numbers, namely")
    println(palindromes)
}


  

You may also check:How to resolve the algorithm Solve a Hopido puzzle step by step in the Racket programming language
You may also check:How to resolve the algorithm GUI component interaction step by step in the Go programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the Phix programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Bitmap/PPM conversion through a pipe step by step in the Perl programming language