Skip to content

DigitSum ​

The sum of the digits of n in the given base (default 10).

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaDigit sumMathWorldDigitSumWikidataQ1363568Wolfram LanguageDigitSum✓Rosetta CodeSum digits of an integer
DigitSum(n)sum of the digits of nn, base 10.
SageMathsum(Integer(abs(n)).digits(10))SymPysum(digits(abs(n))[1:])
DigitSum(n, base)sum of the digits of nn in the given base.
SageMathsum(Integer(abs(n)).digits(base))SymPysum(digits(abs(n), base)[1:])
DigitSum(n, base, k)sum of just the first kk digits (most significant first); a negative kk sums the last ∣k∣|k| instead (least significant first).
Typed signatures
TypePackageApplies
(integer, integer?) -> integercompute-enginealways
(integer, integer?, integer?) -> integernumeralsreplaces compute-enginealways

Domain: Numeral systems

Details
  • Equivalent to summing IntegerDigits(n, base).
  • In base 2, the digit sum is the number of set bits (population count).
  • n≡DigitSum(n)(mod9)n\equiv\mathrm{DigitSum}(n)\pmod9 in base 10 -- the basis of the classic divisibility-by-9 check and digital root.
  • The sign of n is discarded before summing.

Plot

Examples

3 more kept as data, checked against the oracles
Basic #
live ↗
InDigitSum(58 127)\mathrm{DigitSum}(58\,127)Out2323

In base 2 this is the number of set bits

live ↗
InDigitSum(58 127,2)\mathrm{DigitSum}(58\,127, 2)Out99
Scope #

255=ff16255 = \mathrm{ff}_{16}: 15+1515 + 15

live ↗
InDigitSum(255,16)\mathrm{DigitSum}(255, 16)Out3030

Threads element-wise over a list, as Wolfram's Listable heads do

live ↗
InDigitSum([12,345])\mathrm{DigitSum}(\bigl\lbrack12, 345\bigr\rbrack)Out[3,12]\bigl\lbrack3, 12\bigr\rbrack

Just the first 4 digits, 6+3+4+5=186+3+4+5=18, not the full digit sum (30)

live ↗
InDigitSum(6 345 354,10,4)\mathrm{DigitSum}(6\,345\,354, 10, 4)Out1818

Exact digit sum of a roughly thousand-digit integer

live ↗
InDigitSum(1+91 000)\mathrm{DigitSum}(1+9^{1\,000})Out4 3394\,339

In base 16, 58127=E30F1658127=\text{E30F}_{16}, so 14+3+0+15=3214+3+0+15=32

live ↗
InDigitSum(58 127,16)\mathrm{DigitSum}(58\,127, 16)Out3232

Threads over a list in base 2

live ↗
InDigitSum([6,7,2],2)\mathrm{DigitSum}(\bigl\lbrack6, 7, 2\bigr\rbrack, 2)Out[2,3,1]\bigl\lbrack2, 3, 1\bigr\rbrack

Threads over a list of bases

live ↗
InDigitSum(7,[2,3,4])\mathrm{DigitSum}(7, \bigl\lbrack2, 3, 4\bigr\rbrack)Out[3,3,4]\bigl\lbrack3, 3, 4\bigr\rbrack

A negative third argument takes only the last four digits

live ↗
InDigitSum(6 345 354,10,−4)\mathrm{DigitSum}(6\,345\,354, 10, -4)Out1717

Default base 10: 3+5+2+9+73+5+2+9+7

live ↗
InDigitSum(35 297)\mathrm{DigitSum}(35\,297)Out2626
Properties #

Digit sum ≡ n (mod 9): the basis of the classic divisibility-by-9 check and the digital root

live ↗
InDigitSum(58 127) mod 9=58 127 mod 9\mathrm{DigitSum}(58\,127)\bmod9=58\,127\bmod9OutTrue\mathrm{True}

3+7+1+0+33+7+1+0+3

live ↗
InDigitSum(37 103)\mathrm{DigitSum}(37\,103)Out1414

143=100011112143=10001111_2 has five ones

live ↗
InDigitSum(143,2)\mathrm{DigitSum}(143, 2)Out55

The last three digits are all zero

live ↗
InDigitSum(123 000,10,−3)\mathrm{DigitSum}(123\,000, 10, -3)Out00

Zero has digit sum zero

live ↗
InDigitSum(0,10,−3)\mathrm{DigitSum}(0, 10, -3)Out00

In base 2 the digit sum counts the ones

live ↗
InDigitSum(2 147,2)\mathrm{DigitSum}(2\,147, 2)Out55

Base 10 digit sum of 2147

live ↗
InDigitSum(2 147)\mathrm{DigitSum}(2\,147)Out1414

The repunit of 17 ones sums to 1717

live ↗
InDigitSum(1017−19)\mathrm{DigitSum}(\frac{10^{17}-1}{9})Out1717

A third argument at or above the digit count gives the plain sum

live ↗
InDigitSum(123 456,10,7)\mathrm{DigitSum}(123\,456, 10, 7)Out2121

263=20235263=2023_5, so the digits add to 77

live ↗
InDigitSum(263,5)\mathrm{DigitSum}(263, 5)Out77

Reversing the digits does not change the sum

live ↗
InDigitSum(IntegerReverse(263,5),5)\mathrm{DigitSum}(\mathrm{IntegerReverse}(263, 5), 5)Out77

Negative inputs use the absolute value

live ↗
InDigitSum(−3 134)\mathrm{DigitSum}(-3\,134)Out1111
Possible issues #
live ↗
InDigitSum(0)\mathrm{DigitSum}(0)Out00

The sign is discarded

live ↗
InDigitSum(−123)\mathrm{DigitSum}(-123)Out66
Neat examples #
live ↗
InDigitSum(100!)\mathrm{DigitSum}(100!)Out648648

The digit sum of 210002^{1000}

live ↗
InDigitSum(21 000)\mathrm{DigitSum}(2^{1\,000})Out1 3661\,366
About
Examples30
Checked againstWolfram Language 30 agreeSageMath 21 agreeSymPy 21 agreemathlib4 18 agree11 error
CrosswalkWikipediaMathWorldWikidataWolfram LanguageSageMath ×2SymPy ×2Rosetta Code

Implementation

mappedsagepackages/oracle/src/mappings.ts
mappedsagepackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts
mappedmathlib4packages/oracle/src/mappings.ts

The sum of Nat.digits; natural n only, so a negative n (whose sign ours discards) is a domain error.

mappedmathlib4packages/oracle/src/mappings.ts

As the one-argument row, in an integer base.

mappedmathlib4packages/oracle/src/mappings.ts

The first k digits, most significant first; k past the digit count is the plain sum. A negative k (the last |k| digits) is a domain error, as is a negative n.

See also: DigitCount