Drill

ProblemsPython › finance

Months to reach a savings target

easyfinancePython

A plan asks how many whole months of flat saving are needed to top up a target balance.

months_to_target(target: int, monthly_saving: int) → int

Solve it in the editor →

Where you start

def months_to_target(target: int, monthly_saving: int) -> int:
    

Worked examples

CallResult
months_to_target(1000, 300)4
months_to_target(1000, 500)2
months_to_target(1000, 1000)1
months_to_target(0, 500)0

Hint

Ceil(target / saving) via (target + saving - 1) / saving.

Reference solution in Python
def months_to_target(target: int, monthly_saving: int) -> int:
    if target <= 0 or monthly_saving <= 0:
        return 0
    return (target + monthly_saving - 1) // monthly_saving

The same problem in another language

More finance problems in Python