Arithmetic/Rational: Difference between revisions

→‎{{header|Julia}}: A new entry for Julia
No edit summary
(→‎{{header|Julia}}: A new entry for Julia)
Line 1,687:
<div style="text-align:right;font-size:7pt">''<nowiki>[</nowiki>This section is included from [[Arithmetic/Rational/JavaScript|a subpage]] and should be edited there, not here.<nowiki>]</nowiki>''</div>
{{:Arithmetic/Rational/JavaScript}}
 
=={{header|Julia}}==
Julian 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>
function isperfect{T<:Integer}(n::T)
!isprime(n) || return false
tal = 1//n
hi = isqrt(n)
if hi^2 == n
tal += 1//hi
hi -= 1
end
for i in 2:hi
(d, r) = divrem(n, i)
if r == 0
tal += (1//i + 1//d)
end
end
return tal == 1//1
end
 
lo = 2
hi = 2^19
println("Searching for perfect numbers from ", lo, " to ", hi, ".")
for i in 2:2^19
isperfect(i) || continue
println(@sprintf("%8d", i))
end
</lang>
 
{{out}}
<pre>
Searching for perfect numbers from 2 to 524288.
6
28
496
8128
</pre>
 
=={{header|Lua}}==