Simple Interest Earned on a Short-Term Loan
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Compute the interest earned on a loan using a simple (non-compounding) annual percentage rate prorated over a number of months.
A lender issues a single-payment loan. The borrower receives a principal amount, and at the end of the term repays the principal plus interest in one lump sum.
Interest is charged as simple interest at a given annual percentage rate (APR). Assume the following:
- There is no compounding.
- There is no discounting for inflation or time value of money.
- The lender has no cost of funds, no fees, and no defaults.
- The borrower always repays in full.
Under these assumptions, the lender's profit equals the total interest collected. Write a function that returns this profit.
Input
principal: the amount lent, as a number (the loan amount in dollars).apr: the annual interest rate expressed as a percentage, so30means 30% per year (not0.30).months: the length of the loan in whole months.
Output
Return the profit (the total interest earned) as a number. It is the principal multiplied by the annual rate as a fraction, multiplied by the loan term expressed as a fraction of a year (months / 12). Do not include the principal itself in the result, and do not round the result.
Constraints
principalis a non-negative number.apris a non-negative number given in percent units.monthsis a non-negative integer.- The loan is never compounded, and the term is prorated linearly (each month is exactly 1/12 of a year).
Examples
Example 1
Input: simple_loan_interest(1000, 30, 4)
Output: 100
Example 2
Input: simple_loan_interest(5000, 12, 12)
Output: 600
Example 3
Input: simple_loan_interest(250, 24, 3)
Output: 15
Hints
Hint 1
Convert the percentage APR into a fraction by dividing by 100.
Hint 2
A loan lasting `months` months covers `months / 12` of a year, and with no compounding the interest scales linearly with time.
Hint 3
Profit is just the interest: principal times the annual rate fraction times the year fraction. Do not add the principal back.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
With simple interest and no other costs, the profit is exactly the interest charged. The standard formula is:
interest = principal * annual_rate * time_in_years
- The APR is given in percent, so the annual rate as a fraction is
apr / 100. - The term is given in months, so the time in years is
months / 12. - Multiply the three values together and return the result.
For example, a $1,000 loan at 30% APR for 4 months earns 1000 * 0.30 * (4/12) = 100.
Why it is correct
Without compounding, interest accrues linearly: each month earns the same amount, equal to principal * annual_rate / 12. Over months months, the total is that monthly amount times months, which is the formula above. Since there are no costs, defaults, or discounting, interest is the whole profit. The principal is returned to the lender but is not profit, so it is not included.
Complexity
- Time: O(1)
- Space: O(1)
Reference solution
def simple_loan_interest(principal, apr, months):
return principal * (apr / 100.0) * (months / 12.0)