Next Tricks of the Wizards 162

Newton-Raphson: Financial Computations

        sub how_long {
          my ($P, $i, $F) = @_;
          my $g = 1;  # Initial guess
          my $d = 1;
          until ($d/$g < 0.000001) {
            $d =   ($P * (1+$i)**$g - $F) 
                 / ($P * (1+$i)**$g * log(1+$i));
            $g -= $d;
          }
          return $g;
        }


Next Copyright © 2003 M. J. Dominus