Tutorialmath
Sum of Digits
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given a non-negative integer n, return the sum of its decimal digits. For n = 1234 that is
1 + 2 + 3 + 4 = 10.
Examples
| n | result |
|---|---|
1234 | 10 |
0 | 0 |
909 | 18 |
Example 1 — 1 + 2 + 3 + 4 = 10.
Example 2 — The digit sum of 0 is 0.
Example 3 — 9 + 0 + 9 = 18.
This is a tutorial problem: there are no performance tests, just return the correct value.
Function signature
solution(n: int) → int
Constraints
-
n
Expected complexity
- Time
- O(log N)
- Space
- O(1)
Examples
| n | Expected |
|---|---|
1234 | 10 |
0 | 0 |
909 | 18 |
Hint
Hint
n % 10 is the last digit and n / 10 (n // 10 in Python) drops it. Repeat "add the last digit, divide by 10"
until n becomes 0.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1200 ms | estimated |
| C# 14 (.NET 10) | 1200 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 100 | 12 | verdict only |