Golden ratio/Convergence: Difference between revisions
Content added Content deleted
(Added Sidef) |
(Add C# implementation) |
||
Line 598: | Line 598: | ||
Result: 1.618033 after 14 iterations |
Result: 1.618033 after 14 iterations |
||
The error is approximately -0.000001 |
The error is approximately -0.000001 |
||
</pre> |
|||
=={{header|C#}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="C#"> |
|||
using System; |
|||
public class GoldenRatio { |
|||
static void Iterate() { |
|||
double oldPhi = 1.0, phi = 1.0, limit = 1e-5; |
|||
int iters = 0; |
|||
while (true) { |
|||
phi = 1.0 + 1.0 / oldPhi; |
|||
iters++; |
|||
if (Math.Abs(phi - oldPhi) <= limit) break; |
|||
oldPhi = phi; |
|||
} |
|||
Console.WriteLine($"Final value of phi : {phi:0.00000000000000}"); |
|||
double actualPhi = (1.0 + Math.Sqrt(5.0)) / 2.0; |
|||
Console.WriteLine($"Number of iterations : {iters}"); |
|||
Console.WriteLine($"Error (approx) : {phi - actualPhi:0.00000000000000}"); |
|||
} |
|||
public static void Main(string[] args) { |
|||
Iterate(); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Final value of phi : 1.61803278688525 |
|||
Number of iterations : 14 |
|||
Error (approx) : -0.00000120186465 |
|||
</pre> |
</pre> |
||