Arithmetic/Rational: Difference between revisions

added Factor
(added Factor)
Line 879:
 
for i in 1..(1<<<19) do if (perf i) then printfn "%i is perfect" i</lang>
 
=={{header|Factor}}==
<code>ratio</code> is a built-in numeric type.
<lang factor>USING: generalizations io kernel math math.functions
math.primes.factors math.ranges prettyprint sequences ;
IN: rosetta-code.arithmetic-rational
 
2/5 ! literal syntax 2/5
2/4 ! automatically simplifies to 1/2
5/1 ! automatically coerced to 5
26/5 ! mixed fraction 5+1/5
13/178 >fraction ! get the numerator and denominator 13 178
8 recip ! get the reciprocal 1/8
 
! ratios can be any size
12417829731289312/61237812937138912735712
8 ndrop ! clear the stack
! arithmetic works the same as any other number.
 
: perfect? ( n -- ? )
divisors rest [ recip ] map-sum 1 = ;
 
"Perfect numbers <= 2^19: " print
2 19 ^ [1,b] [ perfect? ] filter .</lang>
{{out}}
<pre>
Perfect numbers <= 2^19:
V{ 6 28 496 8128 }
</pre>
 
=={{header|Forth}}==
1,808

edits