Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
130
Task/Proper-divisors/AppleScript/proper-divisors-1.applescript
Normal file
130
Task/Proper-divisors/AppleScript/proper-divisors-1.applescript
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
-- PROPER DIVISORS -----------------------------------------------------------
|
||||
|
||||
-- properDivisors :: Int -> [Int]
|
||||
on properDivisors(n)
|
||||
if n = 1 then
|
||||
{1}
|
||||
else
|
||||
set realRoot to n ^ (1 / 2)
|
||||
set intRoot to realRoot as integer
|
||||
set blnPerfectSquare to intRoot = realRoot
|
||||
|
||||
-- isFactor :: Int -> Bool
|
||||
script isFactor
|
||||
on |λ|(x)
|
||||
n mod x = 0
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- Factors up to square root of n,
|
||||
set lows to filter(isFactor, enumFromTo(1, intRoot))
|
||||
|
||||
-- and quotients of these factors beyond the square root,
|
||||
|
||||
-- integerQuotient :: Int -> Int
|
||||
script integerQuotient
|
||||
on |λ|(x)
|
||||
(n / x) as integer
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- excluding n itself (last item)
|
||||
items 1 thru -2 of (lows & map(integerQuotient, ¬
|
||||
items (1 + (blnPerfectSquare as integer)) thru -1 of reverse of lows))
|
||||
end if
|
||||
end properDivisors
|
||||
|
||||
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
on run
|
||||
-- numberAndDivisors :: Int -> [Int]
|
||||
script numberAndDivisors
|
||||
on |λ|(n)
|
||||
{num:n, divisors:properDivisors(n)}
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- maxDivisorCount :: Record -> Int -> Record
|
||||
script maxDivisorCount
|
||||
on |λ|(a, n)
|
||||
set intDivisors to length of properDivisors(n)
|
||||
|
||||
if intDivisors ≥ divisors of a then
|
||||
{num:n, divisors:intDivisors}
|
||||
else
|
||||
a
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
{oneToTen:map(numberAndDivisors, ¬
|
||||
enumFromTo(1, 10)), mostDivisors:foldl(maxDivisorCount, ¬
|
||||
{num:0, divisors:0}, enumFromTo(1, 20000))} ¬
|
||||
|
||||
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,5 @@
|
|||
{oneToTen:{{num:1, divisors:{1}}, {num:2, divisors:{1}}, {num:3, divisors:{1}},
|
||||
{num:4, divisors:{1, 2}}, {num:5, divisors:{1}}, {num:6, divisors:{1, 2, 3}},
|
||||
{num:7, divisors:{1}}, {num:8, divisors:{1, 2, 4}}, {num:9, divisors:{1, 3}},
|
||||
{num:10, divisors:{1, 2, 5}}},
|
||||
mostDivisors:{num:18480, divisors:79}}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
on properDivisors(n)
|
||||
set output to {}
|
||||
|
||||
if (n > 1) then
|
||||
set sqrt to n ^ 0.5
|
||||
set limit to sqrt div 1
|
||||
if (limit = sqrt) then
|
||||
set end of output to limit
|
||||
set limit to limit - 1
|
||||
end if
|
||||
repeat with i from limit to 2 by -1
|
||||
if (n mod i is 0) then
|
||||
set beginning of output to i
|
||||
set end of output to n div i
|
||||
end if
|
||||
end repeat
|
||||
set beginning of output to 1
|
||||
end if
|
||||
|
||||
return output
|
||||
end properDivisors
|
||||
|
||||
-- Task code.
|
||||
local output, astid, i, maxPDs, maxPDNums, pdCount
|
||||
set output to {}
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to ", "
|
||||
repeat with i from 1 to 10
|
||||
set end of output to (i as text) & "'s proper divisors: {" & properDivisors(i) & "}"
|
||||
end repeat
|
||||
set maxPDs to 0
|
||||
set maxPDNums to {}
|
||||
repeat with i from 1 to 20000
|
||||
set pdCount to (count properDivisors(i))
|
||||
if (pdCount > maxPDs) then
|
||||
set maxPDs to pdCount
|
||||
set maxPDNums to {i}
|
||||
else if (pdCount = maxPDs) then
|
||||
set end of maxPDNums to i
|
||||
end if
|
||||
end repeat
|
||||
set end of output to linefeed & "Largest number of proper divisors for any number from 1 to 20,000: " & maxPDs
|
||||
set end of output to "Numbers with this many: " & maxPDNums
|
||||
set AppleScript's text item delimiters to linefeed
|
||||
set output to output as text
|
||||
set AppleScript's text item delimiters to astid
|
||||
return output
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
"1's proper divisors: {}
|
||||
2's proper divisors: {1}
|
||||
3's proper divisors: {1}
|
||||
4's proper divisors: {1, 2}
|
||||
5's proper divisors: {1}
|
||||
6's proper divisors: {1, 2, 3}
|
||||
7's proper divisors: {1}
|
||||
8's proper divisors: {1, 2, 4}
|
||||
9's proper divisors: {1, 3}
|
||||
10's proper divisors: {1, 2, 5}
|
||||
|
||||
Largest number of proper divisors for any number from 1 to 20,000: 79
|
||||
Numbers with this many: 15120, 18480"
|
||||
Loading…
Add table
Add a link
Reference in a new issue