#!/usr/bin/perl -W sub f { my $i = shift; return $capital - $cuota * (1-(1+$i)**(-$n)) / $i; } $capital = 400_000; $n = 30*12; $cuota = 2200; $EPS=1; while (1+$EPS > 1){ $EPS /= 2; } $a = 2*$EPS; # strictly >0 $b = 1; # could be a bigger number, 1 == 100% while (++$count < 1000 && $a ne $b){ $m = ($a+$b)/2; $max = f($a); $min = f($b); $mean = f($m); print "$count) $max $min\n"; print "$a $b mean $m\n"; print "error= $mean\n"; if ($max*$mean<0){ $b=$m; }elsif ($mean*$min<0){ $a=$m; }elsif (abs($max-$min)<=$EPS || $max eq $min){ last; } } print "TAE = ", ((1+$m)**12)-1, "\n"; # annualized __END__ This is a mortage data, to solve for interest rate of the pay period (iterative bisection method)