Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
108
Task/Perfect-numbers/AppleScript/perfect-numbers-1.applescript
Normal file
108
Task/Perfect-numbers/AppleScript/perfect-numbers-1.applescript
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
-- PERFECT NUMBERS -----------------------------------------------------------
|
||||
|
||||
-- perfect :: integer -> bool
|
||||
on perfect(n)
|
||||
|
||||
-- isFactor :: integer -> bool
|
||||
script isFactor
|
||||
on |λ|(x)
|
||||
n mod x = 0
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- quotient :: number -> number
|
||||
script quotient
|
||||
on |λ|(x)
|
||||
n / x
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- sum :: number -> number -> number
|
||||
script sum
|
||||
on |λ|(a, b)
|
||||
a + b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- Integer factors of n below the square root
|
||||
set lows to filter(isFactor, enumFromTo(1, (n ^ (1 / 2)) as integer))
|
||||
|
||||
-- low and high factors (quotients of low factors) tested for perfection
|
||||
(n > 1) and (foldl(sum, 0, (lows & map(quotient, lows))) / 2 = n)
|
||||
end perfect
|
||||
|
||||
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
on run
|
||||
|
||||
filter(perfect, enumFromTo(1, 10000))
|
||||
|
||||
--> {6, 28, 496, 8128}
|
||||
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m > n then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end enumFromTo
|
||||
|
||||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
on filter(f, xs)
|
||||
tell mReturn(f)
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to item i of xs
|
||||
if |λ|(v, i, xs) then set end of lst to v
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end filter
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1 @@
|
|||
{6, 28, 496, 8128}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
on aliquotSum(n)
|
||||
if (n < 2) then return 0
|
||||
set sum to 1
|
||||
set sqrt to n ^ 0.5
|
||||
set limit to sqrt div 1
|
||||
if (limit = sqrt) then
|
||||
set sum to sum + limit
|
||||
set limit to limit - 1
|
||||
end if
|
||||
repeat with i from 2 to limit
|
||||
if (n mod i is 0) then set sum to sum + i + n div i
|
||||
end repeat
|
||||
|
||||
return sum
|
||||
end aliquotSum
|
||||
|
||||
on isPerfect(n)
|
||||
if (n > 1.37438691328E+11) then return missing value -- Too high for perfection to be determinable.
|
||||
-- All the known perfect numbers listed in Wikipedia end with either 6 or 28.
|
||||
-- These endings are either preceded by odd digits or are the numbers themselves.
|
||||
tell (n mod 10) to ¬
|
||||
return ((((it = 6) and ((n mod 20 = 16) or (n = 6))) or ¬
|
||||
((it = 8) and ((n mod 200 = 128) or (n = 28)))) and ¬
|
||||
(my aliquotSum(n) = n))
|
||||
end isPerfect
|
||||
|
||||
local output, n
|
||||
set output to {}
|
||||
repeat with n from 1 to 10000
|
||||
if (isPerfect(n)) then set end of output to n
|
||||
end repeat
|
||||
return output
|
||||
|
|
@ -0,0 +1 @@
|
|||
{6, 28, 496, 8128}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
on isPerfect(n)
|
||||
-- All the known perfect numbers listed in Wikipedia end with either 6 or 28.
|
||||
-- These endings are either preceded by odd digits or are the numbers themselves.
|
||||
tell (n mod 10) to ¬
|
||||
if not (((it = 6) and ((n mod 20 = 16) or (n = 6))) or ((it = 8) and ((n mod 200 = 128) or (n = 28)))) then ¬
|
||||
return false
|
||||
-- Work through the only seven primes p where (2 ^ p - 1) is also prime
|
||||
-- and (2 ^ p - 1) * (2 ^ (p - 1)) is a number that AppleScript can handle.
|
||||
repeat with p in {2, 3, 5, 7, 13, 17, 19}
|
||||
tell (2 ^ p - 1) * (2 ^ (p - 1))
|
||||
if (it < n) then
|
||||
else
|
||||
return (it = n)
|
||||
end if
|
||||
end tell
|
||||
end repeat
|
||||
return missing value
|
||||
end isPerfect
|
||||
|
||||
local output, n
|
||||
set output to {}
|
||||
repeat with n from 2 to 33551000 by 2
|
||||
if (isPerfect(n)) then set end of output to n
|
||||
end repeat
|
||||
return output
|
||||
|
|
@ -0,0 +1 @@
|
|||
{6, 28, 496, 8128, 33550336}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
on isPerfect(n)
|
||||
if (n > 1.37438691328E+11) then return missing value -- Too high for perfection to be determinable.
|
||||
return (n is in {6, 28, 496, 8128, 33550336, 8.589869056E+9, 1.37438691328E+11})
|
||||
end isPerfect
|
||||
Loading…
Add table
Add a link
Reference in a new issue