Skip to content

feat(int): add DigitSum builtin#8

Open
msollami wants to merge 1 commit into
stblake:mainfrom
msollami:feat/digit-sum
Open

feat(int): add DigitSum builtin#8
msollami wants to merge 1 commit into
stblake:mainfrom
msollami:feat/digit-sum

Conversation

@msollami

Copy link
Copy Markdown
Contributor

Summary

  • Adds DigitSum[n] — sum of the base-10 digits of |n|
  • Adds DigitSum[n, b] — sum of the base-b digits of |n| (b ≥ 2)
  • Sign discarded; DigitSum[0]0; bignum inputs supported via GMP

Changes

  • src/int.{c,h}builtin_digitsum() implementation (fast ulong path via mpz_tdiv_q_ui; slow bignum-base path via mpz_tdiv_qr); registered in int_init() with attributes Listable | NumericFunction | Protected
  • src/sym_names.{h,c}SYM_DigitSum interned symbol
  • src/info.c — docstring
  • tests/test_digit_sum.c — 33 tests: basic decimal, zero, negatives, two-arg base form, cross-check vs Total[IntegerDigits[n]], bignum inputs (10^30, 100!, 2^100), arity/type diagnostics, symbolic silence, Listable threading, attributes, docstring, 50-iteration stress loop
  • tests/CMakeLists.txtdigit_sum_tests target
  • docs/spec/builtins/arithmetic.mdDigitSum section
  • docs/spec/changelog/2026-06-22.md — changelog entry

Testing

  • make USE_ECM=0 — clean build, no warnings
  • cd tests/build && make digit_sum_tests && ./digit_sum_tests — all 33 tests pass
  • REPL: DigitSum[1234]10, DigitSum[255, 16]30, DigitSum[{1,99}]{1, 18}

Out of scope

  • No 3-arg form (Mathematica has no DigitSum[n, b, ...] variant)
  • No symbolic simplification rules

DigitSum[n] returns the sum of the base-10 digits of |n|.
DigitSum[n, b] returns the sum of the base-b digits of |n|.

Sign is discarded; DigitSum[0] -> 0. Both machine integers and
arbitrary-precision bignums are supported via GMP (fast ulong path
via mpz_tdiv_q_ui; slow bignum-base path via mpz_tdiv_qr).

Attributes: Listable | NumericFunction | Protected.

- src/int.{c,h}: implementation, declaration, registration in int_init()
- src/sym_names.{h,c}: SYM_DigitSum interned symbol
- src/info.c: docstring
- tests/test_digit_sum.c: 33 tests covering basic decimal, zero,
  negatives, two-arg base, cross-check vs Total[IntegerDigits[n]],
  bignum inputs, arity/type diagnostics, symbolic silence, Listable
  threading, attributes, docstring, and a 50-iteration stress loop
- tests/CMakeLists.txt: digit_sum_tests target
- docs/spec/builtins/arithmetic.md: DigitSum section
- docs/spec/changelog/2026-06-22.md: changelog entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant