How to resolve the algorithm Long multiplication step by step in the Kotlin programming language
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:
-
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, anIllegalArgumentException
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). -
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. -
Inside the
times
function, thetoDigits()
function is called on both input strings to convert them to lists of digits. The resulting lists are stored inleft
andright
. -
An array
result
is created to store the result of the multiplication. The size of the array is the sum of the sizes ofleft
andright
. -
The
right
list is iterated over, and for each digit inright
, the following steps are performed:- A temporary variable
tmp
is initialized to 0. - The
left
list is iterated over, and for each digit inleft
, the following steps are performed:- The value of
tmp
is incremented by the value ofresult[leftPos + rightPos]
and the product of the right digit and the left digit. - The value of
result[leftPos + rightPos]
is updated to the remainder oftmp
divided by 10. - The value of
tmp
is updated to the quotient oftmp
divided by 10.
- The value of
- The value of
destPos
is initialized to the sum ofrightPos
and the size ofleft
. - While
tmp
is not 0, the following steps are performed:- The value of
tmp
is incremented by the value ofresult[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 oftmp
divided by 10. - The value of
tmp
is updated to the quotient oftmp
divided by 10. - The value of
destPos
is incremented by 1.
- The value of
- A temporary variable
-
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 aStringBuilder
in reverse order. -
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