How can I make a module that finds the sum of digits of any given positive integer?
I use this command
F[x_]:= Apply[ Plus, IntegerDigits[x] ]
Is there another module to do it ?
|show 1 more comment|
Yes, there are other ways. In Mathematica there are usually many other ways.
It can be easily extended to bases other than 10:
In base 7:
Note that there is no need to use
Also since I assume you are a new Mathematica user (and even if you're not) I recommend referencing What are the most common pitfalls awaiting new users?.
At some point you may wish to read How to Combine Pattern Constraints and Default Values for Function Arguments for an understanding of the second parameter pattern I used in the extended definition of