Sum multiples of 3 and 5: Difference between revisions

Content added Content deleted
Line 15: Line 15:
{{out}}
{{out}}
<pre>233168</pre>
<pre>233168</pre>


== {{header|AppleScript}} ==

{{Trans|JavaScript}}
<lang AppleScript>on run
-- sums of all multiples of 3 or 5 below or equal to N
-- for N = 10 to N = 10E8 (limit of AS integers)
foldl(sum35Result, "", map(tenE, range(1, 8)))
end run

-- sum35 :: Int -> Int
on sum35(n)
sumMults(n, 3) + sumMults(n, 5) - sumMults(n, 15)
end sum35

-- Area under straight line between first multiple and last:

-- sumMults :: Int -> Int -> Int
on sumMults(n, factor)
set n1 to (n - 1) div factor
factor * n1 * (n1 + 1) div 2
end sumMults



-- TESTING

-- tenE :: Int -> Int
on tenE(e)
10 ^ e
end tenE

-- sum35Result :: String -> Int -> String
on sum35Result(a, x, i)
(a & (x as integer as string)) & "<sup>" & i & ¬
"</sup> -> " & sum35(x) & "<br>"
end sum35Result


-- GENERIC FUNCTIONS

-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set mf to mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map

-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
set mf to mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to mf's lambda(v, item i of xs, i, xs)
end repeat
return v
end foldl

-- range :: Int -> Int -> [Int]
on range(m, n)
set d to 1
if n < m then set d to -1
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
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then return f
script
property lambda : f
end script
end mReturn

-- mClosure :: Handler -> Record -> Script
on mClosure(f, recBindings)
script
property closure : recBindings
property lambda : f
end script
end mClosure
</lang>


{{Out}}
10<sup>1</sup> -> 23<br>100<sup>2</sup> -> 2318<br>1000<sup>3</sup> -> 233168<br>10000<sup>4</sup> -> 23331668<br>100000<sup>5</sup> -> 2.333316668E+9<br>1000000<sup>6</sup> -> 2.33333166668E+11<br>10000000<sup>7</sup> -> 2.333333166667E+13<br>100000000<sup>8</sup> -> 2.333333316667E+15<br>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==