Which Mathematica command gives the sum of digits in base 10?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 4K views
amcavoy
Messages
663
Reaction score
0
Which command in Mathematica will give me the sum of the digits of a number in base 10? Thanks.
 
Physics news on Phys.org
So you want it to give you something like this:

Input: 380234
Output: 20
Input: 32138
Output: 17

?
 
Yes, exactly. Do you know how that is done?
 
I had already found that; nothing happens when I plug that into Mathematica. I entered:

Code:
s[3^14, 10] := Plus @@ IntegerDigits[3^14, 10]
and then nothing came out. Is this the correct way to enter it? For this example I was trying to compute the sum of the digits of 314 in base 10. Thanks.
 
That's because you're attempting to define a "function" and haven't asked Mathematica to do anything with it.

For a quick answer, just copy and paste the right side into an input line and evaluate it.