feat(int): add DigitSum builtin#8
Open
msollami wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DigitSum[n]— sum of the base-10 digits of|n|DigitSum[n, b]— sum of the base-bdigits of|n|(b ≥ 2)DigitSum[0]→0; bignum inputs supported via GMPChanges
src/int.{c,h}—builtin_digitsum()implementation (fastulongpath viampz_tdiv_q_ui; slow bignum-base path viampz_tdiv_qr); registered inint_init()with attributesListable | NumericFunction | Protectedsrc/sym_names.{h,c}—SYM_DigitSuminterned symbolsrc/info.c— docstringtests/test_digit_sum.c— 33 tests: basic decimal, zero, negatives, two-arg base form, cross-check vsTotal[IntegerDigits[n]], bignum inputs (10^30,100!,2^100), arity/type diagnostics, symbolic silence,Listablethreading, attributes, docstring, 50-iteration stress looptests/CMakeLists.txt—digit_sum_teststargetdocs/spec/builtins/arithmetic.md—DigitSumsectiondocs/spec/changelog/2026-06-22.md— changelog entryTesting
make USE_ECM=0— clean build, no warningscd tests/build && make digit_sum_tests && ./digit_sum_tests— all 33 tests passDigitSum[1234]→10,DigitSum[255, 16]→30,DigitSum[{1,99}]→{1, 18}Out of scope
DigitSum[n, b, ...]variant)