WOLFRAM

gives the sum of the decimal digits in the integer n.

DigitSum[n,b]

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

DigitSum[n,b,k]

gives the sum of the first k base b digits in the integer n.

DigitSum[n,b,-k]

gives the sum of the last k base b digits in the integer n.

DigitSum[n,MixedRadix[blist]]

uses the mixed radix with list of bases blist.

Details

Examples

open allclose all

Basic Examples  (3)Summary of the most common use cases

Find the sum of the decimal digits in :

Out[1]=1

Find the sum of the binary digits in :

Out[1]=1

Find the digit sum in a mixed radix system:

Out[1]=1

Scope  (8)Survey of the scope of standard use cases

Compute for large numbers:

Out[9]=9

Use a base larger than 10:

Out[1]=1

DigitSum threads over lists:

Out[1]=1

Find the digit sum of 7 in different bases:

Out[1]=1

Sum only the first 4 digits:

Out[1]=1

Sum only the last 4 digits:

Out[2]=2

Find digit sums using a MixedRadix specification:

Out[1]=1

Sum only the last 2 digits:

Out[1]=1

Exact values are generated at integers:

Out[1]=1

Applications  (1)Sample problems that can be solved with this function

Plot the sum of the digits in the first 100 positive integers:

Out[1]=1

Properties & Relations  (9)Properties of the function, and connections to other functions

Use IntegerDigits to compute DigitSum:

Out[6]=6
Out[7]=7

Use HammingDistance to compute DigitSum in binary:

Out[1]=1
Out[2]=2

DigitSum[n,b,-k] gives 0 when k is less than or equal to IntegerExponent[n,b]:

Out[1]=1
Out[2]=2

In particular, DigitSum[0,b,k] is always 0:

Out[3]=3
Out[4]=4

Use DigitCount to compute DigitSum in binary:

Out[1]=1
Out[2]=2

Use DigitCount to compute DigitSum in any base:

Out[1]=1
Out[2]=2

IntegerLength and DigitSum give for in base :

Out[1]=1
Out[2]=2

DigitSum[n,b,k] is equivalent to DigitSum[n,b] when k is greater than the integer length of n in base b:

Out[1]=1
Out[2]=2

DigitSum gives the same result for n and IntegerReverse[n,b]:

Out[1]=1
Out[2]=2

The sign is ignored:

Out[1]=1
Wolfram Research (2024), DigitSum, Wolfram Language function, https://reference.wolfram.com/language/ref/DigitSum.html.
Wolfram Research (2024), DigitSum, Wolfram Language function, https://reference.wolfram.com/language/ref/DigitSum.html.

Text

Wolfram Research (2024), DigitSum, Wolfram Language function, https://reference.wolfram.com/language/ref/DigitSum.html.

Wolfram Research (2024), DigitSum, Wolfram Language function, https://reference.wolfram.com/language/ref/DigitSum.html.

CMS

Wolfram Language. 2024. "DigitSum." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/DigitSum.html.

Wolfram Language. 2024. "DigitSum." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/DigitSum.html.

APA

Wolfram Language. (2024). DigitSum. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DigitSum.html

Wolfram Language. (2024). DigitSum. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DigitSum.html

BibTeX

@misc{reference.wolfram_2025_digitsum, author="Wolfram Research", title="{DigitSum}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/DigitSum.html}", note=[Accessed: 26-March-2025 ]}

@misc{reference.wolfram_2025_digitsum, author="Wolfram Research", title="{DigitSum}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/DigitSum.html}", note=[Accessed: 26-March-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_digitsum, organization={Wolfram Research}, title={DigitSum}, year={2024}, url={https://reference.wolfram.com/language/ref/DigitSum.html}, note=[Accessed: 26-March-2025 ]}

@online{reference.wolfram_2025_digitsum, organization={Wolfram Research}, title={DigitSum}, year={2024}, url={https://reference.wolfram.com/language/ref/DigitSum.html}, note=[Accessed: 26-March-2025 ]}