DigitSum

DigitSum[n]

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)

Find the sum of the decimal digits in :

Find the sum of the binary digits in :

Find the digit sum in a mixed radix system:

Scope  (8)

Compute for large numbers:

Use a base larger than 10:

DigitSum threads over lists:

Find the digit sum of 7 in different bases:

Sum only the first 4 digits:

Sum only the last 4 digits:

Find digit sums using a MixedRadix specification:

Sum only the last 2 digits:

Exact values are generated at integers:

Applications  (1)

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

Properties & Relations  (9)

Use IntegerDigits to compute DigitSum:

Use HammingDistance to compute DigitSum in binary:

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

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

Use DigitCount to compute DigitSum in binary:

Use DigitCount to compute DigitSum in any base:

IntegerLength and DigitSum give for in base :

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

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

The sign is ignored:

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.

CMS

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

BibTeX

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

BibLaTeX

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