Problem 116336 · medium · Phase 01 Prerequisites & Setup

Months to Pay Off a Loan

loops · while · simulation · percent

You owe balance coins. Every month, in this order: interest of balance * rate // 100 is added (rounded down), then you pay payment coins. Write months_to_payoff(balance, rate, payment) that returns how many months it takes for the balance to drop to 0 or below. If the balance is already 0 or less, return 0.

If after some month the balance is not smaller than it was at the start of that month, the loan will never be paid off: return -1.

Examples

Input:  balance = 1000, rate = 1, payment = 100
Output: 11
Explanation: 1000 -> 910 -> 819 -> 727 -> 634 -> 540 -> 445 -> 349 -> 252 -> 154 -> 55 -> -45

Input:  balance = 1000, rate = 10, payment = 100
Output: -1
Explanation: 1000 + 100 interest - 100 payment = 1000; nothing changes.

Constraints

  • 0 <= balance <= 10**6
  • 0 <= rate <= 100
  • 1 <= payment <= 10**6

Goals

  • Simulate a repeating monthly process with two operations per step
  • Detect when a loop would never finish and return a sentinel
Starting Python…