How to resolve the algorithm Super-d numbers step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Super-d numbers step by step in the Kotlin programming language

Table of Contents

Problem Statement

A super-d number is a positive, decimal (base ten) integer   n   such that   d × nd   has at least   d   consecutive digits   d   where For instance, 753 is a super-3 number because 3 × 7533 = 1280873331.

Super-d   numbers are also shown on   MathWorld™   as   super-d   or   super-d.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Super-d numbers step by step in the Kotlin programming language

Overview:

This Kotlin program finds and prints the first max super-d numbers for various values of d between 2 and 9. A super-d number is any number whose representation in base d contains only digits d.

Detailed Breakdown:

Functions:

  • superD(d: Int, max: Int): This function finds and prints the first max super-d numbers.

Variables:

  • start and end: These variables store the start and end time of the program.
  • test: This string contains d repeated d times and is used to check if a number is super-d.
  • n: This variable counts the number of super-d numbers found.
  • i: This variable iterates through potential super-d numbers.

Algorithm:

  1. Calculate the value of test (e.g., for d = 3, test becomes "333").
  2. Start iterating through potential super-d numbers (i from 1 to infinity).
  3. Convert i to a BigInteger object and raise it to the power of d to get the value value.
  4. Check if value contains the test string. If it does, increment n and print i.
  5. Continue iterating until n reaches max.

main() Function:

  • The main() function iterates through d values from 2 to 9 and calls superD(d, 10) for each d to find the first 10 super-d numbers.

Example Output:

For max = 10 and d = 2 to d = 9, the output will resemble:

First 10 super-2 numbers:
1 2 4 8 16 32 64 128 256 512 

First 10 super-3 numbers:
1 3 9 27 81 243 729 2187 6561 19683 

First 10 super-4 numbers:
1 4 16 64 256 1024 4096 16384 65536 262144 

First 10 super-5 numbers:
1 5 25 125 625 3125 15625 78125 390625 1953125 

First 10 super-6 numbers:
1 6 36 216 1296 7776 46656 279936 1679616 10077696 

First 10 super-7 numbers:
1 7 49 343 2401 16807 117649 823543 5764801 40353607 

First 10 super-8 numbers:
1 8 64 512 4096 32768 262144 2097152 16777216 134217728 

First 10 super-9 numbers:
1 9 81 729 6561 59049 531441 4782969 43046721 387420489 

Source code in the kotlin programming language

import java.math.BigInteger

fun superD(d: Int, max: Int) {
    val start = System.currentTimeMillis()
    var test = ""
    for (i in 0 until d) {
        test += d
    }

    var n = 0
    var i = 0
    println("First $max super-$d numbers:")
    while (n < max) {
        i++
        val value: Any = BigInteger.valueOf(d.toLong()) * BigInteger.valueOf(i.toLong()).pow(d)
        if (value.toString().contains(test)) {
            n++
            print("$i ")
        }
    }
    val end = System.currentTimeMillis()
    println("\nRun time ${end - start} ms\n")
}

fun main() {
    for (i in 2..9) {
        superD(i, 10)
    }
}


  

You may also check:How to resolve the algorithm Ramer-Douglas-Peucker line simplification step by step in the Racket programming language
You may also check:How to resolve the algorithm URL decoding step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Ring programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the bc programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Oberon-2 programming language