Skip to content

Instantly share code, notes, and snippets.

@samidarko
Last active April 2, 2018 05:58
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 samidarko/de61edebd97bc28a8285ec2860fc56e0 to your computer and use it in GitHub Desktop.
Save samidarko/de61edebd97bc28a8285ec2860fc56e0 to your computer and use it in GitHub Desktop.
find divisors of n
findDivisors :: (RealFrac a, Integral b, Floating a) => a -> [b]
findDivisors n = fn (truncate . sqrt $ n)
where
n' = truncate n
fn i
| i == 1 = 1:n':[]
| n' `mod` i == 0 = let n_by_i = (n' `div` i)
in if n_by_i == i then i : fn next
else i : n_by_i : fn next
| otherwise = fn next
where next = i-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment