Factors of an integer: Difference between revisions
→{{header|AppleScript}}: Simpler closures
(→{{header|AppleScript}}: Simpler closures) |
|||
Line 256:
<lang AppleScript>-- integerFactors :: Int -> [Int]
on integerFactors(n)
script mf▼
-- isFactor :: Int -> Bool ▼
on isFactor(x)▼
(n of my closure) mod x = 0▼
end isFactor▼
-- integerQuotient :: Int -> Int▼
on integerQuotient(x)▼
((n of my closure) / x) as integer▼
end integerQuotient▼
end script▼
if n = 1 then
{1}
Line 274 ⟶ 262:
set intRoot to realRoot as integer
set blnPerfectSquare to intRoot = realRoot
▲ -- isFactor :: Int -> Bool
end lambda
-- Factors up to square root of n,
set lows to filter
▲ -- integerQuotient :: Int -> Int
on lambda(x)
end lambda
-- and quotients of these factors beyond the square root.
lows & map
items (1 + (blnPerfectSquare as integer)) thru -1 of reverse of lows)
end if
end integerFactors
-- TEST
on run
Line 337 ⟶ 339:
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
▲ else
property lambda : f▼
▲ property lambda : f
end mReturn▼
▲ end script
▲end mReturn</lang>
▲ end script
{{Out}}
|