Broken Calculator in Python

A broken calculator problem involves finding the minimum operations needed to transform one number into another using only two allowed operations: double (multiply by 2) and decrement (subtract 1).

Given an initial number X displayed on the calculator, we need to reach the target number Y using the minimum number of operations.

Problem Understanding

The calculator supports only two operations:

  • Double − Multiply the current number by 2

  • Decrement − Subtract 1 from the current number

For example, if X = 5 and Y = 8, we can reach 8 from 5 in 2 operations: decrement once (5 ? 4), then double (4 ? 8).

Algorithm Approach

Instead of working forward from X to Y, we work backward from Y to X using the reverse operations:

  • If Y is even, divide by 2 (reverse of double)

  • If Y is odd, add 1 then divide by 2 (reverse of decrement then double)

Implementation

class Solution:
    def brokenCalc(self, X, Y):
        operations = 0
        
        # Work backward from Y to X
        while Y > X:
            operations += Y % 2 + 1
            Y = Y // 2 if Y % 2 == 0 else (Y + 1) // 2
        
        # If Y <= X, we need (X - Y) decrements
        return operations + X - Y

# Test the solution
solution = Solution()
result = solution.brokenCalc(5, 8)
print(f"Minimum operations needed: {result}")
Minimum operations needed: 2

Step-by-Step Example

Let's trace through the algorithm with X = 5 and Y = 8:

def broken_calc_with_steps(X, Y):
    operations = 0
    original_Y = Y
    
    print(f"Starting: X = {X}, Y = {Y}")
    
    while Y > X:
        if Y % 2 == 0:
            print(f"Y = {Y} is even, divide by 2")
            operations += 1
            Y = Y // 2
        else:
            print(f"Y = {Y} is odd, add 1 then divide by 2")
            operations += 2
            Y = (Y + 1) // 2
        
        print(f"Operations so far: {operations}, Y now: {Y}")
    
    final_ops = operations + X - Y
    print(f"Final: {final_ops} operations needed")
    return final_ops

# Test with example
broken_calc_with_steps(5, 8)
Starting: X = 5, Y = 8
Y = 8 is even, divide by 2
Operations so far: 1, Y now: 4
Final: 2 operations needed

Why This Algorithm Works

Working backward is more efficient because:

  • When Y is even, we can always divide by 2 (reverse of doubling)

  • When Y is odd, we must increment first to make it even, then divide

  • This greedy approach always finds the minimum operations

Additional Test Cases

solution = Solution()

test_cases = [
    (2, 3),   # Expected: 2 (double to 4, then decrement to 3)
    (5, 8),   # Expected: 2 (decrement to 4, then double to 8)
    (3, 10),  # Expected: 3
    (1024, 1) # Expected: 1023 (all decrements)
]

for x, y in test_cases:
    result = solution.brokenCalc(x, y)
    print(f"X = {x}, Y = {y} ? {result} operations")
X = 2, Y = 3 ? 2 operations
X = 5, Y = 8 ? 2 operations
X = 3, Y = 10 ? 3 operations
X = 1024, Y = 1 ? 1023 operations

Conclusion

The broken calculator problem is solved efficiently by working backward from the target to the start. This greedy approach using reverse operations guarantees the minimum number of steps needed.

Updated on: 2026-03-25T08:19:39+05:30

420 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements