September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,10 +1,18 @@
|
|||
import Data.Ratio
|
||||
import Data.Ratio ((%))
|
||||
|
||||
-- simply prints all the perfect numbers
|
||||
main = mapM_ print [candidate
|
||||
| candidate <- [2 .. 2^19],
|
||||
getSum candidate == 1]
|
||||
where getSum candidate = 1 % candidate +
|
||||
sum [1 % factor + 1 % (candidate `div` factor)
|
||||
| factor <- [2 .. floor(sqrt(fromIntegral(candidate)))],
|
||||
candidate `mod` factor == 0]
|
||||
-- Prints the first N perfect numbers.
|
||||
main = do
|
||||
let n = 4
|
||||
mapM_ print $
|
||||
take
|
||||
n
|
||||
[ candidate
|
||||
| candidate <- [2 .. 2 ^ 19]
|
||||
, getSum candidate == 1 ]
|
||||
where
|
||||
getSum candidate =
|
||||
1 % candidate +
|
||||
sum
|
||||
[ 1 % factor + 1 % (candidate `div` factor)
|
||||
| factor <- [2 .. floor (sqrt (fromIntegral candidate))]
|
||||
, candidate `mod` factor == 0 ]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
for 2..2**19 -> $candidate {
|
||||
(2..2**19).hyper.map: -> $candidate {
|
||||
my $sum = 1 / $candidate;
|
||||
for 2 .. ceiling(sqrt($candidate)) -> $factor {
|
||||
if $candidate %% $factor {
|
||||
|
|
|
|||
28
Task/Arithmetic-Rational/Phix/arithmetic-rational-2.phix
Normal file
28
Task/Arithmetic-Rational/Phix/arithmetic-rational-2.phix
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
include builtins/mpfr.e
|
||||
function is_perfect(integer num)
|
||||
mpq tot = mpq_init(),
|
||||
fth = mpq_init()
|
||||
sequence f = factors(num,1)
|
||||
for i=1 to length(f) do
|
||||
mpq_set_si(fth,1,f[i])
|
||||
mpq_add(tot,tot,fth)
|
||||
end for
|
||||
return mpq_cmp_si(tot,2,1)=0
|
||||
end function
|
||||
|
||||
procedure get_perfect_numbers()
|
||||
atom t0 = time()
|
||||
for i=2 to power(2,19) do
|
||||
if is_perfect(i) then
|
||||
printf(1,"perfect: %d\n",i)
|
||||
end if
|
||||
end for
|
||||
printf(1,"elapsed: %3.2f seconds\n",time()-t0)
|
||||
|
||||
integer pn5 = power(2,12)*(power(2,13)-1) -- 5th perfect number
|
||||
if is_perfect(pn5) then
|
||||
printf(1,"perfect: %d\n",pn5)
|
||||
end if
|
||||
end procedure
|
||||
|
||||
get_perfect_numbers()
|
||||
Loading…
Add table
Add a link
Reference in a new issue