Engel expansion: Difference between revisions

Content added Content deleted
m (→‎{{header|Julia}}: add number of components to output)
m (syntax highlighting fixup automation)
Line 44: Line 44:


=={{header|J}}==
=={{header|J}}==
{{trans|Raku}}<lang J>to_engle=: {{ r=. i.0 while. y * 30>:#r do. y=. _1+y*{:r=. r, >.%y end. r }}
{{trans|Raku}}<syntaxhighlight lang="j">to_engle=: {{ r=. i.0 while. y * 30>:#r do. y=. _1+y*{:r=. r, >.%y end. r }}
from_engle=: {{+/%*/\y}}</lang>
from_engle=: {{+/%*/\y}}</syntaxhighlight>


Task examples:<lang J> to_engle 3.14159265358979
Task examples:<syntaxhighlight lang="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
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
from_engle to_engle 3.14159265358979
Line 66: Line 66:
1.41421
1.41421
1.414213562373095-from_engle to_engle 1.414213562373095
1.414213562373095-from_engle to_engle 1.414213562373095
0</lang>
0</syntaxhighlight>
(by default, J displays the first six digits of floating point numbers)
(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):<lang j> pi175=: (%10x^175)*<.@o.10x^175
Stretch goal (note that we seem to have a problem here with e, presumably because of the limited length of the series):<syntaxhighlight lang="j"> pi175=: (%10x^175)*<.@o.10x^175
e101=: +/ %@!@i. 101x
e101=: +/ %@!@i. 101x
sq2_179=: (10x^179)%~<.@%:2*10x^2*179
sq2_179=: (10x^179)%~<.@%:2*10x^2*179
Line 92: Line 92:
1.25532e_34
1.25532e_34
0.0+sq2_179-from_engle to_engle sq2_179
0.0+sq2_179-from_engle to_engle sq2_179
9.66281e_196</lang>
9.66281e_196</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang ruby>tobigrational(s) = (d = length(s) - something(findfirst(==('.'), s), 0); parse(BigInt, replace(s, '.' => "")) // big"10"^d)
<syntaxhighlight 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)
toEngel(x) = (a = BigInt[]; while x != 0; y = ceil(big"1" // x); push!(a, y); x = x * y - 1; end; a)
Line 123: Line 123:
"25.628906",
"25.628906",
])
])
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Number: 3.14159265358979
Number: 3.14159265358979
Line 159: Line 159:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: 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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
I could only get pi accurate to 125 decimal places and root2 to 87, so cut the input strings accordingly.<br>
I could only get pi accurate to 125 decimal places and root2 to 87, so cut the input strings accordingly.<br>
Line 306: Line 306:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>sub to-engel ($rat is copy) { do while $rat { my $a = ceiling 1 / $rat; $rat = $rat × $a - 1; $a } }
<syntaxhighlight lang="raku" line>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, $_ } }
sub from-engel (@expanded) { sum [\×] @expanded.map: { FatRat.new: 1, $_ } }
Line 327: Line 327:
say " Converted back: " ~ @expanded.&from-engel;
say " Converted back: " ~ @expanded.&from-engel;
put '';
put '';
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Rational number: 3.14159265358979
<pre>Rational number: 3.14159265358979
Line 367: 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.
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.
<lang ecmascript>import "./big" for BigRat
<syntaxhighlight lang="ecmascript">import "./big" for BigRat
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 407: Line 407:
Fmt.print("Number of terms : $d", engel.count)
Fmt.print("Number of terms : $d", engel.count)
Fmt.print("Back to rational: $s\n", fromEngel.call(engel.take(70).toList).toDecimal(places))
Fmt.print("Back to rational: $s\n", fromEngel.call(engel.take(70).toList).toDecimal(places))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}