in reply to dividend with recursive routine

Why would you do such a thing? Just use the formula for compound interest:
Future_value = present_value*(1 + interest_rate_per_period)**number_of +_periods

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: dividend with recursive routine
by JavaFan (Canon) on Aug 23, 2010 at 21:27 UTC
    That formula assumes intermediate values can be non-integers. However, the OP wants to buy more shares with each years profit. And shares can only be bought as units. For instance, had the starting number of stock been 10 shares, it would have never increased, as the paid out yearly dividend would not have been enough to buy a new share.

    For the OPs values, a starting value of 1000, an interest of 6.78% and 20 years, your formula results in 3714 (rounded to nearest integer). But if you can only buy shares in units, you end up with 3696 shares. Close, but still 0.5% difference.

      Then I must have a better stockbroker than you, as I find partial shares in my account, which indeed automatically invests the dividends.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        It doesn't matter how good your stockbroker is. What matters is what the OP does. And the OP buys shares in units.
Re^2: dividend with recursive routine
by derpp (Acolyte) on Aug 23, 2010 at 19:54 UTC
    Yeah, but I wanna practice using recursive routines. Not working out so well, though.

      Define your problem in terms of itself:

      To calculate the total dividends received over N periods, calculate the total dividends received over the first N-1 periods, then add the dividends received in the Nth period.

      The rest should follow.