Fact-checked by Grok 2 weeks ago

Digit sum

The digit sum of a in a given , typically 10 for , is the sum of all its digits. For instance, the digit sum of 9045 is 9 + 0 + 4 + 5 = 18. In , the digit sum exhibits key properties, notably that it is to the original number 9, meaning s(n) \equiv n \pmod{9}, except when n is a multiple of 9, in which case both are congruent to 0 9. This congruence arises because powers of 10 are also congruent to 1 9, allowing the digit sum to preserve the number's residue class. Repeated application of the digit sum process yields the , a single-digit value equivalent to n \mod 9 (or 9 if the remainder is 0, for n > 0). The thus provides a recursive reduction to a value between 1 and 9, with applications in simplifying arithmetic and . Digit sums find practical use in divisibility rules, such as testing for factors of 3 or 9 by checking if the sum is divisible by those numbers, a method known as "." In and , variants appear in algorithms like the , employed for credit card numbers, where digit sums (or their equivalents after doubling) help detect transcription errors by ensuring the total sum is divisible by 10. These techniques extend to error detection in identifiers like ISBNs and barcodes, enhancing reliability in numerical data processing. More advanced research explores summatory functions of digit sums and their inequalities in .

Definition and Fundamentals

For non-negative integers

The digit sum of a non-negative n in base 10, often denoted s(n) or \operatorname{digsum}(n), is defined as the sum of all its digits. This includes the case n = [0](/page/0), where s([0](/page/0)) = [0](/page/0). For instance, the digits of 84001 are 8, 4, , 0, and 1, so s(84001) = 8 + 4 + [0](/page/0) + [0](/page/0) + 1 = 13. The digits themselves can be explicitly extracted using the formula d_i = \left\lfloor \frac{n}{10^i} \right\rfloor \mod 10 for each position i = 0, 1, \dots, \left\lfloor \log_{10} n \right\rfloor, where d_0 is the units digit and higher i correspond to higher place values; the digit sum is then s(n) = \sum_i d_i. Alternatively, s(n) admits a recursive definition: s([0](/page/0)) = [0](/page/0), and for n \geq 1, s(n) = (n \mod 10) + s\left( \left\lfloor \frac{n}{10} \right\rfloor \right), which corresponds to adding the least significant digit and recursing on the remaining number. This recursive relation is equivalently expressed as s(10k + d) = s(k) + d for digits d = [0](/page/0) to $9 and k \geq [0](/page/0). The sequence of base-10 digit sums for successive non-negative n = 0, 1, 2, \dots begins 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \dots and is cataloged as OEIS A007953.

In general bases

In a general b > 1, the digit sum of a non-negative n, denoted F_b(n), is the sum of its digits in the base-b positional . The base-b representation of n is given by n = \sum_{i=0}^k d_i b^i, where k = \lfloor \log_b n \rfloor and each digit satisfies $0 \leq d_i < b. Thus, F_b(n) = \sum_{i=0}^k d_i. The i-th digit can be computed as d_i = \frac{n \mod b^{i+1} - n \mod b^i}{b^i}. For different bases, the cumulative digit sums up to n exhibit a comparison property: if $2 \leq b_1 < b_2, then \sum_{k=0}^n F_{b_1}(k) < \sum_{k=0}^n F_{b_2}(k) for sufficiently large n. This follows from the asymptotic behavior of the summatory function S_b(n) = \sum_{k=1}^n F_b(k) \sim c_b n \log n, where c_b = \frac{b-1}{2 \log b} is increasing in b. A notable example occurs in base 2, where the digit sum F_2(n) equals the number of 1s in the binary expansion of n, known as the . For instance, $13_{10} = 1101_2, so F_2(13) = 3. Generating functions for the sums of digit sums in general bases have been explored, providing tools for analytic expressions and approximations, as studied by Borwein and Borwein (1992).

Generalizations and Extensions

To negative integers

The digit sum for negative integers extends the concept from non-negative integers by utilizing signed-digit representations, in which individual digits may take negative values alongside non-negative ones. This allows negative numbers to be expressed without a separate sign prefix, with the digit sum computed as the algebraic sum of these signed digits. Such representations are particularly useful in systems like or (CSD) formats, where digits range from -1 to 1, enabling efficient arithmetic operations on both positive and negative values. In balanced ternary (base 3 with digits -1, 0, 1), every integer, including negatives, has a unique representation, and the digit sum directly incorporates the signed values. For example, -2 is represented as T1 (where T denotes -1), corresponding to (-1) × 3¹ + 1 × 3⁰ = -3 + 1 = -2, with digit sum -1 + 1 = 0. To obtain the representation of a negative number, one inverts the digits of the positive counterpart (replacing 1 with -1 and vice versa, leaving 0 unchanged). This system avoids the need for explicit sign handling and maintains properties like modular congruences when summing digits. For general bases, such as base 10, signed-digit systems allow digits typically from -(b-1)/2 to (b+1)/2 or wider ranges (e.g., -9 to 9), introducing redundancy that results in non-unique representations for the same integer. The digit sum thus depends on the chosen representation, posing a challenge compared to the unique sums for positive integers in standard positional notation. One common approach is to use the signed digits of the absolute value, yielding -123 as -1 × 10² + (-2) × 10¹ + (-3) × 10⁰, with digit sum -1 + (-2) + (-3) = -6. However, an alternative representation, such as -2 × 10² + 8 × 10¹ + (-3) × 10⁰ = -200 + 80 - 3 = -123, gives digit sum -2 + 8 + (-3) = 3. To mitigate non-uniqueness, variants prioritize representations with minimal absolute digit sum or other optimization criteria, often for computational efficiency in arithmetic.

Iterated sums and digital root

The iterated digit sum of a non-negative integer n, often denoted s^k(n), is the result of applying the digit sum function s repeatedly k times until a single-digit number is obtained. This process begins with the sum of the digits of n, then takes the sum of those digits if more than one, and continues iteratively. The final single-digit outcome is known as the , denoted \mathrm{dr}(n). For n > 0, the digital root ranges from 1 to 9; specifically, \mathrm{dr}(n) = n \mod 9 if n \mod 9 \neq 0, and \mathrm{dr}(n) = 9 if n \mod 9 = 0. An equivalent formula is \mathrm{dr}(n) = 1 + (n - 1) \mod 9, which avoids the special case for multiples of 9. For n = 0, \mathrm{dr}(n) = 0. This iterative approach differs from a single digit sum, as the latter may yield a multi-digit result, whereas the always terminates at a single digit through repeated . For example, consider n = 84001: the initial digit sum is s(84001) = 8 + 4 + 0 + 0 + 1 = [13](/page/13), and the next gives s([13](/page/13)) = 1 + 3 = 4, so \mathrm{dr}(84001) = 4. The process is well-defined for any non-negative and converges quickly, typically in a number of steps logarithmic in the size of n. The concept of the digital root traces back to ancient arithmetic practices, notably the method of , which has been used for centuries to verify computations by reducing numbers to their digital roots. This technique, rooted in modular properties, predates modern notation and appears in historical texts on error-checking in multiplication and addition.

Mathematical Properties

Modular arithmetic relations

The digit sum of a non-negative n in base 10, denoted s_{10}(n), satisfies the s_{10}(n) \equiv n \pmod{9}. This follows from the fact that $10 \equiv 1 \pmod{9}, so each $10^i \equiv 1 \pmod{9} for i \geq 0. If n = \sum_{i=0}^k d_i 10^i where $0 \leq d_i \leq 9, then n \equiv \sum_{i=0}^k d_i \cdot 1 \equiv s_{10}(n) \pmod{9}. This relation generalizes to arbitrary bases. For a non-negative n in base b \geq 2, the digit sum s_b(n) satisfies s_b(n) \equiv n \pmod{b-1}. The proof proceeds by : b^0 = 1 \equiv 1 \pmod{b-1}, and assuming b^j \equiv 1 \pmod{b-1}, then b^{j+1} = b \cdot b^j \equiv 1 \cdot 1 = 1 \pmod{b-1} since b \equiv 1 \pmod{b-1}. Thus, if n = \sum_{i=0}^k d_i b^i with $0 \leq d_i < b, it follows that n \equiv \sum_{i=0}^k d_i \equiv s_b(n) \pmod{b-1}. The congruence extends to negative integers using a signed-digit representation, where the digit sum s_{10}(n) includes the sign of n. For any integer n with decimal representation \pm d_k d_{k-1} \cdots d_1 d_0, we have n = \pm \sum_{i=0}^k d_i 10^i, so n \equiv \pm \sum_{i=0}^k d_i \pmod{9} since $10^i \equiv 1 \pmod{9}, and defining s_{10}(n) = \pm \sum_{i=0}^k d_i yields s_{10}(n) \equiv n \pmod{9}. Repeated application of the digit sum preserves the congruence: if s_{10}^m(n) denotes the m-fold iteration, then s_{10}^m(n) \equiv n \pmod{9} for all m \geq 1. This holds because each iteration maps to a value congruent to the previous modulo 9, eventually yielding the digital root as the nonzero residue of n modulo 9 (or 9 if n \equiv 0 \pmod{9}).

Combinatorial counts

The number of n-digit numbers in base 10 with digit sum equal to q, where 1 ≤ q ≤ 9n, can be determined using a recursive formula that accounts for the constraint against leading zeros. Let f(n, q) denote this count. The base case is f(1, q) = 1 for 1 ≤ q ≤ 9 and 0 otherwise. For n > 1 and 0 ≤ q ≤ 9n, f(n, q) = \sum_{i=\max(q-9, 0)}^{q} f(n-1, i), but adjusted for the first digit ranging from 1 to 9 by computing the sum over possible first digits d from 1 to \min(9, q) of the count for the remaining n-1 digits summing to q - d (where the remaining digits allow 0). This recursive structure arises from dynamic programming approaches to bounded sums. An equivalent combinatorial interpretation is the number of integer solutions to d_1 + d_2 + \dots + d_n = q where 1 ≤ d_1 ≤ 9 and 0 ≤ d_i ≤ 9 for 2 ≤ i ≤ n. This count is the coefficient of x^q in the generating function \left( \sum_{k=1}^{9} x^k \right) \left( \sum_{k=0}^{9} x^k \right)^{n-1} = x \frac{1 - x^9}{1 - x} \left( \frac{1 - x^{10}}{1 - x} \right)^{n-1}. Generating functions of this form are standard tools for enumerating restricted compositions or partitions with upper bounds on parts. The distribution exhibits : f(n, q) = f(n, 9n - q + 1) for q > \lceil (9n + 1)/2 \rceil. This follows from the that maps a number with digits (d_1, d_2, \dots, d_n) to (10 - d_1, 9 - d_2, \dots, 9 - d_n), which preserves the no-leading-zero condition (since 10 - d_1 ranges from 1 to 9 if d_1 does) and transforms the digit sum s to 9(n-1) + 10 - s = 9n + 1 - s. This pairs sets with sums q and 9n + 1 - q, establishing the equality. For example, the number of 2-digit numbers with digit sum 5 is f(2, 5) = 5, corresponding to , , , , and 50. If leading zeros were allowed (treating sequences as padded representations), the count would be 6, including 05, but this is excluded for strict n-digit numbers.

Applications

Divisibility and validation

A number is divisible by 3 the of its digits is divisible by 3. Similarly, a number is divisible by 9 the of its digits (or the of that ) is divisible by 9. These rules stem from the of a number and its digit 9 (and thus 3, as 3 divides 9). The "casting out nines" method, a historical error-checking technique dating back to at least the 15th century, leverages this modulo 9 equivalence to verify arithmetic operations like addition and multiplication. By replacing numbers with their digit sums (iterated until a single digit) before and after computation, one checks if the results match modulo 9; discrepancies indicate errors. This approach was documented in early printed mathematics texts, such as the 1478 Treviso Arithmetic, and provided a simple way to detect mistakes in hand calculations. Digit sums form the basis of various algorithms for in identification systems. In the International Standard Book Number (ISBN-10) system, the check digit is computed as a weighted sum of the first nine digits 11, ensuring detection of transcription errors. For ISBN-13, an alternating weighted sum (weights 1 and 3) of the first 12 digits is taken 10 to derive the check digit. The , used for numbers, employs a similar process: digits are doubled every second position from the right (reducing doubled values by summing their digits if over 9), then all results are summed 10; a valid card yields 0. This detects common errors like single-digit mistakes or transpositions with high reliability. In early computing, digit sums served as rudimentary checksums to validate arithmetic results and data transmission, often as sums of bytes or digits appended to outputs for integrity checks. For example, to check if 123456 is divisible by 9, compute the digit sum: 1+2+3+4+5+6=21, then 2+1=3; since 3 is neither 0 nor 9 (divisible by 9), the number is not.

Special numbers and algorithms

, also known as Niven numbers, are positive integers that are divisible by the sum of their own digits in a given base, typically base 10. For example, 18 is a Harshad number because the sum of its digits is 1 + 8 = 9, and 18 ÷ 9 = 2, an integer. This property was first explored by in 1955, who termed such numbers "multidigital," and later popularized as Harshad numbers, derived from the for "great joy." Smith numbers represent another class defined through digit sums, specifically composite numbers where the sum of the digits equals the sum of the digits in the prime of the number, excluding 1 and considering multiplicities. For instance, 4 = 2 × 2 is a Smith number since the digit sum of 4 is 4, and the digit sum of its prime factors is 2 + 2 = 4. Named after mathematician Albert Wilansky, who introduced the concept in 1982, Smith numbers exclude primes as they trivially satisfy the condition. Computing the digit sum efficiently is fundamental to identifying such numbers and has applications in algorithms. The standard approach uses an iterative process based on and operations, achieving O(log ) due to the logarithmic number of digits in . This method repeatedly extracts the last with n % 10, adds it to a running , and removes it via n //= 10 until reaches 0. An alternative involves converting the to a and summing the values, which is equally efficient for typical sizes but more flexible for varying bases. Here is pseudocode for the iterative modulo-based :
def digit_sum(n):
    if n < 0:
        n = -n  # Handle absolute value if needed
    total = 0
    while n > 0:
        total += n % 10
        n //= 10
    return total
This is widely used in for its simplicity and performance on large integers.

Other uses

In , the digit sum in base 2, known as the or population count, represents the number of 1 bits in a string and finds extensive use in for designing error-correcting codes, where the minimum Hamming weight determines the code's ability to detect and correct errors. For instance, in Hamming codes, the weight helps ensure single-error correction by maintaining a minimum distance related to the weight of nonzero codewords. In , Hamming weight supports error detection mechanisms, such as checks, where the weight modulo 2 verifies during transmission; for example, the string 1101₂ has a Hamming weight of 3, indicating an odd parity that could flag errors in even-parity schemes. Beyond error handling, population count is integral to engines employing bitboard representations, where it efficiently tallies the number of pieces of a specific type on the board or computes attack masks by counting set bits in 64-bit integers corresponding to the . This operation accelerates move generation and , as CPUs provide dedicated instructions like POPCNT for rapid . In statistics and early , proposed in 1888 using sums of extracted randomly from logarithm tables to approximate Gaussian distributions, forming a to generate pseudo-random variates by aggregating 50 such digits for better uniformity and normality. This approach leveraged the assumed randomness of table digits to simulate continuous distributions before computational methods dominated. sums also appear in through puzzles that challenge solvers to arrange digits to achieve target sums or explore properties like minimal numbers with given digit totals, fostering logical deduction and . In , binary population counts facilitate efficient simulations of models like the for , where popcount instructions tally aligned spins in bit-packed representations to compute energies in large-scale parallel runs.

References

  1. [1]
    Digit Sum -- from Wolfram MathWorld
    A digit sum s_b(n) is a sum of the base- b digits of n. The base-10 digit sum of the integer n is implemented in the Wolfram Language as DigitSum[n].Missing: definition | Show results with:definition
  2. [2]
    What does the sum of digits refer to in number theory? - CK-12
    In number theory, the sum of digits of a number refers to the arithmetic sum of its digits. For example, if you have a number 123, the sum of its digits would ...
  3. [3]
    [PDF] Some more fun problems 1. (a) Prove that any number is congruent ...
    (a) Prove that any number is congruent to the sum of its digits modulo 9. Hint: consider a two-digit number ab first, and use the definition of congruence.
  4. [4]
    Casting Out Nines: Why It Works - The Math Doctors
    Apr 19, 2024 · (In advanced terms, the digit sum is congruent to the original number modulo 9). When numbers are added or multiplied, the remainder of the ...
  5. [5]
    Digital Root -- from Wolfram MathWorld
    The digital root of a number is the digit obtained after repeatedly summing its digits until a single digit remains. For example, 9876 has a digital root of 3.Missing: definition | Show results with:definition
  6. [6]
    The Math Trick Hidden in Your Credit Card Number
    Aug 12, 2025 · How does Luhn's algorithm know when your fingers fumble? Every digit in a credit card number contributes a one-digit number to the final sum in ...
  7. [7]
    Credit Card Data Formats and the Luhn Algorithm | Ground Labs
    The final digits of a credit card number is a check digit, akin to a checksum. The Luhn algorithm, also known as a Mod 10 calculation, can be used to validate ...
  8. [8]
    Summing the sum of digits - arXiv
    We revisit and generalize inequalities for the summatory function of the sum of digits in a given integer base. We prove that several known results can be ...<|control11|><|separator|>
  9. [9]
    A007953 - OEIS
    A007953. Digital sum (i.e., sum of digits) of n; also called digsum(n). 1190. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, ...
  10. [10]
    [PDF] ON ZECKENDORF AND BASE b DIGIT SUMS
    asymptotics of the average sum of digits of integers in base b. 1. Introduction. The purpose of this note is to present a self-contained, elementary and ...
  11. [11]
    Strange Series and High Precision Fraud - jstor
    These sums break into four types. Sums 2, 3, 4, 5, and 6 are all specializations of generating functions for digit sums, more-or-less of the type: 00 00. (1 ...
  12. [12]
    Canonic Signed Digit (CSD) Representation of Integers
    Feb 18, 2017 · Negative integers. To implement a negative integer, just compute the CSD value of the positive integer and invert the sign of each digit.
  13. [13]
  14. [14]
    Balanced Ternary - Algorithms for Competitive Programming
    Apr 16, 2023 · This system allows you to write negative values without leading minus sign: you can simply invert digits in any positive number. -1 Z -2 Z1 ...
  15. [15]
    [PDF] Hybrid Signed–Digit Number Systems - UMBC CSEE
    The well known signed–digit (SD) number representation makes it possible to perform addition with carry propagation chains that are limited to a single digit ...Missing: integers | Show results with:integers
  16. [16]
    Digital root | Brilliant Math & Science Wiki
    The digital root or digital sum of a non-negative integer is the single-digit value obtained by an iterative process of summing digits.
  17. [17]
    digital root - PlanetMath
    Mar 22, 2013 · The digital root of an integer of the form n(b−1) n ⁢ ( b - 1 ) is always b−1 b - 1 . Another way to calculate the digital root for m>b ...
  18. [18]
    Casting Out Nines: An Explanation and Extensions in - NCTM
    Nov 1, 1990 · The method of casting out 9s has been used for centuries, perhaps even as long as a millenium, for checking computations with integers ...Missing: ancient | Show results with:ancient
  19. [19]
    3.1 Congruence
    +d1+d0(mod9). This actually proves more than we need. It says that an integer and the sum of its digits are congruent modulo 9. In particular, one is congruent ...
  20. [20]
    [PDF] Number Bases and Modular Arithmetic - University College Dublin
    Jan 21, 2023 · The sum of the digits of a number n in base b is a multiple of b − 1 if and only if n is a multiple of b − 1. This works because any power of b ...
  21. [21]
    [PDF] Elementary Number Theory
    Jul 8, 2021 · Every integer is congruent modulo 9 to the sum of its digits. Proof. Let n be an integer with decimal representation. ±dkdk−1dk−2 ···d1d0 ...
  22. [22]
    Count of n digit numbers whose sum of digits equals to given sum
    Jul 23, 2025 · To count all n-digit numbers whose sum of digits equals a given target sum, we iterate through each number within the range of n-digit numbers.
  23. [23]
    [PDF] Divisibility Rules
    Add up all the digits in the number. Find out what the sum is. If the sum is divisible by 9, so is the number. E.g., 43785 (4+3+7+8+5=27) 27 is divisible by 9, ...
  24. [24]
    [PDF] Stupid Divisibility Tricks 1 Introduction
    Many of us learn “the rule of three” in childhood: a number is divisible by 3 if and only if the sum of its digits is divisible by 3. The Babylonians knew that ...
  25. [25]
    Casting Out Nines: An Explanation and Extensions - jstor
    Nov 2, 1990 · A recent interest is in how the history of the development of mathematical concepts can be used to enhance students' understanding of, and ...Missing: ancient | Show results with:ancient<|control11|><|separator|>
  26. [26]
    Interlude II: Checking, Check Digits, and Casting Out Nines
    And casting out nines is of historical interest: it was included in the first-ever mathematics book to be printed in the West, the so-called Treviso Arithmetic, ...
  27. [27]
    Secrets of the ISBN - An Error Detection Method
    Both the simple checksum and the ISBN method involve adding only one additional digit, yet the ISBN method is more effective. The use of a prime modulus is ...
  28. [28]
    ISBNs and checksum digits - FutureLearn
    The check digit (which is the 13th digit) should be the difference between the sum from the second step and the closest multiple of 10 that is larger or equal ...
  29. [29]
    22C:169 Notes, Lecture 23 - University of Iowa
    A common very simple scheme was to use the arithmetic sum of all the bytes in the message as a check at the end of the message. This is called a checksum, and ...Missing: early | Show results with:early
  30. [30]
    Harshad Number -- from Wolfram MathWorld
    A positive integer which is divisible by the sum of its digits, also called a Niven number (Kennedy et al. 1980) or a multidigital number (Kaprekar 1955).
  31. [31]
    Smith Number -- from Wolfram MathWorld
    A Smith number is a composite number the sum of whose digits is the sum of the digits of its prime factors (excluding 1).
  32. [32]
    Sum of Digits of a Number - GeeksforGeeks
    Jul 14, 2025 · We can sum the digits of a number by repeatedly extracting the last digit using n % 10 , adding it to the sum, and then removing it by dividing n by 10 using ...
  33. [33]
    Generalized Hamming weights for linear codes - IEEE Xplore
    Generalized Hamming weights are higher-dimensional weights obtained by viewing minimum Hamming weight as a property of subcodes, characterizing code ...
  34. [34]
    [PDF] Graph Theoretic Methods in Coding Theory
    A constant-weight code is a code where all the codewords have the same Hamming weight. The Hamming distance d(c, c0) between two codewords c and c0 is the ...
  35. [35]
    [PDF] Hamming Weight Attacks on Cryptographic Hardware
    Based on this phenomenon, they propose a Hamming- weight attack on DES-chips (and similar ciphers) with the aim to derive a secret key stored inside the device.
  36. [36]
    Population Count - Chessprogramming wiki
    Population count, also called Hamming weight, determines the number of one bits in a bitboard, used to evaluate piece mobility in chess.
  37. [37]
    The Mathematical Theory of Banking - jstor
    diagram represents the grouping of forty-eight numbers, each formed by taking at random five digits and adding them together. The d'egrees of the horizontal ...
  38. [38]
    Digit Sum Puzzle - NRICH - Millennium Mathematics Project
    Problem. Let N be the smallest positive integer whose digits add up to 2015. What is the sum of the digits of N + 1 ? This problem is taken from the UKMT ...Missing: recreational | Show results with:recreational
  39. [39]
    World records in the size of simulated Ising models - SciELO
    The line INFO = BARRIER() synchronizes the processors: computation proceeds after all nodes have reached this line. POPCNT counts the number of set bits in a ...