Engel expansion: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added stretch task.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(One intermediate revision by one other user not shown)
Line 156:
Engel expansion: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 33, 33, 35, 58, 62, 521, 3125] (34 components)
Back to rational: 25.628906
</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
engel_encode(x) := block (
[a:[]],
while(x > 0) do (
ai: ceiling(1/x),
x: x*ai - 1,
a: append(a, [ai])
),
return(a)
);
engel_decode(a) := block (
[x:0, my_product:1],
for ai in a do (
my_product: my_product*ai,
x: x + 1/(my_product)
),
return(x)
);
</syntaxhighlight>
{{out}}
<pre>
engel_encode(3.14159265358979);
[1,1,1,8,8,17,19,300,1991,2767,8641,16313,1628438,7702318,25297938,431350188,765676622,776491263,1739733589,2329473788,6871947674,17179869184]
engel_decode(%);
7074237752028433/2251799813685248
 
engel_encode(2.71828182845904);
[1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17,60,89,126,565,686,1293,7419,13529,59245,65443,133166,225384,655321,656924,2365071,2618883,5212339,107374183,178956971,536870912]
engel_decode(%);
3060513257434031/1125899906842624
 
engel_encode(1.414213562373095);
[1,3,5,5,16,18,78,102,120,144,277,286,740,38370,118617,120453,169594,5696244,6316129,10129640,67108864]
engel_decode(%);
1592262918131443/1125899906842624
</pre>
 
Line 645 ⟶ 683:
 
However, I've also limited the number of terms accumulated by the 'fromEngel' function to 70 which is just enough to reproduce the high precision rationals in decimal notation. To accumulate all the terms in a reasonable time would require the use of Wren-gmp which I've tried to avoid so the solution will run under Wren-CLI.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigRat
import "./fmt" for Fmt
 
9,476

edits