Proper divisors: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives
(→‎{{header|AppleScript}}: Updated primitives)
Line 291:
 
=={{header|AppleScript}}==
 
 
{{Trans|JavaScript}}
<lang AppleScript>-- PROPER DIVISORS -----------------------------------------------------------
 
<lang AppleScript>-- properDivisors :: Int -> [Int]
on properDivisors(n)
if n = 1 then
Line 306 ⟶ 305:
-- isFactor :: Int -> Bool
script isFactor
on lambda|λ|(x)
n mod x = 0
end lambda|λ|
end script
-- Factors up to square root of n,
set lows to filter(isFactor, rangeenumFromTo(1, intRoot))
-- and quotients of these factors beyond the square root,
Line 318 ⟶ 317:
-- integerQuotient :: Int -> Int
script integerQuotient
on lambda|λ|(x)
(n / x) as integer
end lambda|λ|
end script
Line 330 ⟶ 329:
 
 
-- TEST ----------------------------------------------------------------------
-- TEST
 
on run
-- numberAndDivisors :: Int -> [Int]
script numberAndDivisors
on lambda|λ|(n)
{num:n, divisors:properDivisors(n)}
end lambda|λ|
end script
-- maxDivisorCount :: Record -> Int -> Record
script maxDivisorCount
on lambda|λ|(a, n)
set intDivisors to length of properDivisors(n)
Line 350 ⟶ 348:
a
end if
end lambda|λ|
end script
{oneToTen:map(numberAndDivisors, ¬
rangeenumFromTo(1, 10)), mostDivisors:foldl(maxDivisorCount, ¬
{num:0, divisors:0}, rangeenumFromTo(1, 20000))} ¬
end run
 
 
------------------ GENERIC FUNCTIONS ---------------------------------------------------------
 
-- rangeenumFromTo :: Int -> Int -> [Int]
-- GENERIC LIBRARY FUNCTIONS
on rangeenumFromTo(m, n)
if nm <> mn 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]
Line 371 ⟶ 381:
repeat with i from 1 to lng
set v to item i of xs
if lambda|λ|(v, i, xs) then set end of lst to v
end repeat
return lst
Line 383 ⟶ 393:
set lng to length of xs
repeat with i from 1 to lng
set v to lambda|λ|(v, item i of xs, i, xs)
end repeat
return v
Line 395 ⟶ 405:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m 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 range
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 422 ⟶ 418:
else
script
property lambda|λ| : f
end script
end if
end mReturn</lang>
 
{{Out}}
<lang AppleScript>{oneToTen:{{num:1, divisors:{1}}, {num:2, divisors:{1}}, {num:3, divisors:{1}},
9,659

edits