Question
You have deposited a specific amount of money into your bank account. Each year your balance increases at the same growth rate
. With the assumption that you don't make any additional deposits, find out how long it would take for your balance to pass a specific threshold
.
Example
For
deposit = 100
,rate = 20
, andthreshold = 170
, the output should bedepositProfit(deposit, rate, threshold) = 3
.Each year the amount of money in your account increases by
20%
. So throughout the years, your balance would be:- year 0:
100
; - year 1:
120
; - year 2:
144
; - year 3:
172.8
.
Thus, it will take
3
years for your balance to pass thethreshold
, so the answer is3
- year 0:
Input/Output
[execution time limit] 4 seconds (py3)
[input] integer deposit
The initial deposit, guaranteed to be a positive integer.
Guaranteed constraints:
1 ≤ deposit ≤ 100
.[input] integer rate
The rate of increase. Each year the balance increases by the
rate
percent of the current sum.Guaranteed constraints:
1 ≤ rate ≤ 100
.[input] integer threshold
The target balance.
Guaranteed constraints:
deposit < threshold ≤ 200
.[output] integer
- The number of years it would take to hit the
threshold
.
- The number of years it would take to hit the
MY_ANSWER
def depositProfit(deposit, rate, threshold):
i = 0
while threshold > deposit:
deposit = deposit + deposit*(rate/100)
i += 1
return i
- deposit이 rate만큼 이자율을 가질때 복리로 threshold까지 얼마나 걸릴지를 반환하는 문제
- while 구문을 이용하여 deposit이 threshold보다 커지기 전까지 반복
Best_ANSWER
def depositProfit(deposit, rate, threshold):
return math.ceil(math.log(threshold/deposit, 1+rate/100))
- threshold < deposit(1+rate/100)n 의 수식에서 n에 대한 값을 구하도록 return 값을 구현하였다.
- threshold/deposit < (1+rate/100)n -> log(threshold/deposit) / log(1+rate/100) < n