Arithmetic/Rational: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: Use lang tags)
Line 674: Line 674:
J implements rational numbers:
J implements rational numbers:


3r4*2r5
<lang j> 3r4*2r5
3r10
3r10</lang>


That said, note that J's floating point numbers work just fine for the stated problem:
That said, note that J's floating point numbers work just fine for the stated problem:
is_perfect_rational=: 2 = (1 + i.) +/@:%@([ #~ 0 = |) ]
<lang j> is_perfect_rational=: 2 = (1 + i.) +/@:%@([ #~ 0 = |) ]</lang>


faster version (but the problem, as stated, is still tremendously inefficient):
faster version (but the problem, as stated, is still tremendously inefficient):
factors=: ([: ~. ] */ .^"1 2: #:@i.@^ #)@q: ::]
<lang j> factors=: ([: ~. ] */ .^"1 2: #:@i.@^ #)@q: ::]
is_perfect_rational=: 2= +/@:%@factors
is_perfect_rational=: 2= +/@:%@factors</lang>


I.is_perfect_rational@"0 i.2^19
<lang j> I.is_perfect_rational@"0 i.2^19
6 28 496 8128
6 28 496 8128
I.is_perfect_rational@x:@"0 i.2^19x
I.is_perfect_rational@x:@"0 i.2^19x
6 28 496 8128
6 28 496 8128</lang>


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