I want to calculate the time it takes for the compound interest to reach a certain goal by using Pn=(1 i)Pn−1. What I have done is:
def my_saving_plan(P0,i,goal):
P = [P0]
years = 0
for n in range(0,10000):
if P[n] < goal:
P.append((1 i)*P[n-1])
years = 1
print(P)
elif P[n] >= goal:
return years
print(my_saving_plan(1000, 0.07, 2000))
But What I get from that is the number of compound interest twice. So for this example what I get is:
[1000, 1070.0, 1070.0, 1144.9, 1144.9, 1225.0430000000001,
1225.0430000000001, 1310.7960100000003, 1310.7960100000003,
1402.5517307000005, 1402.5517307000005, 1500.7303518490005,
1500.7303518490005, 1605.7814764784307, 1605.7814764784307,
1718.186179831921, 1718.186179831921, 1838.4592124201556,
1838.4592124201556, 1967.1513572895667, 1967.1513572895667,
2104.8519522998363]
21
Also I was wondering if there is a way to use a while loop instead of putting a ridiculous number in the for loop.
CodePudding user response:
Might as well turn my comment into an answer:
def my_saving_plan(P0,i,goal):
P = [P0]
while P[-1] < goal:
P.append((1 i)*P[-1])
print(P)
return len(P) - 1
print(my_saving_plan(1000, 0.07, 2000))
From my comment:
You have an off-by-one error in your append line. n
is already the last element, and using n-1
will be one behind that, thus causing duplication.
You could also use while P[-1] < goal
instead of for
. Index -1
always refers to the last element. You don't need n
or years
at all.
CodePudding user response:
You are always refering to an element one behind where you think you are.
This is the corrected code:
def my_saving_plan(P0, i, goal):
P = [P0]
years = 0
for n in range(0, 10000):
if P[n] < goal:
P.append((1 i) * P[n])
years = 1
else:
print(P)
return years
print(my_saving_plan(1000, 0.07, 2000))
Your code had: P.append((1 i)*P[n-1])
.
The expression: P[n-1]
on the first iteration has n == 0
, so n-1
is -1
which refers to the last element, which is the same as the first element since there is only one element P0
.
Each subsequent iteration (where n is 1...) then refers to the previous element, so you end up calculating each value twice.
CodePudding user response:
Using a while loop you can just keep track of the current value of the savings and in each step compare if the current value already reached the goal:
def my_saving_plan(P0, i, goal):
current_value = P0
P = [P0]
years = 0
while current_value < goal:
years = 1
current_value *= (1 i)
P.append(current_value)
print(P)
return years
CodePudding user response:
Instead of all this, why not use the formula to calculate the time it takes for a value to reach another with compound interest rate?
The problem can be expressed as: A x (1 e)^t = B
and we want to find t
. So, t = log(B/A) / log(1 e)
import math
B = 2000
A = 1000
e = 0.07
out = math.ceil(math.log(B/A) / math.log(1 e))
Output:
11
CodePudding user response:
Why you compute each value twice:
You insert P0 only once, but you use it twice. You use P[n-1]
, which in the first loop is P[-1]
. Python lists support negative indexes, index -1
gives you the last element, which is the value P0. In the second loop you use P[0]
, using the same element again (just using a different index). After that, you don't use the same element twice again, but you've appended the same value twice, so those two equal values will make you append two equal values. Which in turn will make you append two more equal values. Etc.