Skip to content

DigitCount ​

Counts of each digit (1 through 9, then 0) occurring in n, base 10 by default.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageDigitCount✓
DigitCount(n)counts of each digit 1-9 then 0, base 10.
DigitCount(n, base)counts of each digit in the given base, highest digit value first.
DigitCount(n, base, digit)count of just one digit value.
DigitCount(n, base, digit(s), width)counted over exactly width digits, padded or truncated to fit.
Typed signatures
TypePackageApplies
(integer, integer?, integer?) -> integer | list<integer>compute-enginealways
(integer, integer?, (integer | list<integer>)?, integer?) -> integer | list<integer>numeralsreplaces compute-enginealways

Domain: Numeral systems

Details
  • Equivalent to tallying IntegerDigits(n, base) bucket by bucket.
  • The default base-10 form orders counts 1 through 9, then 0 last.
  • The 3-argument form isolates the count of a single digit value.
  • Trailing zeros count individually -- 122000 has three trailing 0 digits, not a single "trailing zeros" tally.

Examples

Basic #
live ↗
InDigitCount(2 147)\mathrm{DigitCount}(2\,147)Out[1,1,0,1,0,0,1,0,0,0]\bigl\lbrack1, 1, 0, 1, 0, 0, 1, 0, 0, 0\bigr\rbrack

In base 2, counts of digit 1 then digit 0

live ↗
InDigitCount(2 147,2)\mathrm{DigitCount}(2\,147, 2)Out[5,7]\bigl\lbrack5, 7\bigr\rbrack

2147=10000110001122147 = 100001100011_2 has five 1 bits

live ↗
InDigitCount(2 147,2,1)\mathrm{DigitCount}(2\,147, 2, 1)Out55
live ↗
InDigitCount(122 000)\mathrm{DigitCount}(122\,000)Out[1,2,0,0,0,0,0,0,0,3]\bigl\lbrack1, 2, 0, 0, 0, 0, 0, 0, 0, 3\bigr\rbrack
Scope #

an explicit base 10 is the default

live ↗
InDigitCount(122 000,10)\mathrm{DigitCount}(122\,000, 10)Out[1,2,0,0,0,0,0,0,0,3]\bigl\lbrack1, 2, 0, 0, 0, 0, 0, 0, 0, 3\bigr\rbrack

the digits of 100!100!, 30 of them zeros

live ↗
InDigitCount(100!)\mathrm{DigitCount}(100!)Out[15,19,10,10,14,19,7,14,20,30]\bigl\lbrack15, 19, 10, 10, 14, 19, 7, 14, 20, 30\bigr\rbrack

a width counts padding zeros too: 000122000000122000 has six

live ↗
InDigitCount(122 000,10,0,9)\mathrm{DigitCount}(122\,000, 10, 0, 9)Out66

a list of digit values gives one count each

live ↗
InDigitCount(242 442 422,3,[1,2])\mathrm{DigitCount}(242\,442\,422, 3, \bigl\lbrack1, 2\bigr\rbrack)Out[6,7]\bigl\lbrack6, 7\bigr\rbrack

counts over the 12 least-significant base-3 digits

live ↗
InDigitCount(242 442 422,3,[1,2],12)\mathrm{DigitCount}(242\,442\,422, 3, \bigl\lbrack1, 2\bigr\rbrack, 12)Out[4,4]\bigl\lbrack4, 4\bigr\rbrack

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

live ↗
InDigitCount([23,45])\mathrm{DigitCount}(\bigl\lbrack23, 45\bigr\rbrack)Out[[0,1,1,0,0,0,0,0,0,0],[0,0,0,1,1,0,0,0,0,0]]\bigl\lbrack\bigl\lbrack0, 1, 1, 0, 0, 0, 0, 0, 0, 0\bigr\rbrack, \bigl\lbrack0, 0, 0, 1, 1, 0, 0, 0, 0, 0\bigr\rbrack\bigr\rbrack
Applications #

The 3-argument form isolates a single digit's count

live ↗
InDigitCount(2 147,10,1)\mathrm{DigitCount}(2\,147, 10, 1)Out11
Properties #

Single-digit form matches counting that digit in the digit list

live ↗
InDigitCount(242 442 422,3,0)=Count(IntegerDigits(242 442 422,3),0)\mathrm{DigitCount}(242\,442\,422, 3, 0)=\mathrm{Count}(\mathrm{IntegerDigits}(242\,442\,422, 3), 0)OutTrue\mathrm{True}

A fourth argument counts leading zeros in a fixed-width field

live ↗
InDigitCount(242 442 422,3,0,20)=Count(PadLeft(IntegerDigits(242 442 422,3),20),0)\mathrm{DigitCount}(242\,442\,422, 3, 0, 20)=\mathrm{Count}(\mathrm{PadLeft}(\mathrm{IntegerDigits}(242\,442\,422, 3), 20), 0)OutTrue\mathrm{True}

Result lists counts for digits 1,…,b−11,\dots,b-1 then 00; zero is a single 00

live ↗
InDigitCount(0,2)\mathrm{DigitCount}(0, 2)Out[0,1]\bigl\lbrack0, 1\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

One binary digit 11, no zeros

live ↗
InDigitCount(1,2)\mathrm{DigitCount}(1, 2)Out[1,0]\bigl\lbrack1, 0\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

10210_2: one of each

live ↗
InDigitCount(2,2)\mathrm{DigitCount}(2, 2)Out[1,1]\bigl\lbrack1, 1\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

11211_2 has two ones

live ↗
InDigitCount(3,2)\mathrm{DigitCount}(3, 2)Out[2,0]\bigl\lbrack2, 0\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

1002100_2: one 1 and two 0s

live ↗
InDigitCount(4,2)\mathrm{DigitCount}(4, 2)Out[1,2]\bigl\lbrack1, 2\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

1012101_2 has two ones and one zero

live ↗
InDigitCount(5,2)\mathrm{DigitCount}(5, 2)Out[2,1]\bigl\lbrack2, 1\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

1102110_2 has the same counts as 1012101_2

live ↗
InDigitCount(6,2)\mathrm{DigitCount}(6, 2)Out[2,1]\bigl\lbrack2, 1\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

1112111_2 has no zeros

live ↗
InDigitCount(7,2)\mathrm{DigitCount}(7, 2)Out[3,0]\bigl\lbrack3, 0\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

100021000_2: a power of two has one 1

live ↗
InDigitCount(8,2)\mathrm{DigitCount}(8, 2)Out[1,3]\bigl\lbrack1, 3\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

100121001_2 balances ones and zeros

live ↗
InDigitCount(9,2)\mathrm{DigitCount}(9, 2)Out[2,2]\bigl\lbrack2, 2\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

101021010_2 also two of each

live ↗
InDigitCount(10,2)\mathrm{DigitCount}(10, 2)Out[2,2]\bigl\lbrack2, 2\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

Zero in base 3, counted under the final slot for digit 00

live ↗
InDigitCount(0,3)\mathrm{DigitCount}(0, 3)Out[0,0,1]\bigl\lbrack0, 0, 1\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

Count vector runs over digits 1,2,01,2,0

live ↗
InDigitCount(1,3)\mathrm{DigitCount}(1, 3)Out[1,0,0]\bigl\lbrack1, 0, 0\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

A lone digit 2 shows in the middle slot

live ↗
InDigitCount(2,3)\mathrm{DigitCount}(2, 3)Out[0,1,0]\bigl\lbrack0, 1, 0\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

10310_3: a one and a zero

live ↗
InDigitCount(3,3)\mathrm{DigitCount}(3, 3)Out[1,0,1]\bigl\lbrack1, 0, 1\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

11311_3 has two ones

live ↗
InDigitCount(4,3)\mathrm{DigitCount}(4, 3)Out[2,0,0]\bigl\lbrack2, 0, 0\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

12312_3: one of digit 1, one of digit 2

live ↗
InDigitCount(5,3)\mathrm{DigitCount}(5, 3)Out[1,1,0]\bigl\lbrack1, 1, 0\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

20320_3: a two and a zero

live ↗
InDigitCount(6,3)\mathrm{DigitCount}(6, 3)Out[0,1,1]\bigl\lbrack0, 1, 1\bigr\rbrack

Known value · Wolfram MathematicalFunctionData, DigitCount

Possible issues #

Three trailing zeros, not four: 122000122000 has digits 1,2,2,0,0,01,2,2,0,0,0

live ↗
InDigitCount(122 000,10,0)\mathrm{DigitCount}(122\,000, 10, 0)Out33

0 has one digit, a zero

live ↗
InDigitCount(0)\mathrm{DigitCount}(0)Out[0,0,0,0,0,0,0,0,0,1]\bigl\lbrack0, 0, 0, 0, 0, 0, 0, 0, 0, 1\bigr\rbrack
About
Examples33
Checked againstWolfram Language 33 agree
CrosswalkWolfram Language

See also: IntegerDigits, DigitSum