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