Engel expansion: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Julia}}: add number of components to output)
m (syntax highlighting fixup automation)
Line 44:
 
=={{header|J}}==
{{trans|Raku}}<langsyntaxhighlight Jlang="j">to_engle=: {{ r=. i.0 while. y * 30>:#r do. y=. _1+y*{:r=. r, >.%y end. r }}
from_engle=: {{+/%*/\y}}</langsyntaxhighlight>
 
Task examples:<langsyntaxhighlight Jlang="j"> to_engle 3.14159265358979
1 1 1 8 8 17 19 300 1991 2767 8641 16313 1628438 7702318 25297938 431350188 765676622 776491263 1739733589 2329473788 6871947674 17179869184
from_engle to_engle 3.14159265358979
Line 66:
1.41421
1.414213562373095-from_engle to_engle 1.414213562373095
0</langsyntaxhighlight>
(by default, J displays the first six digits of floating point numbers)
 
Stretch goal (note that we seem to have a problem here with e, presumably because of the limited length of the series):<langsyntaxhighlight lang="j"> pi175=: (%10x^175)*<.@o.10x^175
e101=: +/ %@!@i. 101x
sq2_179=: (10x^179)%~<.@%:2*10x^2*179
Line 92:
1.25532e_34
0.0+sq2_179-from_engle to_engle sq2_179
9.66281e_196</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="ruby">tobigrational(s) = (d = length(s) - something(findfirst(==('.'), s), 0); parse(BigInt, replace(s, '.' => "")) // big"10"^d)
 
toEngel(x) = (a = BigInt[]; while x != 0; y = ceil(big"1" // x); push!(a, y); x = x * y - 1; end; a)
Line 123:
"25.628906",
])
</langsyntaxhighlight>{{out}}
<pre>
Number: 3.14159265358979
Line 159:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 215:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Back to rational: %s\n\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
I could only get pi accurate to 125 decimal places and root2 to 87, so cut the input strings accordingly.<br>
Line 306:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub to-engel ($rat is copy) { do while $rat { my $a = ceiling 1 / $rat; $rat = $rat × $a - 1; $a } }
 
sub from-engel (@expanded) { sum [\×] @expanded.map: { FatRat.new: 1, $_ } }
Line 327:
say " Converted back: " ~ @expanded.&from-engel;
put '';
}</langsyntaxhighlight>
{{out}}
<pre>Rational number: 3.14159265358979
Line 367:
 
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.
<langsyntaxhighlight lang="ecmascript">import "./big" for BigRat
import "./fmt" for Fmt
 
Line 407:
Fmt.print("Number of terms : $d", engel.count)
Fmt.print("Back to rational: $s\n", fromEngel.call(engel.take(70).toList).toDecimal(places))
}</langsyntaxhighlight>
 
{{out}}
10,327

edits