# Program for calculating inverse series summation with compound interest

print "Number of payments? ";  $n = <>;
print "Size of each payment? ";  $payment = <>;
print "Resulting principal? ";  $total = <>;
# print "Initial estimate of interest (%)? ";  $xinit = <>;
$xinit = 10;  # This is actually way too high, but then you iterate in from a safe direction.

$epsilon = 0.000100/2;

$k = 1+($total/$payment);
$xnew = $xinit;

open(TEST, ">numbers.txt");

for ($x = $xinit+1; abs ($xnew-$x) > $epsilon; $xnew = $x - $f/$fprime) {
	$x = $xnew;
	$f = $x**($n+1) - $k*$x + $k-1;
	$fprime = ($n+1)*($x**$n) - $k;
print TEST $xnew,"\n";
};
$xnew = ($xnew-1)*100;

print TEST "Inputs: $n payments of \$$payment to get \$$total.\n";
print TEST "The annual compound interest rate is ",$xnew,"%\n";

close(TEST);

# 10 INPUT "number of payments?               ",N
# 20 INPUT "size of each payment?            $",PAYMENT
# 30 INPUT "resulting principal?             $",TOTAL
# 40 INPUT "initial estimate of interest (%)? ",XINIT
# 50 EPSILON=0.0001
# 60 K=1+(TOTAL/PAYMENT)
# 70 X=XINIT
# 80 XOLD=XINIT
# 90 EPSILON=EPSILON/2
# 100 F=X^(N+1)-K*X+K-1
# 110 FPRIME=(N+1)*(X^N)-K
# 120 X=XOLD-F/FPRIME
# 130 IF ABS(X-XOLD)<EPSILON THEN 160
# 140 XOLD=X
# 150 GOTO 100
# 160 X=(X-1)*100
# 170 PRINT "The annual compound interest rate is "X"%
# 180 END
