본문으로 건너뛰기

digitSum

Calculates the sum of all digits in an integer. Computes the digital root by adding all individual digits of the given integer. For negative numbers, the calculation is performed on the absolute value. This function is useful in number theory, digital root calculations, and checksum algorithms.

Signature​

const digitSum: (value: number) => number

Parameters​

NameTypeDescription
value-Integer to calculate digit sum for (must be an integer)

Returns​

Sum of all digits in the number

Examples​

Basic digit sum calculations​

import { digitSum } from '@winglet/common-utils';

console.log(digitSum(123)); // 6 (1 + 2 + 3)
console.log(digitSum(456)); // 15 (4 + 5 + 6)
console.log(digitSum(1111)); // 4 (1 + 1 + 1 + 1)
console.log(digitSum(0)); // 0
console.log(digitSum(9)); // 9

Negative numbers and large integers​

// Negative numbers use absolute value
console.log(digitSum(-123)); // 6 (same as digitSum(123))
console.log(digitSum(-456)); // 15 (same as digitSum(456))

// Large numbers
console.log(digitSum(1234567890)); // 45 (1+2+3+4+5+6+7+8+9+0)
console.log(digitSum(999999999)); // 81 (9 * 9)
console.log(digitSum(1000000000)); // 1 (1+0+0+0+0+0+0+0+0+0)

Playground​

import { digitSum } from '@winglet/common-utils';

console.log(digitSum(123)); // 6 (1 + 2 + 3)
console.log(digitSum(456)); // 15 (4 + 5 + 6)
console.log(digitSum(1111)); // 4 (1 + 1 + 1 + 1)
console.log(digitSum(0)); // 0
console.log(digitSum(9)); // 9

Notes​

Mathematical Properties:

  • Always returns a non-negative integer
  • For single-digit numbers, returns the number itself
  • For negative numbers, uses absolute value (sign is ignored)
  • Can be used iteratively to find digital root: digitSum(digitSum(n))

Use Cases:

  • Digital root calculations in numerology
  • Checksum algorithms and data validation
  • Number theory and mathematical analysis
  • ISBN/EAN barcode validation algorithms
  • Luhn algorithm for credit card validation
  • Recreational mathematics and puzzles

Performance: O(log₁₀(n)) time complexity where n is the input value. Space complexity is O(1) as it uses iterative approach.