Golden ratio/Convergence: Difference between revisions

Realize in F#
(Realize in F#)
 
(13 intermediate revisions by 9 users not shown)
Line 279:
 
print "result: ", phi1, " after ", i, " iterations"
print "the error is approximately ", phi1 - (.5 * (1 + sqrt(5)))</syntaxhighlight>
{{out| Output}}<pre>result: 1.618033 after 14 iterations
 
the error is approximately -0.000001</pre>
end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 598:
Result: 1.618033 after 14 iterations
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>
 
Line 872 ⟶ 906:
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses code from [https://rosettacode.org/wiki/Continued_fraction#F# Continued fraction(F#)]
<syntaxhighlight lang="fsharp">
// Golden ratio/Convergence. Nigel Galloway: March 8th., 2024
let ϕ=let aπ()=fun()->1M in cf2S(aπ())(aπ())
let (_,n),g=let mutable l=1 in (ϕ|>Seq.pairwise|>Seq.skipWhile(fun(n,g)->l<-l+1;abs(n-g)>1e-5M)|>Seq.head,l)
printfn "Value of ϕ is %1.14f after %d iterations error with respect to (1+√5)/2 is %1.14f" n g (abs(n-(decimal(1.0+(sqrt 5.0))/2M)))
</syntaxhighlight>
{{out}}
<pre>
Value of ϕ is 1.61803278688525 after 14 iterations error with respect to (1+√5)/2 is 0.00000120186465
</pre>
=={{header|Fortran}}==
==={{header|Fortran77}}===
Line 1,166 ⟶ 1,212:
Number of iterations : 61
Error (approx) : 2.722811719173566624681571006109388407808876983723400587864054809516456794284321e-26
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">local phi, phi0, expected, iters = 1, 0, (1 + math.sqrt(5)) / 2, 0
repeat
phi0, phi = phi, 1 + 1 / phi
iters = iters + 1
until math.abs(phi0 - phi) < 1e-5
io.write( "after ", iters, " iterations, phi = ", phi )
io.write( ", expected value: ", expected, ", diff: ", math.abs( expected - phi ), "\n" )
</syntaxhighlight>
{{out}}
<pre>after 14 iterations, phi = 1.6180327868852, expected value: 1.6180339887499, diff: 1.2018646491363e-06
</pre>
 
Line 1,287 ⟶ 1,346:
<pre>Result: 1.6180327868852458 after 14 iterations
The error is approximately -1.2018646491362972e-06
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/strutils
 
iterator goldenRatio(): (int, float) =
var phi = 1.0
var idx = 0
while true:
yield (idx, phi)
phi = 1 + 1 / phi
inc idx
 
const Eps = 1e-5
const Phi = 1.6180339887498948482045868343656381177203091798057628621354486
var prev = 0.0
for (i, phi) in goldenRatio():
if abs(phi - prev) <= Eps:
echo "Final value of φ: ", phi
echo "Number of iterations: ", i
echo "Approximative error: ", formatFloat(phi - Phi, ffDecimal)
break
prev = phi
</syntaxhighlight>
 
{{out}}
<pre>Final value of φ: 1.618032786885246
Number of iterations: 14
Approximative error: -0.0000012018646491</pre>
 
=={{header|Odin}}==
<syntaxhighlight lang="Go">
package golden
import "core:fmt"
import "core:math"
/* main block */
main :: proc() {
iterate()
}
/* definitions */
iterate :: proc() {
count := 0
phi0: f64 = 1.0
difference: f64 = 1.0
phi1: f64
fmt.println("\nGolden ratio/Convergence")
fmt.println("-----------------------------------------")
for 1.0e-5 < difference {
phi1 = 1.0 + (1.0 / phi0)
difference = abs(phi1 - phi0)
phi0 = phi1
count += 1
fmt.printf("Iteration %2d : Estimate : %.8f\n", count, phi1)
}
fmt.println("-----------------------------------------")
fmt.printf("Result: %.8f after %d iterations", phi1, count)
fmt.printf("\nThe error is approximately %.10f\n", (phi1 - (0.5 * (1.0 + math.sqrt_f64(5.0)))))
}
 
</syntaxhighlight>
{{out}}
<pre>
Golden ratio/Convergence
-----------------------------------------
Iteration 01 : Estimate : 2.00000000
Iteration 02 : Estimate : 1.50000000
Iteration 03 : Estimate : 1.66666667
Iteration 04 : Estimate : 1.60000000
Iteration 05 : Estimate : 1.62500000
Iteration 06 : Estimate : 1.61538462
Iteration 07 : Estimate : 1.61904762
Iteration 08 : Estimate : 1.61764706
Iteration 09 : Estimate : 1.61818182
Iteration 10 : Estimate : 1.61797753
Iteration 11 : Estimate : 1.61805556
Iteration 12 : Estimate : 1.61802575
Iteration 13 : Estimate : 1.61803714
Iteration 14 : Estimate : 1.61803279
-----------------------------------------
Result: 1.61803279 after 14 iterations
The error is approximately -0.0000012019
</pre>
 
Line 1,349 ⟶ 1,489:
<pre>Result: 987/610 (1.618032786) after 14 iterations
The error is approximately -0.000001202</pre>
 
=={{header|Onyx (wasm)}}==
<syntaxhighlight lang="TS">
package main
use core {*}
use core.math{abs}
use core.intrinsics.wasm{sqrt_f64}
main :: () {
iterate();
}
iterate :: () {
count := 0;
phi0: f64 = 1.0;
difference: f64 = 1.0;
phi1: f64;
println("\nGolden ratio/Convergence");
println("-----------------------------------------");
while 0.00001 < difference {
phi1 = 1.0 + (1.0 / phi0);
difference = abs(phi1 - phi0);
phi0 = phi1;
count += 1;
printf("Iteration {} : Estimate : {.8}\n", count, phi1);
}
println("-----------------------------------------");
printf("Result: {} after {} iterations", phi1, count);
printf("\nThe error is approximately {.8}\n", (phi1 - (0.5 * (1.0 + sqrt_f64(5.0)))));
println("\n");
}
</syntaxhighlight>
{{out}}
<pre>
Golden ratio/Convergence
-----------------------------------------
Iteration 1 : Estimate : 2.00000000
Iteration 2 : Estimate : 1.50000000
Iteration 3 : Estimate : 1.66666666
Iteration 4 : Estimate : 1.60000000
Iteration 5 : Estimate : 1.62500000
Iteration 6 : Estimate : 1.61538461
Iteration 7 : Estimate : 1.61904761
Iteration 8 : Estimate : 1.61764705
Iteration 9 : Estimate : 1.61818181
Iteration 10 : Estimate : 1.61797752
Iteration 11 : Estimate : 1.61805555
Iteration 12 : Estimate : 1.61802575
Iteration 13 : Estimate : 1.61803713
Iteration 14 : Estimate : 1.61803278
-----------------------------------------
Result: 1.6180 after 14 iterations
The error is approximately -0.00000120
</pre>
 
=={{header|ooRexx}}==
Line 1,662 ⟶ 1,854:
I ran this with shen-scheme.
<pre>[1.6180327868852458 14 -1.2018646493583418e-6]</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">const phi = (1+sqrt(5))/2
func GR(n) is cached { (n == 1) ? 1 : (1 + 1/__FUNC__(n-1)) }
 
var n = (1..Inf -> first {|n|
abs(GR(n) - GR(n+1)) <= 1e-5
})
 
say "#{n} iterations: #{GR(n+1)} (cfrac)\n#{' '*15}#{phi} (actual)"
say "The error is: #{GR(n+1) - phi}"</syntaxhighlight>
{{out}}
<pre>
14 iterations: 1.6180327868852459016393442622950819672131147541 (cfrac)
1.61803398874989484820458683436563811772030917981 (actual)
The error is: -0.00000120186464894656524257207055615050719442570740221
</pre>
 
=={{header|V (Vlang)}}==
Line 1,696 ⟶ 1,905:
{{libheader|Wren-fmt}}
Wren's only built-in numeric type is a 64 bit float.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var oldPhi = 1
2,171

edits