in reply to Help w/ Code Optimization
Newton's method works by drawing a straight line, tangent to the polynomial curve, at the point of the guess. For your examples, your guess was much too big, so the straight line was almost vertical. This means that the new guess wasn't very far from the old one.my $guess = Math::BigFloat->new( $num / $root );
I replaced this one line with the following:
This uses regular hardware floating-point arithmetic to make the initial guess. Hardware floating point is accurate to 17 decimal places and is almost instantaneous. Suddenly, the initial guess is vastly more accurate, and the cost is almost zero.my $guess = Math::BigFloat->new( $num**(1/$root));
Suddenly, I was getting very accurate answers very quickly. After 10 iterations, your version of the program still thinks that Root(1000,60) is about 14.088. The correct answer is about 1.12; with my change the program has the answer correct to 130 places after only 4 iterations.
I'd also recommend simplifying the guess computation as suggested by arhuman above. I guess he made a simple mistake in the algebra. But you have g- (gr-n)/rgr-1, and you can cancel the gr out of the numerator by rewriting this expression as g-g/r+ n/rgr-1. This trades a ** for a /, which should be a big win.
Hope this helps.