Jump to content

Knuth's power tree: Difference between revisions

m
syntax highlighting fixup automation
(Ada version)
m (syntax highlighting fixup automation)
Line 82:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V p = [1 = 0]
V lvl = [[1]]
 
Line 116:
show_pow_i(2, x)
show_pow_i(3, 191)
show_pow_f(1.1, 81)</langsyntaxhighlight>
 
{{out}}
Line 183:
=={{header|Ada}}==
In this program the power tree is in a generic package which is instantiated for each base value.
<langsyntaxhighlight Adalang="ada">with Ada.Containers.Ordered_Maps;
with Ada.Numerics.Big_Numbers.Big_Integers;
with Ada.Text_IO; use Ada.Text_IO;
Line 298:
Part_2;
Part_3;
end Power_Tree;</langsyntaxhighlight>
{{out}}
<pre>
Line 342:
===Power tree===
We build the tree using '''tree.lib''', adding leaves until the target n is found.
<langsyntaxhighlight lang="scheme">
(lib 'tree)
 
Line 383:
(add-level T init-chain: (make-vector 0) target nums)
))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 423:
</pre>
===Exponentiation===
<langsyntaxhighlight lang="scheme">
;; j such as chain[i] = chain[i-1] + chain[j]
(define (adder chain i)
Line 435:
(vector-set! pow i ( * [pow [1- i]] [pow (adder chain i)])))
[pow (1- lg)])
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 452:
=={{header|F_Sharp|F#}}==
===Integer Exponentiation===
<langsyntaxhighlight lang="fsharp">
// Integer exponentiation using Knuth power tree. Nigel Galloway: October 29th., 2020
let kT α=let n=Array.zeroCreate<int*int list>((pown 2 (α+1))+1)
Line 468:
[0..17]|>List.iter(fun n->printfn "2**%d=%A\n" n (xp 2 n))
printfn "3**191=%A" (xp 3 191)
</syntaxhighlight>
</lang>
{out}
<pre>
Line 494:
 
===Float Exponentiation===
<langsyntaxhighlight lang="fsharp">
// Float exponentiation using Knuth power tree. Nigel Galloway: October 29th., 2020
let kTf α=let n=Array.zeroCreate<int*int list>((pown 2 (α+1))+1)
Line 510:
let xpf=kTf 11
printfn "1.1**81=%f" (xpf 1.1 81)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 517:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 584:
showPow(1.1, 81, false)
showPow(3, 191, true)
}</langsyntaxhighlight>
 
{{out}}
Line 651:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class PowerTree {
private static Map<Integer, Integer> p = new HashMap<>()
private static List<List<Integer>> lvl = new ArrayList<>()
Line 712:
showPos 3.0, 191, true
}
}</langsyntaxhighlight>
{{out}}
<pre>0: []
Line 777:
{{works with|GHC|8.8.1}}
{{libheader|containers|0.6.2.1}}
<langsyntaxhighlight lang="haskell">{-# LANGUAGE ScopedTypeVariables #-}
 
module Rosetta.PowerTree
Line 834:
let b' = b * m Map.! (l - k)
m' = Map.insert l b' m
in go b' l m' ls</langsyntaxhighlight>
{{out}}
{{libheader|numbers|3000.2.0.2}}
Line 869:
We can represent the tree as a list of indices. Each entry in the list gives the value of <code>n</code> for the index <code>a+n</code>. (We can find <code>a</code> using subtraction.)
 
<langsyntaxhighlight Jlang="j">knuth_power_tree=:3 :0
L=: P=: %(1+y){._ 1
findpath=: ]
Line 896:
end.
{:exp
)</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> knuth_power_tree 191 NB. generate sufficiently large tree
0 1 1 2 2 3 3 5 4 6 5 10 6 10 7 10 8 16 9 14 10 14 11 13 12 15 13 18 14 28 15 28 16 17 17 21 18 36 19 26 20 40 21 40 22 30 23 42 24 48 25 48 26 52 27 44 28 38 29 31 30 56 31 42 32 64 33 66 34 46 35 57 36 37 37 50 38 76 39 76 40 41 41 43 42 80 43 84 44 47 45 70 46 62 47 57 48 49 49 51 50 100 51 100 52 70 53 104 54 104 55 108 56 112 57 112 58 61 59 112 60 120 61 120 62 75 63 126 64 65 65 129 66 67 67 90 68 136 69 138 70 140 71 140 72 144 73 144 74 132 75 138 76 144 77 79 78 152 79 152 80 160 81 160 82 85 83 162 84 168 85 114 86 168 87 105 88 118 89 176 90 176 91 122 92 184 93 176 94 126 95 190
 
Line 1,002:
(x:1.1) usepath 81
2253240236044012487937308538033349567966729852481170503814810577345406584190098644811r1000000000000000000000000000000000000000000000000000000000000000000000000000000000
</syntaxhighlight>
</lang>
 
Note that an 'r' in a number indicates a rational number with the numerator to the left of the r and the denominator to the right of the r. We could instead use decimal notation by indicating how many characters of result we want to see, as well as how many characters to the right of the decimal point we want to see.
Line 1,008:
Thus, for example:
 
<langsyntaxhighlight Jlang="j"> 90j83 ": (x:1.1) usepath 81
2253.24023604401248793730853803334956796672985248117050381481057734540658419009864481100</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Line 1,079:
showPow(3.0, 191, true);
}
}</langsyntaxhighlight>
{{out}}
<pre>0: []
Line 1,149:
 
(*) Use gojq for infinite-precision integer arithmetic.
<langsyntaxhighlight lang="jq"># Input: {p, lvl, path}
def kpath($n):
if $n == 0 then .path=[]
Line 1,189:
(range(0;18) as $n | showPow(2; $n)),
showPow(1.1; 81),
showPow(3; 191)</langsyntaxhighlight>
{{out}}
Using gojq, the output is the same as for [[#Wren|Wren]].
Line 1,197:
 
'''Module''':
<langsyntaxhighlight lang="julia">module KnuthPowerTree
 
const p = Dict(1 => 0)
Line 1,228:
end
 
end # module KnuthPowerTree</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang="julia">using .KnuthPowerTree: path, pow
 
for n in 0:17
Line 1,239:
for (x, n) in ((big(3), 191), (1.1, 81))
println("$x ^ $n:\n - path: ", join(path(n), ", "), "\n - result: ", pow(x, n))
end</langsyntaxhighlight>
 
{{out}}
Line 1,305:
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import java.math.BigDecimal
Line 1,348:
showPow(1.1, 81, false)
showPow(3.0, 191)
}</langsyntaxhighlight>
 
{{out}}
Line 1,414:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[NextStep, TreePow]
NextStep[pows_List] := Module[{maxlen, sel, new, vals, knows},
maxlen = Max[Length /@ pows[[All, "Path"]]];
Line 1,465:
steps = NestWhile[NextStep, pows, Not[MemberQ[#[[All, "P"]], 81]] &];
SelectFirst[steps, #["P"] == 81 &]["Path"];
TreePow[%, 1.1]</langsyntaxhighlight>
{{out}}
<pre>[Graphics object showing the tree]
Line 1,502:
This is a translation of the Kotlin/Python program. The algorithm is the same but there are some changes: replacement of global variables by a tree object, use of longer names, replacement of the "lvl" list of lists by a simple list, use of generics to handle the case of big integers...
 
<langsyntaxhighlight Nimlang="nim">import tables
import bignum
 
Line 1,565:
tree.showPow(1.1, 81)
echo ""
tree.showPow(newInt(3), 191)</langsyntaxhighlight>
 
{{out}}
Line 1,612:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my @lvl = [1];
my %p = (1 => 0);
 
Line 1,655:
use bigint (try => 'GMP');
show_pow(3, 191);
}</langsyntaxhighlight>
{{out}}
<pre style="height:32ex;overflow:scroll">
Line 1,703:
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}})</span>
Line 1,765:
<span style="color: #000000;">showpow</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.1"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">81</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">showpow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">191</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre style="font-size: 12px">
Line 1,791:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from __future__ import print_function
 
# remember the tree generation state and expand on demand
Line 1,818:
for x in range(18): show_pow(2, x)
show_pow(3, 191)
show_pow(1.1, 81)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,844:
=={{header|Racket}}==
{{trans|Python}}
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define pow-path-cache (make-hash '((0 . (0)) (1 . (0 1)))))
Line 1,883:
(show-pow 2 x))
(show-pow 3 191)
(show-pow 1.1 81)</langsyntaxhighlight>
{{out}}
<pre>0: ()
Line 1,929:
(formerly Perl 6)
Paths are random. It is possible replace <code>.pick(*)</code> with <code>.reverse</code> if you want paths as in Perl, or remove it for Python paths.
<syntaxhighlight lang="raku" perl6line>use v6;
 
sub power-path ($n ) {
Line 1,968:
say 'Path for 81: ', power-path 81;
say '1.1 ** 81 = ', power 1.1, 81;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,983:
 
Also, negative powers are supported.
<langsyntaxhighlight lang="rexx">/*REXX program produces & displays a power tree for P, and calculates & displays X^P.*/
numeric digits 1000 /*be able to handle some large numbers.*/
parse arg XP /*get sets: X, low power, high power.*/
Line 2,042:
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: if e<0 then z=format(1/z, , 40)/1; _=right(e, w) /*use reciprocal? */
say left('power tree for ' _ " is: " $,60) '═══' x"^"_ ' is: ' z; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,073:
=={{header|Sidef}}==
{{trans|zkl}}
<langsyntaxhighlight lang="ruby">var lvl = [[1]]
var p = Hash(1 => 0)
 
Line 2,110:
for x in ^18 { show_pow(2, x) }
show_pow(1.1, 81)
show_pow(3, 191)</langsyntaxhighlight>
{{out}}
<pre style="height:32ex;overflow:scroll">
Line 2,159:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigRat
import "/fmt" for Fmt
 
Line 2,203:
for (n in 0..17) showPow.call(2, n)
showPow.call(1.1, 81)
showPow.call(3, 191)</langsyntaxhighlight>
 
{{out}}
Line 2,271:
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl"># remember the tree generation state and expand on demand
fcn path(n,p=Dictionary(1,0),lvl=List(List(1))){
if(n==0) return(T);
Line 2,295:
fmt:="%d: %s\n" + T("%g^%d = %f", "%d^%d = %d")[x==Int(x)] + "\n";
println(fmt.fmt(n,p:=path(n),x,n,tree_pow(x,n,p)))
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach x in (18){ show_pow(2,x) }
show_pow(1.1,81);
 
var [const] BN=Import("zklBigNum"); // GNU GMP big ints
show_pow(BN(3),191);</langsyntaxhighlight>
{{out}}
<pre style="height:32ex;overflow:scroll">
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.