Skip to content

Instantly share code, notes, and snippets.

@ekmett
Last active March 13, 2019 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekmett/cc7998be62f83cb92ad7dd2f47001eda to your computer and use it in GitHub Desktop.
Save ekmett/cc7998be62f83cb92ad7dd2f47001eda to your computer and use it in GitHub Desktop.
Falling sum
-- emit o numbers in an arithmetic series stepping by -r such that the sum
-- of the output numbers is n
-- assumes o > 0
allen :: Double -> Double -> Int -> [Double]
allen n r o = take o [base,base-r..] where
z = fromIntegral o
base = n/z + (z-1)*r/2
Examples:
*Main> allen 33 5 1
[33.0]
*Main> allen 33 5 2
[19.0,14.0]
*Main> allen 33 5 3
[16.0,11.0,6.0]
*Main> allen 33 5 4
[15.75,10.75,5.75,0.75]
*Main> allen 33 5 5
[16.6,11.6,6.6,1.6,-3.4]
*Main> allen 33 1 2
[17.0,16.0]
*Main> allen 33 1 3
[12.0,11.0,10.0]
*Main> allen 33 1 4
[9.75,8.75,7.75,6.75]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment