Skip to content

IntegerDigits ​

The digits of nn, most significant first — base 10 by default. The base slot takes a whole numeral SYSTEM, not only an integer — factorial, Zeckendorf, Ostrowski, balanced, negative, bijective, mixed-radix, primorial, combinatorial or residue.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaNumerical digitMathWorldDigitWikidataQ82990Wolfram LanguageIntegerDigits!Rosetta CodeSum digits of an integer
IntegerDigits(n)digits of nn in base 10, most significant first.
SymPydigits(n)[1:]Oscarreverse(digits(BigInt(n)))
IntegerDigits(n, base)digits of nn in the given base.
SymPydigits(n, base)[1:]Oscarreverse(digits(BigInt(n), base=base))
IntegerDigits(n, base, len)the lenlen least-significant digits, zero-padded if needed.
IntegerDigits(n, system)digits in any of the systems below
SymPydigits(n, system)[1:]Oscarreverse(digits(BigInt(n), base=system))
IntegerDigits(n, system, width)left-padded with zeros to width digits
Typed signatures
TypePackageApplies
(integer, integer?, integer?) -> list<integer>compute-enginealways
(integer, any?, integer?) -> list<integer>numeralsreplaces compute-enginealways

Domain: Numeral systems

Details
  • Digits come out most significant first, matching ordinary positional notation.
  • In a fixed base the sign of n is discarded, so negative integers give the same digits as their absolute value.
  • IntegerDigits(0) is {0}\{0\} -- there's always at least one digit.
  • The 3-argument form keeps only the len least-significant digits, truncating or zero-padding as needed.
  • Systems: PositionalNumerals(b), MixedRadixNumerals([…]), FactorialNumerals, PrimorialNumerals, BalancedNumerals(b), NegativeNumerals(b), BijectiveNumerals(k), ZeckendorfNumerals, OstrowskiNumerals([…]), CombinatorialNumerals(k), ResidueNumerals([…]), AdicNumerals(b, prec?) — also written Radix, MixedRadix, Factoradic, PrimorialRadix, BalancedRadix, NegativeRadix, BijectiveRadix, Zeckendorf, Ostrowski, CombinatorialSystem, ResidueSystem
  • PositionalNumerals(b) is ordinary base-bb notation, b≥2b\ge2 — the same digits an integer base already gives, wrapped as a system value so it can stand wherever the others do (NumeralSystemShape, a constant-radix MixedRadixNumerals comparison)
  • OstrowskiNumerals([a₁, …]) is the numeral system a CONTINUED FRACTION defines: place values are the convergents' denominators, and a digit at its ceiling forbids a non-zero digit below it. All quotients 1 is φ\varphi, and that case IS Zeckendorf
  • BalancedNumerals and NegativeNumerals represent NEGATIVE integers with no sign at all; fixed radix drops the sign instead
  • The factoradic digits of nn are the Lehmer code of the nn-th permutation, so padding to the permutation's size makes the two line up
  • An integer with no numeral in a system — anything past ∏mi\prod m_i in a residue system, say — leaves the call standing rather than answering
  • Bijective bases have no zero DIGIT, but zero itself is the empty numeral: that is what makes the correspondence with strings a bijection
  • Inverted by FromDigits with the same system

Examples

19 more kept as data, checked against the oracles
Basic #
live ↗
InIntegerDigits(1 234)\mathrm{IntegerDigits}(1\,234)Out[1,2,3,4]\bigl\lbrack1, 2, 3, 4\bigr\rbrack
live ↗
InIntegerDigits(2 147,2)\mathrm{IntegerDigits}(2\,147, 2)Out[1,0,0,0,0,1,1,0,0,0,1,1]\bigl\lbrack1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1\bigr\rbrack
live ↗
InIntegerDigits(58 127)\mathrm{IntegerDigits}(58\,127)Out[5,8,1,2,7]\bigl\lbrack5, 8, 1, 2, 7\bigr\rbrack
live ↗
InIntegerDigits(58 127,2)\mathrm{IntegerDigits}(58\,127, 2)Out[1,1,1,0,0,0,1,1,0,0,0,0,1,1,1,1]\bigl\lbrack1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1\bigr\rbrack

93 784 seconds as days, hours, minutes, seconds

live ↗
InIntegerDigits(93 784,MixedRadixNumerals([24,60,60]))\mathrm{IntegerDigits}(93\,784, \mathrm{MixedRadixNumerals}(\bigl\lbrack24, 60, 60\bigr\rbrack))Out[1,2,3,4]\bigl\lbrack1, 2, 3, 4\bigr\rbrack

100=89+8+3100 = 89 + 8 + 3, with no two adjacent ones

live ↗
InIntegerDigits(100,ZeckendorfNumerals)\mathrm{IntegerDigits}(100, \mathrm{ZeckendorfNumerals})Out[1,0,0,0,0,1,0,1,0,0]\bigl\lbrack1, 0, 0, 0, 0, 1, 0, 1, 0, 0\bigr\rbrack

102341 seconds split into days, hours, minutes, seconds

live ↗
InIntegerDigits(102 341,MixedRadixNumerals([24,60,60]))\mathrm{IntegerDigits}(102\,341, \mathrm{MixedRadixNumerals}(\bigl\lbrack24, 60, 60\bigr\rbrack))Out[1,4,25,41]\bigl\lbrack1, 4, 25, 41\bigr\rbrack
Scope #

Digits past 9 are plain integers: 58127=e30f1658127 = \mathrm{e30f}_{16}

live ↗
InIntegerDigits(58 127,16)\mathrm{IntegerDigits}(58\,127, 16)Out[14,3,0,15]\bigl\lbrack14, 3, 0, 15\bigr\rbrack

a mixed radix, spelled MixedRadix: 102 341 seconds is 1d 4h 25m 41s

live ↗
InIntegerDigits(102 341,MixedRadix([24,60,60]))\mathrm{IntegerDigits}(102\,341, \mathrm{MixedRadix}(\bigl\lbrack24, 60, 60\bigr\rbrack))Out[1,4,25,41]\bigl\lbrack1, 4, 25, 41\bigr\rbrack

the leading digit is unbounded: 137=(3⋅3+2)⋅12+5137 = (3 \cdot 3 + 2) \cdot 12 + 5

live ↗
InIntegerDigits(137,MixedRadix([3,12]))\mathrm{IntegerDigits}(137, \mathrm{MixedRadix}(\bigl\lbrack3, 12\bigr\rbrack))Out[3,2,5]\bigl\lbrack3, 2, 5\bigr\rbrack

a width longer than the numeral pads with leading zeros

live ↗
InIntegerDigits(5,2,8)\mathrm{IntegerDigits}(5, 2, 8)Out[0,0,0,0,0,1,0,1]\bigl\lbrack0, 0, 0, 0, 0, 1, 0, 1\bigr\rbrack

Listable in the base: one digit list per base

live ↗
InIntegerDigits(7,[2,3,4])\mathrm{IntegerDigits}(7, \bigl\lbrack2, 3, 4\bigr\rbrack)Out[[1,1,1],[2,1],[1,3]]\bigl\lbrack\bigl\lbrack1, 1, 1\bigr\rbrack, \bigl\lbrack2, 1\bigr\rbrack, \bigl\lbrack1, 3\bigr\rbrack\bigr\rbrack

Listable: threads over a range of integers

live ↗
InIntegerDigits(0..7,2)\mathrm{IntegerDigits}(0..7, 2)Out[[0],[1],[1,0],[1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]]\bigl\lbrack\bigl\lbrack0\bigr\rbrack, \bigl\lbrack1\bigr\rbrack, \bigl\lbrack1, 0\bigr\rbrack, \bigl\lbrack1, 1\bigr\rbrack, \bigl\lbrack1, 0, 0\bigr\rbrack, \bigl\lbrack1, 0, 1\bigr\rbrack, \bigl\lbrack1, 1, 0\bigr\rbrack, \bigl\lbrack1, 1, 1\bigr\rbrack\bigr\rbrack

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

live ↗
InIntegerDigits([6,7,2],2)\mathrm{IntegerDigits}(\bigl\lbrack6, 7, 2\bigr\rbrack, 2)Out[[1,1,0],[1,1,1],[1,0]]\bigl\lbrack\bigl\lbrack1, 1, 0\bigr\rbrack, \bigl\lbrack1, 1, 1\bigr\rbrack, \bigl\lbrack1, 0\bigr\rbrack\bigr\rbrack

ordinary base 2, as a system value — same digits as the native 2-argument form

live ↗
InIntegerDigits(2 147,PositionalNumerals(2))\mathrm{IntegerDigits}(2\,147, \mathrm{PositionalNumerals}(2))Out[1,0,0,0,0,1,1,0,0,0,1,1]\bigl\lbrack1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1\bigr\rbrack

the same 20=13+5+220 = 13 + 5 + 2, over a continued fraction's convergents

live ↗
InIntegerDigits(20,OstrowskiNumerals([1,1,1,1,1,1,1,1]))\mathrm{IntegerDigits}(20, \mathrm{OstrowskiNumerals}(\bigl\lbrack1, 1, 1, 1, 1, 1, 1, 1\bigr\rbrack))Out[0,1,0,1,0,1,0,0]\bigl\lbrack0, 1, 0, 1, 0, 1, 0, 0\bigr\rbrack

a negative integer, with no sign

live ↗
InIntegerDigits(−5,BalancedNumerals(3))\mathrm{IntegerDigits}(-5, \mathrm{BalancedNumerals}(3))Out[−1,1,1]\bigl\lbrack-1, 1, 1\bigr\rbrack

spreadsheet column AAA

live ↗
InIntegerDigits(703,BijectiveNumerals(26))\mathrm{IntegerDigits}(703, \mathrm{BijectiveNumerals}(26))Out[1,1,1]\bigl\lbrack1, 1, 1\bigr\rbrack

independent residues — no place values

live ↗
InIntegerDigits(23,ResidueNumerals([3,5,7]))\mathrm{IntegerDigits}(23, \mathrm{ResidueNumerals}(\bigl\lbrack3, 5, 7\bigr\rbrack))Out[2,3,2]\bigl\lbrack2, 3, 2\bigr\rbrack

the 10-adic truncation: a negative is spelled by its digits, and the width is part of the numeral

live ↗
InIntegerDigits(−3,AdicNumerals(10,6))\mathrm{IntegerDigits}(-3, \mathrm{AdicNumerals}(10, 6))Out[9,9,9,9,9,7]\bigl\lbrack9, 9, 9, 9, 9, 7\bigr\rbrack

A mixed radix with radices [3,12][3,12] expands 137 in three places

live ↗
InIntegerDigits(137,MixedRadixNumerals([3,12]))\mathrm{IntegerDigits}(137, \mathrm{MixedRadixNumerals}(\bigl\lbrack3, 12\bigr\rbrack))Out[3,2,5]\bigl\lbrack3, 2, 5\bigr\rbrack
live ↗
InIntegerDigits(1 084,MixedRadixNumerals([17,15,11]),2)\mathrm{IntegerDigits}(1\,084, \mathrm{MixedRadixNumerals}(\bigl\lbrack17, 15, 11\bigr\rbrack), 2)Out[8,6]\bigl\lbrack8, 6\bigr\rbrack
Applications #

The 3-argument form keeps only the 4 least-significant digits

live ↗
InIntegerDigits(6 345 354,10,4)\mathrm{IntegerDigits}(6\,345\,354, 10, 4)Out[5,3,5,4]\bigl\lbrack5, 3, 5, 4\bigr\rbrack

all 3-bit words, by threading a padded form over a range

live ↗
InIntegerDigits(0..7,2,3)\mathrm{IntegerDigits}(0..7, 2, 3)Out[[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]]\bigl\lbrack\bigl\lbrack0, 0, 0\bigr\rbrack, \bigl\lbrack0, 0, 1\bigr\rbrack, \bigl\lbrack0, 1, 0\bigr\rbrack, \bigl\lbrack0, 1, 1\bigr\rbrack, \bigl\lbrack1, 0, 0\bigr\rbrack, \bigl\lbrack1, 0, 1\bigr\rbrack, \bigl\lbrack1, 1, 0\bigr\rbrack, \bigl\lbrack1, 1, 1\bigr\rbrack\bigr\rbrack

the Lehmer code of the 6th permutation of four things

live ↗
InIntegerDigits(5,FactorialNumerals,4)\mathrm{IntegerDigits}(5, \mathrm{FactorialNumerals}, 4)Out[0,2,1,0]\bigl\lbrack0, 2, 1, 0\bigr\rbrack
Properties #

an integer base is still the native handler

live ↗
InIntegerDigits(255,16)\mathrm{IntegerDigits}(255, 16)Out[15,15]\bigl\lbrack15, 15\bigr\rbrack

Another time-of-day style split, inverse of FromDigits

live ↗
InIntegerDigits(98 534,MixedRadixNumerals([24,60,60]))\mathrm{IntegerDigits}(98\,534, \mathrm{MixedRadixNumerals}(\bigl\lbrack24, 60, 60\bigr\rbrack))Out[1,3,22,14]\bigl\lbrack1, 3, 22, 14\bigr\rbrack
Possible issues #

Unlike IntegerString there's always at least one digit, even for 0

live ↗
InIntegerDigits(0)\mathrm{IntegerDigits}(0)Out[0]\bigl\lbrack0\bigr\rbrack

The sign is discarded

live ↗
InIntegerDigits(−3 134)\mathrm{IntegerDigits}(-3\,134)Out[3,1,3,4]\bigl\lbrack3, 1, 3, 4\bigr\rbrack

no numeral for a negative in a system that spells only n≥0n \ge 0; the message says which integers it does spell

live ↗
InIntegerDigits(−3,FactorialNumerals)\mathrm{IntegerDigits}(-3, \mathrm{FactorialNumerals})OutIntegerDigits(−3,FactorialNumerals)\mathrm{IntegerDigits}(-3, \mathrm{FactorialNumerals})
Neat examples #

100!100! has 158 digits

live ↗
InLength(IntegerDigits(100!))\mathrm{Length}(\mathrm{IntegerDigits}(100!))Out158158
About
Examples50
Checked againstWolfram Language 25 agree1 differSymPy 12 agree1 errorOscar 8 agree
CrosswalkWikipediaMathWorldWikidataWolfram LanguageSymPy ×2Oscar ×2Rosetta Code

Implementation

mappedoscarpackages/oracle/src/mappings.ts
mappedoscarpackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts

SymPy's digits returns the base first, then the digits, so the template drops that leading base; for a negative n the base is negated and the digits are those of |n|. A numeral system in the base slot has no counterpart.

mappedsympypackages/oracle/src/mappings.ts

SymPy's digits returns the base first, then the digits, so the template drops that leading base; for a negative n the base is negated and the digits are those of |n|. A numeral system in the base slot has no counterpart.

See also: FromDigits, NumeralSystemShape, DigitCount, IntegerString