Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

Content added Content deleted
Line 262: Line 262:


=={{header|J}}==
=={{header|J}}==
=== version 1 ===
===Tacit version===
This version is a modification of an explicit version shown in http://www.jsoftware.com/jwiki/Essays/Continued%20Fractions to comply with the task specifications.
<lang j>cf=. }:@:({:"1@:((, <.)@%@-/@] ::]^:(<_) (, <.)) %&x:&>/)</lang>
==== Examples ====
<lang j> cf each 1 2;3 1;23 8;13 11;22 7;14142136 10000000;_151 77
┌───┬─┬─────┬─────┬───┬─────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2│_2 25 1 2│
└───┴─┴─────┴─────┴───┴─────────────────────────────────┴─────────┘
cf each 14142 10000;141421 100000;1414214 1000000;14142136 10000000
┌──────────────────┬───────────────────────────┬────────────────────────────┬─────────────────────────────────┐
│1 2 2 2 2 2 1 1 29│1 2 2 2 2 2 2 3 1 1 3 1 7 2│1 2 2 2 2 2 2 2 3 6 1 2 1 12│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2│
└──────────────────┴───────────────────────────┴────────────────────────────┴─────────────────────────────────┘
cf each 31 10;314 100;3142 1000;31428 10000;314285 100000;3142857 1000000;31428571 10000000;314285714 100000000
┌────┬─────┬──────────┬───────┬────────┬──────────┬────────────┬───────────┐
│3 10│3 7 7│3 7 23 1 2│3 7 357│3 7 2857│3 7 142857│3 7 476190 3│3 7 7142857│
└────┴─────┴──────────┴───────┴────────┴──────────┴────────────┴───────────┘</lang>
This tacit version first produces the answer with a trailing ∞ (represented by _ in J) which is then removed by the last operation (}:). A continued fraction can be evaluated using the verb ((+%)/) and both representations produce equal results,
<lang j> 3 7 =&((+ %)/) 3 7 _
1</lang>
Incidentally, J and Tcl report a different representation for -151/77 versus the representation of other implementations; however, both representations produce equal results.
<lang j> _2 25 1 2 =&((+ %)/) _1 _1 _24 _1 _2
1</lang>
===Explicit versions===
==== version 1 ====
Implemented as a class, r2cf preserves state in a separate locale. I've used some contrivances to jam the examples onto one line.
Implemented as a class, r2cf preserves state in a separate locale. I've used some contrivances to jam the examples onto one line.
<lang J>
<lang J>
Line 297: Line 320:
│_151 77 │_2 25 1 2 │
│_151 77 │_2 25 1 2 │
└─────────────────┴─────────────────────────────────┘
└─────────────────┴─────────────────────────────────┘
)</lang>
)
==== version 2 ====
</lang>



=== version 2 ===
<lang J>
<lang J>
f =: 3 : 0
f =: 3 : 0
Line 316: Line 335:
┌───┬─┬─────┬─────┬───┬───────────────────────────────────┬─────────┐
┌───┬─┬─────┬─────┬───┬───────────────────────────────────┬─────────┐
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 _│_2 25 1 2│
│0 2│3│2 1 7│1 5 2│3 7│1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2 _│_2 25 1 2│
└───┴─┴─────┴─────┴───┴───────────────────────────────────┴─────────┘
└───┴─┴─────┴─────┴───┴───────────────────────────────────┴─────────┘</lang>
</lang>


=={{header|Mathematica}}==
=={{header|Mathematica}}==