Drill

ProblemsGo › finance

Months to reach a savings target

easyfinanceMathGo

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

monthsToTarget(target: int, monthlySaving: int) → int

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func monthsToTarget(target int, monthlySaving int) int {
	
}

Worked examples

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

Hint

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

Reference solution in Go
func monthsToTarget(target int, monthlySaving int) int {
	if target <= 0 || monthlySaving <= 0 {
		return 0
	}
	return (target + monthlySaving - 1) / monthlySaving
}

The same problem in another language

More finance problems in Go