Foundations
Linear Structures
Hashing
Array and String Patterns
Recursion, Search, and Sorting
Problem Solving Patterns
Algorithm Searching
Sorting Algorithms
Array Algorithms
Grid Algorithms
Linear Structure Algorithms
Hashing Algorithms
Recursion and Backtracking
Greedy Algorithms
Tree and Heap Algorithms
Graph Algorithms
Dynamic Programming Algorithms
String Algorithms
Math Algorithms
Keyboard ready
Space plays. Arrow keys step. R restarts.
LAB Sum of Digits Lab
Math
Problem shape
Beginner
4
8
15
16
23
42
Core idea
Repeatedly take the last digit and divide the number by ten.
Step 1 / 5
Problem shape
Math Beginner
Sum of Digits
Welcome to the Sum of Digits algorithm lab. Repeatedly take the last digit and divide the number by ten. Use it when: Use for number decomposition and digit-processing practice. The time complexity is O(number of digits), and the space complexity is O(1). The pseudocode is: sum = 0 while n > 0 sum += n % 10 n = n / 10 return sum As you practice, track the state that changes and the condition that makes the algorithm stop.
Time
O(number of digits)
Space
O(1)
Pseudocode
sum = 0
while n > 0
sum += n % 10
n = n / 10
return sum 1.0x