How do I get the sums of the digits of a large number in Haskell?
Posted
by Tim
on Stack Overflow
See other posts from Stack Overflow
or by Tim
Published on 2010-05-15T02:51:47Z
Indexed on
2010/05/15
2:54 UTC
Read the original article
Hit count: 283
haskell
I'm a C++ Programmer trying to teach myself Haskell and it's proving to be challenging grasping the basics of using functions as a type of loop. I have a large number, 50!, and I need to add the sum of its digits. It's a relatively easy loop in C++ but I want to learn how to do it in Haskell.
I've read some introductory guides and am able to get 50! with
sum50fac.hs::
fac 0 = 1 fac n = n * fac n - 1 x = fac 50 main = print x
Unfortunately at this point I'm not entirely sure how to approach the problem. Is it possible to write a function that adds (mod) x 10 to a value and then calls the same function again on x / 10 until x / 10 is less than 10? If that's not possible how should I approach this problem?
Thanks!
© Stack Overflow or respective owner