Function Repository Resource:

DigitSum

Source Notebook

Find the sum of digits in the positional representation of an integer

Contributed by: Wolfram|Alpha Math Team

ResourceFunction["DigitSum"][n]

gives the sum of digits in the base-10 representation of the integer n.

ResourceFunction["DigitSum"][n,b]

gives the sum of digits in the base-b representation of integer n.

Details and Options

ResourceFunction["DigitSum"] ignores the sign of its input.

Examples

Basic Examples (3) 

Compute a digit sum in base 10:

In[1]:=
ResourceFunction["DigitSum"][12]
Out[1]=

Compute another digit sum:

In[2]:=
ResourceFunction["DigitSum"][1000]
Out[2]=

Compute a digit sum in another numerical base:

In[3]:=
ResourceFunction["DigitSum"][11, 3]
Out[3]=

Compare this to the output of IntegerDigits:

In[4]:=
digs = IntegerDigits[11, 3]
Out[4]=
In[5]:=
Total[digs]
Out[5]=

Properties and Relations (2) 

DigitSum ignores the sign of its input:

In[6]:=
ResourceFunction["DigitSum"][-55]
Out[6]=

The digit sum of 0 is 0 in any base:

In[7]:=
ResourceFunction["DigitSum"][0]
Out[7]=
In[8]:=
ResourceFunction["DigitSum"][0, 5]
Out[8]=

Publisher

Wolfram|Alpha Math Team

Version History

  • 4.0.0 – 23 March 2023
  • 3.1.0 – 11 May 2021
  • 3.0.0 – 24 January 2020
  • 2.0.0 – 06 September 2019
  • 1.0.0 – 31 July 2019

Related Resources

License Information