Skip to content

fix(maths): correct recursive call in sum_of_digits_recursion#14768

Open
fauzan171 wants to merge 1 commit into
TheAlgorithms:masterfrom
fauzan171:fix/sum-of-digits-recursion-bug
Open

fix(maths): correct recursive call in sum_of_digits_recursion#14768
fauzan171 wants to merge 1 commit into
TheAlgorithms:masterfrom
fauzan171:fix/sum-of-digits-recursion-bug

Conversation

@fauzan171
Copy link
Copy Markdown

Summary

Fix a bug where sum_of_digits_recursion() was calling sum_of_digits() (the iterative version) instead of itself in the recursive case.

Bug

In maths/sum_of_digits.py line 34:

# Before (bug): calls the iterative function, not itself
return n if n < 10 else n % 10 + sum_of_digits(n // 10)

# After (fix): properly calls itself recursively
return n if n < 10 else n % 10 + sum_of_digits_recursion(n // 10)

Impact

  • The function was not actually recursive as its name and documentation suggest
  • The result was correct (it fell back to the iterative version), but the intent was wrong
  • This is a correctness fix for the algorithm implementation

Checklist

  • All doctests pass
  • No behavioral change in output (the function still produces correct results)

sum_of_digits_recursion() was calling sum_of_digits() (the iterative version) instead of itself in the recursive case. This meant the function was not actually recursive as its name suggests.
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant