TexanBruceWayne
TexanBruceWayne

Reputation: 11

Python - Accumulated Value of Annual Investment

I have a program (futval.py) that will calculate the value of an investment after 10 years. I want to modify the program so that instead of calculating the value of a one time investment after 10 years, it will calculate the value of an annual investment after 10 years. I want to do this without using an accumulator variable. Is it possible to do this with only the variables that were present in the original program (investment, apr, i)?

# futval.py
# A program to compute the value of an investment
# carried 10 years into the future

def main():
    print "This program calculates the future value",
    print "of a 10-year investment."

    investment = input("Enter the initial investment: ")
    apr = input("Enter the annual interest rate: ")

    for i in range(10):
        investment = investment * (1 + apr)

    print "The value in 10 years is:", investment

main()

I was not able to accomplish modifying the program without introducing the 'futval' accumulator variable.

# futval10.py
# A program to compute the value of an annual investment
# carried 10 years into the future

def main():
    print "This program calculates the future value",
    print "of a 10-year annual investment."

    investment = input("Enter the annual investment: ")
    apr = input("Enter the annual interest rate: ")

    futval = 0

    for i in range(10):
        futval = (futval + investment) * (1+apr)

    print "The value in 10 years is:", futval

main()

Upvotes: 1

Views: 1250

Answers (2)

chapelo
chapelo

Reputation: 2562

Well, you don't need an accumulator, but you still need a temporary variable to hold the original value of the periodic investment:

def main():
    print "This program calculates the future value",
    print "of a 10-year investment."

    investment = input("Enter the initial investment: ")
    apr = input("Enter the annual interest rate: ")

    hold = investment
    investment = investment / apr

    for i in range(10):
        investment = investment * (1 + apr)

    investment = investment - hold / apr

    print "The value in 10 years is:", investment

main()

Upvotes: 0

Vsevolod Timchenko
Vsevolod Timchenko

Reputation: 756

Okay, if you try doing some math you will see the solution yourself. For the first year we have:

new_value = investment*(1 + apr)

For the second:

new_second_value = (new_value + investment)*(1+apr)

or

new_second_value = (investment*(1 + apr) + investment)*(1+apr)

Et cetera. If you actually try multiplying this stuff, you'll see that after 10 years the final value is

investment*((1+apr)**10) + investment*((1+apr)**9)+...   etc

so the solution for your problem is just

print("The value in 10 years is:", sum([investment*((1+apr)**i) for i in range(1, 11)]))

EDIT: Somehow I managed to overlook the fact that what I wrote is just a geometric series, so the answer is even simpler:

ten_years_annual_investment = investment*(apr+1)*((apr+1)**10 - 1)/apr

Upvotes: 1

Related Questions