How to resolve the algorithm Long multiplication step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Long multiplication step by step in the Kotlin programming language

Table of Contents

Problem Statement

Explicitly implement   long multiplication.
This is one possible approach to arbitrary-precision integer algebra.

For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: The output of   264 * 264   is   2128,   and is:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Long multiplication step by step in the Kotlin programming language

The provided code implements multiplication of large numbers in Kotlin. It starts by converting the input strings this and n into lists of digits, then it performs the multiplication by iterating over the digits of the second number n. For each digit in n, it iterates over the digits of the first number this, performing the multiplication. The result is stored in an array result, which is then converted back to a string and returned.

Here is a step-by-step explanation of the code:

  1. The toDigits() extension function converts a string to a list of digits. It does this by iterating over the characters in the string and checking if each character is a digit. If a non-digit character is found, an IllegalArgumentException is thrown. If all characters are digits, they are converted to their corresponding integer values and added to the list in reverse order (least significant digit first).

  2. The operator fun String.times(n: String): String function is the entry point for the multiplication operation. It takes two strings as input and returns the result of multiplying them as a string.

  3. Inside the times function, the toDigits() function is called on both input strings to convert them to lists of digits. The resulting lists are stored in left and right.

  4. An array result is created to store the result of the multiplication. The size of the array is the sum of the sizes of left and right.

  5. The right list is iterated over, and for each digit in right, the following steps are performed:

    • A temporary variable tmp is initialized to 0.
    • The left list is iterated over, and for each digit in left, the following steps are performed:
      • The value of tmp is incremented by the value of result[leftPos + rightPos] and the product of the right digit and the left digit.
      • The value of result[leftPos + rightPos] is updated to the remainder of tmp divided by 10.
      • The value of tmp is updated to the quotient of tmp divided by 10.
    • The value of destPos is initialized to the sum of rightPos and the size of left.
    • While tmp is not 0, the following steps are performed:
      • The value of tmp is incremented by the value of result[destPos] converted to a long and masked with 0xFFFFFFFFL and then converted back to an int.
      • The value of result[destPos] is updated to the remainder of tmp divided by 10.
      • The value of tmp is updated to the quotient of tmp divided by 10.
      • The value of destPos is incremented by 1.
  6. The result array is converted back to a string and returned. The conversion is done by folding right over the array, appending the digits to a StringBuilder in reverse order.

  7. In the main function, the multiplication of two large numbers is performed and the result is printed to the console.

The time complexity of the multiplication algorithm is O(n^2), where n is the number of digits in the larger of the two input numbers. This is because the algorithm iterates over each digit in the first number left for each digit in the second number right.

Source code in the kotlin programming language

fun String.toDigits() = mapIndexed { i, c ->
    if (!c.isDigit())
        throw IllegalArgumentException("Invalid digit $c found at position $i")
    c - '0'
}.reversed()

operator fun String.times(n: String): String {
    val left = toDigits()
    val right = n.toDigits()
    val result = IntArray(left.size + right.size)

    right.mapIndexed { rightPos, rightDigit ->
        var tmp = 0
        left.indices.forEach { leftPos ->
            tmp += result[leftPos + rightPos] + rightDigit * left[leftPos]
            result[leftPos + rightPos] = tmp % 10
            tmp /= 10
        }
        var destPos = rightPos + left.size
        while (tmp != 0) {
            tmp += (result[destPos].toLong() and 0xFFFFFFFFL).toInt()
            result[destPos] = tmp % 10
            tmp /= 10
            destPos++
        }
    }

    return result.foldRight(StringBuilder(result.size), { digit, sb ->
        if (digit != 0 || sb.length > 0) sb.append('0' + digit)
        sb
    }).toString()
}

fun main(args: Array<out String>) {
    println("18446744073709551616" * "18446744073709551616")
}


  

You may also check:How to resolve the algorithm General FizzBuzz step by step in the Batch File programming language
You may also check:How to resolve the algorithm Machine code step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Uniface programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Julia programming language