Anonymous user
Arithmetic/Rational: Difference between revisions
→{{header|Julia}}: Updated for Julia 1.2
(→{{header|Julia}}: Updated for Julia 1.2) |
|||
Line 1,910:
=={{header|Julia}}==
Julia has native support for rational numbers. Rationals are expressed as <tt>m//n</tt>, where <tt>m</tt> and <tt>n</tt> are integers. In addition to supporting most of the usual mathematical functions in a natural way on rationals, the methods <tt>num</tt> and <tt>den</tt> provide the fully reduced numerator and denominator of a rational value.
<lang Julia>using Primes
divisors(n) = foldl((a, (p, e)) -> vcat((a * [p^i for i in 0:e]')...), factor(n), init=[1])
isperfect(n) = sum(1 // d for d in divisors(n)) == 2
lo, hi = 2, 2^19
println("Perfect numbers between ", lo, " and ", hi, ": ", collect(filter(isperfect, lo:hi)))
</lang>
{{out}}
<pre>
</pre>
|