Have you considered your loop's condition? It will never be reached..
Translated to pascal the above code is equivalent to:
Pascal Code:
function func(N: Integer): Single;
var
guess: Single;
avgDeter: Single;
begin
guess := n / 4;
writeln('Creating a guess, we will use: ');
while (abs(n - guess) > 0.0001) do
begin
writeln('Condition: ', n - guess);
avgDeter := n / guess;
writeln('Average: ', avgDeter);
guess = (guess + avgDeter) / 2;
writeln('Guess: ', guess);
end;
end;
begin
func(144);
end.
It will print:
Progress Report:
Condition: 108
Average: 4
Guess: 20
Condition: 124
Average: 7.199999809
Guess: 13.60000038
Condition: 130.3999939
Average: 10.5882349
Guess: 12.09411812
Condition: 131.9058838
Average: 11.9066143
Guess: 12.00036621
Condition: 131.9996338
Average: 11.99963379
Guess: 12
Condition: 132
Average: 12
Guess: 12
And finally it will hit an equilibrium.. such that the avgDeter = guess. The steps it takes are:
Progress Report:
guess = 144 / 4 = 36
if ((abs(n - guess) = 144 - 36 = 108) > 0.001) continue.
108 > 0.001 = true.
avgDeter = n / guess = 144 / 36 = 4.
guess = (guess + avgDeter) / 2 = (36 + 4) / 2 = 20.
--------------------------------------------------------
if ((abs(n - guess) = 144 - 20 = 124) > 0.001) continue.
124 > 0.0001 = true.
avgDeter = n / guess = 144 / 124 = 1.16129032258
guess = (guess + avgDeter) / 2 = (20 + 1.16129032258) / 2 = 10.5806451613
---------------------------------------------------------
if ((abs(n - guess) = 144 - 10.5806451613 = 133.419354839) > 0.001) continue.
133.419354839 > 0.001 = true.
avgDeter = n / guess = 144 / 10.5806451613 = 13.6097560975
guess = (guess + avgDeter) / 2 = (10.5806451613 + 13.6097560975) / 2 = 12.0952006294
---------------------------------------------------------------------------
if ((abs(n - guess) = 144 - 12.0952006294 = 131.904799371) > 0.001) continue.
131.904799371 > 0.001 = true.
avgDeter = n / guess = 144 / 12.0952006294 = 11.9055486893
guess = (guess + avgDeter) / 2 = (12.0952006294 + 11.9055486893) / 2 = 12.0003746594
---------------------------------------------------------------------------
if ((abs(n - guess) = 144 - 12.0003746594 = 131.999625341) > 0.001) continue.
131.999625341 > 0.001 = true.
And so on.. you can see that your condition is the problem and that the avgDeter & guess become very close; if not the exact same. With your condition, it will NEVER ever get anywhere close to 0.001 and so.. the infinite loop occurs.
Taking a stab based on the above numbers without messing up your algorithm, the following can be assumed to be the correct formula:
Simba Code:
function func(N: Integer): Single;
var
guess: Single;
avgDeter: Single;
begin
guess := n / 4;
writeln('Creating a guess, we will use: ');
while ((avgDeter - guess) <> 0) do //use epsilon instead of 0 when comparing floats.. but for the sake of simplicity here, I left it out..
begin
writeln('Condition: ', n - guess);
avgDeter := n / guess;
writeln('Average: ', avgDeter);
guess := (guess + avgDeter) / 2;
writeln('Guess: ', guess);
end;
result := guess;
end;
and you can see that for three of the test cases: 144, 25, 9 it prints: 12, 5, 3 respectively.
Translate it back to python, all you have to do.. is change the condition to:
Python Code:
while (compWithEpsilon(abs(avgDeter - guess))):
where compWithEpsilon should be a function that does proper floating point comparisons for you.
QED.