How to resolve the algorithm Knuth's power tree step by step in the Kotlin programming language
How to resolve the algorithm Knuth's power tree step by step in the Kotlin programming language
Table of Contents
Problem Statement
(Knuth's power tree is used for computing xn efficiently.)
Compute and show the list of Knuth's power tree integers necessary for the computation of:
Then, using those integers, calculate and show the exact values of (at least) the integer powers below:
A zero power is often handled separately as a special case. Optionally, support negative integer powers.
An example of a small power tree for some low integers: Where, for the power 43, following the tree "downwards" from 1: Note that for every even integer (in the power tree), one just squares the previous value. For an odd integer, multiply the previous value with an appropriate odd power of X (which was previously calculated). For the last multiplication in the above example, it would be (43-40), or 3. According to Dr. Knuth (see below), computer tests have shown that this power tree gives optimum results for all of the n listed above in the graph. For n ≤ 100,000, the power tree method:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Knuth's power tree step by step in the Kotlin programming language
The provided code showcases a recursive algorithm to compute the exponential tree forms of real numbers with integer exponents. This approach is more efficient and accurate than the traditional method of repeated multiplication, particularly for large exponents. The code uses the path
function to generate the path of numbers leading to the target exponent, and the treePow
function to calculate the exponential tree form. The showPow
function prints out the path and the computed result for various inputs.
The path
function employs a breadth-first search algorithm to find the path of numbers leading to the desired exponent. It starts with the number 1 and iteratively adds new numbers by summing existing numbers in the path. The function uses a mutable map p
to efficiently track the parent of each number and a mutable list lvl
to represent the current level of numbers in the path.
The treePow
function uses dynamic programming with a mutable map r
to compute the exponential tree form based on the path found by the path
function. It stores intermediate results in the map to avoid recomputation and ensures accuracy by using the BigDecimal
class for decimal operations.
The showPow
function provides a user-friendly interface to display the path and the computed result. It formats the output based on whether the exponent is integral or not.
The main
function demonstrates the usage of the showPow
function for various inputs. It calculates and displays the exponential tree forms of 2.0, 1.1, and 3.0 for different exponents, showcasing the efficiency and accuracy of this approach.
Source code in the kotlin programming language
// version 1.1.3
import java.math.BigDecimal
var p = mutableMapOf(1 to 0)
var lvl = mutableListOf(listOf(1))
fun path(n: Int): List<Int> {
if (n == 0) return emptyList<Int>()
while (n !in p) {
val q = mutableListOf<Int>()
for (x in lvl[0]) {
for (y in path(x)) {
if ((x + y) in p) break
p[x + y] = x
q.add(x + y)
}
}
lvl[0] = q
}
return path(p[n]!!) + n
}
fun treePow(x: Double, n: Int): BigDecimal {
val r = mutableMapOf(0 to BigDecimal.ONE, 1 to BigDecimal(x.toString()))
var p = 0
for (i in path(n)) {
r[i] = r[i - p]!! * r[p]!!
p = i
}
return r[n]!!
}
fun showPow(x: Double, n: Int, isIntegral: Boolean = true) {
println("$n: ${path(n)}")
val f = if (isIntegral) "%.0f" else "%f"
println("${f.format(x)} ^ $n = ${f.format(treePow(x, n))}\n")
}
fun main(args: Array<String>) {
for (n in 0..17) showPow(2.0, n)
showPow(1.1, 81, false)
showPow(3.0, 191)
}
You may also check:How to resolve the algorithm Brownian tree step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Klarner-Rado sequence step by step in the C programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Fantom programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Lua programming language
You may also check:How to resolve the algorithm Continued fraction step by step in the Java programming language