Home Python Interview Programs Condense a Number Python Condense a Number Program Definition
What Does Condense a Number Mean? To condense a number means repeatedly add its digits until only one digit remains. That final digit is the digital root .
while n has more than one digit:
n = sum of digits of n
Example: 9875
9 + 8 + 7 + 5 = 29
2 + 9 = 11
1 + 1 = 2 ← condensed result n Reduction chain Result 3838 → 11 → 2 2999999 → 27 → 9 97(already one digit) 7
Approach
How to Condense a Number in Python Four clear steps — then pick an iterative loop or the O(1) formula.
Work with a nonnegative value (use abs if needed). While the value is greater than 9, replace it with the sum of its digits. Stop when only one digit remains. Or skip the loops and compute the digital root with n % 9. Method Idea Time Best for Digit-sum loop Sum digits until n <= 9 O(log n)Interviews, showing the process Modulo 9 formula 0 if n == 0 else 1 + (n - 1) % 9O(1)Production code, quick answers
Pseudocode function Condense(n):
n = abs(n)
while n > 9:
sum = 0
while n > 0:
sum = sum + (n mod 10)
n = n / 10
n = sum
return n Reference
Cheat Sheet Keep these facts handy while writing the program.
Topic Detail Goal Reduce to a single digit 0–9 Stop when n <= 9One digit already Return n unchanged Fast formula 0 if n == 0 else 1 + (n - 1) % 9Multiples of 9 For n > 0, digital root is 9 (not 0) Classic check 9875 → 2
Try it
Live Preview Enter any integer to see the reduction chain and the condensed digit.
Trace
Worked Walkthrough — n = 9875 Sum digits, replace n, and repeat until one digit is left.
Step 1: 9 + 8 + 7 + 5 = 29
Step 2: 2 + 9 = 11
Step 3: 1 + 1 = 2
Stop (single digit)
Condensed value = 2
Check with formula:
1 + (9875 - 1) % 9 = 1 + 9874 % 9 = 1 + 1 = 2 Code
Python Programs Start with the iterative digit-sum loop so you can show each reduction. Then use the O(1) modulo formula when speed matters.
1. Digit-sum loop 2. Modulo 9 formula Example 1 — Digit-Sum Loop Keep replacing the number with the sum of its digits until it is a single digit.
def condense(n: int) -> int:
n = abs(n)
while n > 9:
digit_sum = 0
while n > 0:
digit_sum += n % 10
n //= 10
n = digit_sum
return n
number = 9875
print(f"{number} condensed to {condense(number)}") How It Works Take the absolute value so negatives behave like positives. Outer while n > 9 means “keep going until one digit remains.” Inner loop peels digits with n % 10 and builds digit_sum. Assign n = digit_sum and repeat. For 9875: 29, then 11, then 2. Return the final single digit. Example 2 — Modulo 9 Formula Use the base-10 identity: the digital root equals 1 + (n - 1) % 9 for n > 0.
def condense_fast(n: int) -> int:
n = abs(n)
if n == 0:
return 0
return 1 + (n - 1) % 9
print(condense_fast(9875)) # 2
print(condense_fast(999)) # 9
print(condense_fast(0)) # 0 How It Works 0 is a special case — its digital root is 0.For any positive n, (n - 1) % 9 maps multiples of 9 to the right slot. Adding 1 yields digits 1–9. That is why 999 becomes 9, not 0. Same answers as the loop, but in constant time with no digit scanning. Use the loop in interviews when you must show the reduction; use this formula in real code. Edge Cases & Pitfalls Check these before calling the solution done.
0–9 Already condensed Return the value as-is. No loop body should run.
Multiples of 9 Root is 9 For n > 0, do not return 0 from a raw n % 9.
Negatives Use abs() Digital root is usually defined for nonnegative integers.
One pass Keep repeating A single digit sum is not enough when the sum itself has two digits.
Analysis
Time and Space Complexity Program Time Extra space Digit-sum loop O(log n)O(1)Modulo 9 formula O(1)O(1)
Each digit-sum pass costs O(digits). Few passes are needed because the sum shrinks quickly.
Remember
Key Takeaways Definition: keep summing digits until one digit remains (digital root).
Loop: peel digits with % 10 while n > 9.
Formula: 1 + (n - 1) % 9 for n > 0.
Test set: 9875 → 2; 999 → 9; 7 → 7; 0 → 0.
One line: repeatedly sum digits — or jump straight to the digital root with modulo 9.
Frequently Asked Questions What does condense a number mean? It means repeatedly adding the digits of a number until only one digit remains. That final digit is called the digital root.
How is it different from a one-time digit sum? A digit sum is one pass. Condensing keeps summing until the result is a single digit from 0 to 9.
What is the fast formula? For n > 0: if n % 9 == 0 then 9, else n % 9. For n == 0 the answer is 0. Equivalently: 1 + (n - 1) % 9 for n > 0.
Why does modulo 9 work? In base 10, a number and the sum of its digits are congruent modulo 9, so repeated digit sums preserve that congruence.
What is the time complexity? The iterative method is O(log n) per reduction step and needs only a few steps. The closed-form mod-9 method is O(1).
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion