Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Golden-ratio-Convergence/00-META.yaml
Normal file
3
Task/Golden-ratio-Convergence/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Golden_ratio/Convergence
|
||||
note: Mathematics
|
||||
12
Task/Golden-ratio-Convergence/00-TASK.txt
Normal file
12
Task/Golden-ratio-Convergence/00-TASK.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
The [[wp:Golden_ratio|golden ratio]] can be defined as the continued fraction
|
||||
:<math>\phi = 1 + {1\over{1+{1\over{1+{1\over{1 + \cdots}}}}}}</math>
|
||||
Thus <math>\phi = 1 + {1/\phi}</math>. Multiplying both sides by <math>\phi</math> and solving the resulting quadratic equation for its positive solution, one gets <math>\phi = (1 + \sqrt{5})/2 \approx 1.61803398875</math>.
|
||||
|
||||
The golden ratio has the slowest convergence of any continued fraction, as one might guess by noting that the denominators are made of the smallest positive integer. This task treats the problem of convergence in a somewhat backwards fashion: we are going to iterate the recursion <math>\phi_{n+1} = 1 + {1/\phi_{n}}</math>, <math>\phi_{0} = 1</math>, and see how long it takes to get an answer.
|
||||
|
||||
===Task===
|
||||
Iterate <math>\phi_{n+1} = 1 + {1/\phi_{n}}</math>, <math>\phi_{0} = 1</math>, until <math>|\phi_{n+1} - \phi_{n}| \le 1\times10^{-5}</math>. Report the final value of <math>\phi_{n+1}</math>, the number of iterations required, and the error with respect to <math>(1 + \sqrt{5})/2</math>.
|
||||
|
||||
===See also===
|
||||
* [[Metallic_ratios|Metallic ratios]]
|
||||
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
procedure iterate (phi0, n0, phi, n);
|
||||
value phi0, n0;
|
||||
real phi0, phi;
|
||||
integer n0, n;
|
||||
begin
|
||||
phi := 1.0 + (1.0 / phi0);
|
||||
n := n0 + 1;
|
||||
if 100000.0 * abs (phi - phi0) > 1.0 then
|
||||
iterate (phi, n, phi, n)
|
||||
end iterate;
|
||||
|
||||
begin
|
||||
real phi;
|
||||
integer n;
|
||||
|
||||
iterate (1.0, 0, phi, n);
|
||||
outstring (1, "Result: ");
|
||||
outreal (1, phi);
|
||||
outstring (1, "after ");
|
||||
outinteger (1, n);
|
||||
outstring (1, "iterations\n");
|
||||
outstring (1, "The error is approximately ");
|
||||
outreal (1, phi - (0.5 * (1.0 + sqrt (5.0))));
|
||||
outstring (1, "\n")
|
||||
end
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
BEGIN # calculate the Golden Ratio and show the number of iterations required #
|
||||
|
||||
INT count := 0;
|
||||
REAL phi0 := 1, diff := 1e20;
|
||||
|
||||
WHILE 1e-5 < diff DO
|
||||
REAL phi1 = 1.0 + ( 1.0 / phi0 );
|
||||
diff := ABS ( phi1 - phi0 );
|
||||
phi0 := phi1;
|
||||
count +:= 1
|
||||
OD;
|
||||
|
||||
print( ( "Result:", fixed( phi0, -9, 6 ), " after ", whole( count, 0 ), " iterations", newline ) );
|
||||
print( ( "The error is approximately ", fixed( phi0 - ( 0.5 * ( 1 + sqrt( 5 ) ) ), -9, 6 ), newline ) )
|
||||
|
||||
END
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
begin % calculate the Golden Ratio and show the number of iterations required %
|
||||
|
||||
integer count;
|
||||
real phi0, diff;
|
||||
|
||||
count := 0;
|
||||
phi0 := 1;
|
||||
diff := 1'20;
|
||||
|
||||
while 1'-5 < diff do begin
|
||||
real phi1;
|
||||
phi1 := 1.0 + ( 1.0 / phi0 );
|
||||
diff := abs( phi1 - phi0 );
|
||||
phi0 := phi1;
|
||||
count := count + 1
|
||||
end while_1e_5_lt_diff ;
|
||||
|
||||
write( i_w := 1, s_w := 0, "Result:", phi0, " after ", count, " iterations" );
|
||||
write( i_w := 1, s_w := 0, "The error is approximately ", phi0 - ( 0.5 * ( 1 + sqrt( 5 ) ) ) )
|
||||
|
||||
end.
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
%{^
|
||||
#include <math.h>
|
||||
%}
|
||||
|
||||
extern fn sqrt : double -<> double = "mac#sqrt"
|
||||
|
||||
fun
|
||||
iterate {n : nat}
|
||||
(phi : double,
|
||||
n : int n) : @(double, intGte 1) =
|
||||
let
|
||||
val phi1 = 1.0 + (1.0 / phi)
|
||||
and n1 = succ n
|
||||
in
|
||||
if abs (phi1 - phi) <= 1.0e-5 then
|
||||
@(phi1, n1)
|
||||
else
|
||||
iterate {n + 1} (phi1, n1)
|
||||
end
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val @(phi, n) = iterate {0} (1.0, 0)
|
||||
val _ = $extfcall (int, "printf",
|
||||
"Result: %.10f after %d iterations\n",
|
||||
phi, n)
|
||||
val _ = $extfcall (int, "printf",
|
||||
"The error is approximately %.10f\n",
|
||||
phi - (0.5 * (1.0 + sqrt (5.0))))
|
||||
in
|
||||
end
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
with ada.text_io; use ada.text_io;
|
||||
|
||||
procedure golden_ratio_convergence is
|
||||
|
||||
-- This is a fixed-point type. I typed a bunch of "0"
|
||||
-- without counting them.
|
||||
type number is delta 0.000000000000001 range -10.0 .. 10.0;
|
||||
|
||||
one : constant number := number (1.0);
|
||||
phi : constant number := number (1.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475); -- OEIS A001622
|
||||
phi0, phi1 : number;
|
||||
count : integer;
|
||||
|
||||
begin
|
||||
count := 1;
|
||||
phi0 := 1.0;
|
||||
phi1 := (one + (one / phi0));
|
||||
while abs (phi1 - phi0) > number (1.0e-5) loop
|
||||
count := count + 1;
|
||||
phi0 := phi1;
|
||||
phi1 := (one + (one / phi0));
|
||||
end loop;
|
||||
put ("Result:");
|
||||
put (phi1'image);
|
||||
put (" after");
|
||||
put (count'image);
|
||||
put_line (" iterations");
|
||||
put ("The error is approximately ");
|
||||
put_line (number'image (phi1 - phi));
|
||||
end golden_ratio_convergence;
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
call iterate()
|
||||
end
|
||||
|
||||
subroutine iterate()
|
||||
iter = 0
|
||||
phi0 = 1.0
|
||||
do
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diferencia = abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter += 1
|
||||
until (1.0e-5 > diferencia)
|
||||
|
||||
print "Result: "; phi1; " after "; iter; " iterations"
|
||||
print "The error is approximately "; phi1 - (0.5 * (1.0 + sqrt(5.0)))
|
||||
end subroutine
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
100 I = 0
|
||||
110 P0 = 1.0
|
||||
120 P1 = 1.0 + (1.0 / P0)
|
||||
130 D = ABS (P1 - P0)
|
||||
140 P0 = P1
|
||||
150 I = I + 1
|
||||
160 IF 1.0E-5 < D THEN 120
|
||||
170 PRINT "RESULT:" P1 "AFTER" I "ITERATIONS"
|
||||
180 E = P1 - (0.5 * (1.0 + SQR (5.0)))
|
||||
190 PRINT "THE ERROR IS APPROXIMATELY " E
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
double oldPhi = 1.0, phi = 1.0, limit = 1e-5;
|
||||
int iters = 0;
|
||||
while (true) {
|
||||
phi = 1.0 + 1.0 / oldPhi;
|
||||
iters++;
|
||||
if (abs(phi - oldPhi) <= limit) break;
|
||||
oldPhi = phi;
|
||||
}
|
||||
cout.setf(ios::fixed);
|
||||
cout << "Final value of phi : " << setprecision(14) << phi << endl;
|
||||
double actualPhi = (1.0 + sqrt(5.0)) / 2.0;
|
||||
cout << "Number of iterations : "<< iters << endl;
|
||||
cout << "Error (approx) : " << setprecision(14) << phi - actualPhi << endl;
|
||||
return 0;
|
||||
}
|
||||
54
Task/Golden-ratio-Convergence/C/golden-ratio-convergence.c
Normal file
54
Task/Golden-ratio-Convergence/C/golden-ratio-convergence.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
static void
|
||||
using_float () /* C2x does not require "void". */
|
||||
{
|
||||
int count = 0;
|
||||
float phi0 = 1.0f;
|
||||
float phi1;
|
||||
float difference;
|
||||
do
|
||||
{
|
||||
phi1 = 1.0f + (1.0f / phi0);
|
||||
difference = fabsf (phi1 - phi0);
|
||||
phi0 = phi1;
|
||||
count += 1;
|
||||
}
|
||||
while (1.0e-5f < difference);
|
||||
|
||||
printf ("Using type float --\n");
|
||||
printf ("Result: %f after %d iterations\n", phi1, count);
|
||||
printf ("The error is approximately %f\n",
|
||||
phi1 - (0.5f * (1.0f + sqrtf (5.0f))));
|
||||
}
|
||||
|
||||
static void
|
||||
using_double () /* C2x does not require "void". */
|
||||
{
|
||||
int count = 0;
|
||||
double phi0 = 1.0;
|
||||
double phi1;
|
||||
double difference;
|
||||
do
|
||||
{
|
||||
phi1 = 1.0 + (1.0 / phi0);
|
||||
difference = fabs (phi1 - phi0);
|
||||
phi0 = phi1;
|
||||
count += 1;
|
||||
}
|
||||
while (1.0e-5 < difference);
|
||||
|
||||
printf ("Using type double --\n");
|
||||
printf ("Result: %f after %d iterations\n", phi1, count);
|
||||
printf ("The error is approximately %f\n",
|
||||
phi1 - (0.5 * (1.0 + sqrt (5.0))));
|
||||
}
|
||||
|
||||
int
|
||||
main () /* C2x does not require "void". */
|
||||
{
|
||||
using_float ();
|
||||
printf ("\n");
|
||||
using_double ();
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
*> -*- mode: cobol -*-
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. GOLDEN_RATIO_CONVERGENCE.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 PHI0 PIC 9(1)V9(12).
|
||||
01 PHI1 PIC 9(1)V9(12).
|
||||
01 DIFF PIC S9(1)V9(12).
|
||||
01 ERR PIC S9(1)V9(12).
|
||||
01 NOMINAL-VALUE PIC 9(1)V9(12).
|
||||
01 ITER PIC 9(2).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
MOVE 0 TO ITER
|
||||
MOVE 1.0 TO PHI0.
|
||||
LOOP.
|
||||
COMPUTE PHI1 = 1.0 + (1.0 / PHI0)
|
||||
COMPUTE DIFF = FUNCTION ABS (PHI1 - PHI0)
|
||||
MOVE PHI1 TO PHI0
|
||||
ADD 1 TO ITER
|
||||
IF 100000 * DIFF > 1.0
|
||||
GO TO LOOP
|
||||
END-IF
|
||||
DISPLAY 'RESULT: ' PHI1 ' AFTER ' ITER ' ITERATIONS'
|
||||
MOVE 1.61803398874989 TO NOMINAL-VALUE
|
||||
COMPUTE ERR = PHI1 - NOMINAL-VALUE
|
||||
DISPLAY 'THE ERROR IS APPROXIMATELY ' ERR.
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
100 iterate
|
||||
110 end
|
||||
120 sub iterate()
|
||||
130 iter = 0
|
||||
140 phi0 = 1
|
||||
150 do
|
||||
160 phi1 = 1+(1/phi0)
|
||||
170 diff = abs(phi1-phi0)
|
||||
180 phi0 = phi1
|
||||
190 iter = iter+1
|
||||
200 loop until (1.000000E-05 > diff)
|
||||
210 print "Result: ";phi1;" after ";iter;" iterations"
|
||||
220 print "The error is approximately ";phi1-(0.5*(1+sqr(5)))
|
||||
230 end sub
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
(defun iterate (phi n)
|
||||
;; This is a tail recursive definition, copied from the
|
||||
;; Scheme. Common Lisp does not guarantee proper tail calls, but the
|
||||
;; depth of recursion will not be too great.
|
||||
(let ((phi1 (1+ (/ phi)))
|
||||
(n1 (1+ n)))
|
||||
(if (<= (abs (- phi1 phi)) 1/100000)
|
||||
(values phi1 n1)
|
||||
(iterate phi1 n1))))
|
||||
|
||||
(multiple-value-bind (phi n) (iterate 1 0)
|
||||
(princ "Result: ")
|
||||
(princ phi)
|
||||
(princ " (")
|
||||
(princ (* 1.0 phi))
|
||||
(princ ") after ")
|
||||
(princ n)
|
||||
(princ " iterations")
|
||||
(terpri)
|
||||
(princ "The error is approximately ")
|
||||
(princ (- phi (* 0.5 (+ 1.0 (sqrt 5.0)))))
|
||||
(terpri))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
precision 6
|
||||
|
||||
define i = 0, d = 0, phi0 = 1, phi1 = 0
|
||||
|
||||
do
|
||||
|
||||
let phi1 = 1 / phi0 + 1
|
||||
let d = abs(phi1 - phi0)
|
||||
let phi0 = phi1
|
||||
let i = i + 1
|
||||
|
||||
wait
|
||||
|
||||
loopuntil .00001 > d
|
||||
|
||||
print "result: ", phi1, " after ", i, " iterations"
|
||||
print "the error is approximately ", phi1 - (.5 * (1 + sqrt(5)))
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
import 'dart:math';
|
||||
|
||||
void iterate() {
|
||||
int count = 0;
|
||||
double phi0 = 1.0;
|
||||
double phi1;
|
||||
double difference;
|
||||
do {
|
||||
phi1 = 1.0 + (1.0 / phi0);
|
||||
difference = (phi1 - phi0).abs();
|
||||
phi0 = phi1;
|
||||
count += 1;
|
||||
} while (1.0e-5 < difference);
|
||||
|
||||
print("Result: $phi1 after $count iterations");
|
||||
print("The error is approximately ${phi1 - (0.5 * (1.0 + sqrt(5.0)))}");
|
||||
}
|
||||
|
||||
void main() {
|
||||
iterate();
|
||||
}
|
||||
|
|
@ -0,0 +1,108 @@
|
|||
[GoldenRatio/Convergence for Rosetta Code.
|
||||
EDSAC program, Initial Orders 2]
|
||||
[-----------------------------------------------------------------
|
||||
Given phi_n as in the task description, let u_n = 1 - phi_n.
|
||||
Then u_0 = 0 and the recurrence is u_(n+1) = 1/(u_n - 1).
|
||||
To keep the arguments of the division subroutine D6 within range,
|
||||
the recurrence is calculated as u_(n+1) = (1/4)/(u_n/4 - 1/4).
|
||||
-----------------------------------------------------------------]
|
||||
[Arrange the storage]
|
||||
T47K P300F [M parameter: main routine]
|
||||
T48K P56F [& (Delta) parameter: library s/r D6 (division)]
|
||||
T49K P92F [L parameter: library s/r P1 (prints real)]
|
||||
T50K P200F [X parameter: library s/r P7 (prints integer)]
|
||||
[--------------------------------------------------------------------------
|
||||
Library subroutine R2, reads values as positive integers at load time,
|
||||
and is then overwritten.]
|
||||
GKT20FVDL8FA40DUDTFI40FA40FS39FG@S2FG23FA5@T5@E4@E13Z
|
||||
T#M [tell R2 where to store values]
|
||||
[Values when interpreted as reals are 1/4, 0.00001, -0.6180339887]
|
||||
4294967296F171799F23741995290#TZ
|
||||
[--------------------------------------------------------------------------
|
||||
[Library subroutine M3, prints header at load time, and is then overwritten.
|
||||
Here, last character sets teleprinter to figures.]
|
||||
PFGKIFAFRDLFUFOFE@A6FG@E8FEZPF
|
||||
*AIMING!AT!#1!A!*PHI@&ITERATIONS!!!FINAL!VALUE!!!!!ABS!ERROR@&#..
|
||||
PK [after header, blank tape and PK (WWG, 1951, page 91)]
|
||||
[--------------------------------------------------------------------------
|
||||
M parameter: Main routine]
|
||||
E25K TM GK
|
||||
[Constants read in by R2 are stored here]
|
||||
[0] [1/4]
|
||||
[2] [0.00001, test for convergence]
|
||||
[4] [limit as adapted for EDSAC, (1 - sqrt(5))/2 = -0.618...]
|
||||
T6Z [don't overwrite constants: load from relative location 6]
|
||||
[6] PF PF [current term]
|
||||
[8] PF PF [previous term]
|
||||
[10] PF [number of iterations, right justified]
|
||||
[11] PD [17-bit constant 1]
|
||||
[Enter here with acc = 0]
|
||||
[12] T6#@ [u_0 := 0]
|
||||
T10@ [count of iterations := 0]
|
||||
[Loop to get next term]
|
||||
[14] TF [clear acc]
|
||||
A10@ A11@ T10@ [inc number of iterations]
|
||||
A#@ TD [0D := 1/4 for division subroutine]
|
||||
A6#@ U8#@ [previous term := current term u_n]
|
||||
R1F [shift 2 right, acc := u_n/4]
|
||||
S#@ T4D [4D := u_n/4 - 1/4 for division subroutine]
|
||||
A25@ G& [call dvision s/r, 0D := u_(n+1)]
|
||||
AD U6#@ [store u_(n+1)]
|
||||
S8#@ E33@ [acc := u_(n+1) - u_n, skip if >= 0]
|
||||
T4D S4D [else negate to get absolute difference]
|
||||
[33] S2#@ [test for convergence]
|
||||
E14@ [loop back if not converged]
|
||||
[Here when converged]
|
||||
TF TD [clear acc and whole of 0D (including sandwich bit)]
|
||||
A10@ TF [0D := count of iterations, extended to 35 bits]
|
||||
A39@ GX O79@ [print count and space]
|
||||
A6#@ TD [final value to 0D for printing]
|
||||
A44@ G59@ O79@ [print value and space]
|
||||
A4#@ S6#@ E52@ [acc := error, skip if >= 0]
|
||||
TD SD [else negate to get absolute value]
|
||||
[52] TD [absolute error to 0D for printing]
|
||||
A53@ G59@ O80@ O81@ [print error and new line]
|
||||
O82@ [print null to flush teleprinter buffer]
|
||||
ZF [halt machine]
|
||||
[----------------------------------------------------------------------------
|
||||
Wrapper for library subroutine P1. Adds '0.' before the decimal part,
|
||||
preceded by space or minus sign.]
|
||||
[59] A3F T76@ [plant return link as usual]
|
||||
[61] AD G65@ [acc := number to print; jump if < 0]
|
||||
O79@ E68@ [write space, join common code]
|
||||
[65] TD SD [acc := number negated]
|
||||
O61@ [write minus sign]
|
||||
[68] YF YF [rounding: add 2^-34, nearest possible to 0.5*(10^-10)]
|
||||
O77@ O78@ [print '0.']
|
||||
TD [pass value to print subroutine]
|
||||
A73@ GL P10F [call P1, print 10 decimals]
|
||||
[76] ZF [(planted) jump back to caller]
|
||||
[77] PF [digit '0' in figures mode]
|
||||
[78] MF [full stop, here used for decimal point]
|
||||
[79] !F [space]
|
||||
[80] @F [carriage return]
|
||||
[81] &F [line feed]
|
||||
[82] K4096F [null char]
|
||||
[--------------------------------------------------------------------------
|
||||
Library subroutine P1: prints non-negative fraction in 0D, without '0.']
|
||||
E25K TL
|
||||
GKA18@U17@S20@T5@H19@PFT5@VDUFOFFFSFL4FTDA5@A2FG6@EFU3FJFM1F
|
||||
[--------------------------------------------------------------------------
|
||||
Library subroutine P7, prints long strictly positive integer;
|
||||
10 characters, right justified, padded left with spaces.
|
||||
Even address; 35 storage locations; working position 4D.]
|
||||
E25K TX
|
||||
GKA3FT26@H28#@NDYFLDT4DS27@TFH8@S8@T1FV4DAFG31@SFLDUFOFFFSF
|
||||
L4FT4DA1FA27@G11@XFT28#ZPFT27ZP1024FP610D@524D!FO30@SFL8FE22@
|
||||
[--------------------------------------------------------------------------
|
||||
Library subroutine D6: Division, accurate, fast.
|
||||
0D := 0D/4D, where 4D <> 0, -1.
|
||||
36 locations, working positons 6D and 8D.]
|
||||
E25K T&
|
||||
GKA3FT34@S4DE13@T4DSDTDE2@T4DADLDTDA4DLDE8@RDU4DLD
|
||||
A35@T6DE25@U8DN8DA6DT6DH6DS6DN4DA4DYFG21@SDVDTDEFW1526D
|
||||
[--------------------------------------------------------------------------
|
||||
M parameter again: define entry point]
|
||||
E25K TM GK
|
||||
E12Z
|
||||
PF [enter with acc = 0]
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
phi0 = 1
|
||||
repeat
|
||||
phi = 1 + 1 / phi0
|
||||
until abs (phi - phi0) < 1e-5
|
||||
phi0 = phi
|
||||
iter += 1
|
||||
.
|
||||
numfmt 10 0
|
||||
print "Iterations: " & iter
|
||||
print "Result: " & phi
|
||||
print "Error: " & phi - (1 + sqrt 5) / 2
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
Sub using_Single
|
||||
Dim As Integer iter = 0
|
||||
Dim As Single phi0 = 1.0f
|
||||
Dim As Single phi1
|
||||
Dim As Single diferencia
|
||||
Do
|
||||
phi1 = 1.0f + (1.0f / phi0)
|
||||
diferencia = Abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter += 1
|
||||
Loop While (1.0e-5f < diferencia)
|
||||
|
||||
Print "Using type Single --"
|
||||
Print Using "Result: #.########## after ## iterations"; phi1; iter
|
||||
Print Using "The error is approximately #.##########"; phi1 - (0.5f * (1.0f + Sqr(5.0f)))
|
||||
End Sub
|
||||
|
||||
Sub using_Double
|
||||
Dim As Integer iter = 0
|
||||
Dim As Double phi0 = 1.0
|
||||
Dim As Double phi1
|
||||
Dim As Double diferencia
|
||||
Do
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diferencia = Abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter += 1
|
||||
Loop While (1.0e-5 < diferencia)
|
||||
|
||||
Print "Using type Double --"
|
||||
Print Using "Result: #.########## after ## iterations"; phi1; iter
|
||||
Print Using "The error is approximately #.##########"; phi1 - (0.5 * (1.0 + Sqr(5.0)))
|
||||
End Sub
|
||||
|
||||
using_Single
|
||||
Print
|
||||
using_Double
|
||||
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
100 CLS : rem 100 HOME for Applesoft BASIC : DELETE for Minimal BASIC
|
||||
110 LET I = 0
|
||||
120 LET P0 = 1
|
||||
130 LET P1 = 1+(1/P0)
|
||||
140 LET D = ABS(P1-P0)
|
||||
150 LET P0 = P1
|
||||
160 LET I = I+1
|
||||
170 IF .00001 < D THEN 130
|
||||
180 PRINT "Result: ";P1;" after ";I;" iterations"
|
||||
190 PRINT "The error is approximately ";P1-(.5*(1+SQR(5)))
|
||||
200 END
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
Public Sub Main()
|
||||
|
||||
using_Single
|
||||
Print
|
||||
using_Float
|
||||
|
||||
End
|
||||
|
||||
Sub using_Single()
|
||||
|
||||
Dim iter As Integer = 0
|
||||
Dim phi0 As Single = 1.0
|
||||
Dim phi1 As Single
|
||||
Dim diferencia As Single
|
||||
|
||||
Do
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diferencia = Abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter += 1
|
||||
Loop While (1.0e-5 < diferencia)
|
||||
|
||||
Print "Using type Single --"
|
||||
Print "Result: "; Format$(phi1, "#.##########"); " after "; iter; " iterations"
|
||||
Print "The error is approximately "; Format$(phi1 - (0.5 * (1.0 + Sqr(5.0))), "#.##########")
|
||||
|
||||
End Sub
|
||||
|
||||
Sub using_Float()
|
||||
|
||||
Dim iter As Integer = 0
|
||||
Dim phi0 As Float = 1.0
|
||||
Dim phi1 As Float
|
||||
Dim diferencia As Float
|
||||
|
||||
Do
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diferencia = Abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter += 1
|
||||
Loop While (1.0e-5 < diferencia)
|
||||
|
||||
Print "Using type Float --"
|
||||
Print "Result: "; Format$(phi1, "#.##########"); " after "; iter; " iterations"
|
||||
Print "The error is approximately "; Format$(phi1 - (0.5 * (1.0 + Sqr(5.0))), "#.##########")
|
||||
|
||||
End Sub
|
||||
25
Task/Golden-ratio-Convergence/Go/golden-ratio-convergence.go
Normal file
25
Task/Golden-ratio-Convergence/Go/golden-ratio-convergence.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
oldPhi := 1.0
|
||||
var phi float64
|
||||
iters := 0
|
||||
limit := 1e-5
|
||||
for {
|
||||
phi = 1.0 + 1.0/oldPhi
|
||||
iters++
|
||||
if math.Abs(phi-oldPhi) <= limit {
|
||||
break
|
||||
}
|
||||
oldPhi = phi
|
||||
}
|
||||
fmt.Printf("Final value of phi : %16.14f\n", phi)
|
||||
actualPhi := (1.0 + math.Sqrt(5.0)) / 2.0
|
||||
fmt.Printf("Number of iterations : %d\n", iters)
|
||||
fmt.Printf("Error (approx) : %16.14f\n", phi-actualPhi)
|
||||
}
|
||||
13
Task/Golden-ratio-Convergence/Hy/golden-ratio-convergence.hy
Normal file
13
Task/Golden-ratio-Convergence/Hy/golden-ratio-convergence.hy
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(import math)
|
||||
|
||||
(defn iterate [phi n]
|
||||
(setv phi1 (+ 1.0 (/ 1.0 phi)))
|
||||
(setv n1 (+ n 1))
|
||||
(if (<= (abs (- phi1 phi)) 1e-5)
|
||||
[phi1 n1]
|
||||
(iterate phi1 n1)))
|
||||
|
||||
(setv [phi n] (iterate 1.0 0))
|
||||
(print "Result:" phi "after" n "iterations")
|
||||
(print "The error is approximately"
|
||||
(- phi (* 0.5 (+ 1 (math.sqrt 5)))))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
global one, one_squared
|
||||
|
||||
procedure main ()
|
||||
local result, phi, n, floatphi
|
||||
one := 1000000000000000000
|
||||
one_squared := one * one
|
||||
result := iterate (one, 0)
|
||||
phi := result[1]
|
||||
n := result[2]
|
||||
floatphi := real (phi) / one
|
||||
write ("Result: ", phi, "/", one, " (", floatphi, ")")
|
||||
write (" ", n, " iterations")
|
||||
write ("The error is approximately ",
|
||||
floatphi - (0.5 * (1 + sqrt (5))))
|
||||
end
|
||||
|
||||
procedure iterate (phi, n)
|
||||
local phi1, n1
|
||||
phi1 := one + (one_squared / phi)
|
||||
n1 := n + 1
|
||||
if 100000 * abs (phi1 - phi) <= one then
|
||||
return [phi1, n1]
|
||||
else
|
||||
return iterate (phi1, n1)
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(,({:p)-~{&p)>:1 i.~1e_5>:|2 -/\p=. 1&(+%)^:a:1 NB. iterations and error
|
||||
14 _1.20186e_6
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
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;
|
||||
}
|
||||
System.out.printf("Final value of phi : %16.14f\n", phi);
|
||||
double actualPhi = (1.0 + Math.sqrt(5.0)) / 2.0;
|
||||
System.out.printf("Number of iterations : %d\n", iters);
|
||||
System.out.printf("Error (approx) : %16.14f\n", phi - actualPhi);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
iterate();
|
||||
}
|
||||
}
|
||||
15
Task/Golden-ratio-Convergence/Jq/golden-ratio-convergence.jq
Normal file
15
Task/Golden-ratio-Convergence/Jq/golden-ratio-convergence.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
def phi: (1 + (5|sqrt)) / 2;
|
||||
|
||||
# epsilon > 0
|
||||
def phi($epsilon):
|
||||
{ phi: 1, oldPhi: (1+$epsilon), iters:0}
|
||||
| until( (.phi - .oldPhi) | length < $epsilon;
|
||||
.oldPhi = .phi
|
||||
| .phi = 1 + (1 / .oldPhi)
|
||||
| .iters += 1 )
|
||||
|
||||
| "Final value of phi : \(.phi) vs \(phi)",
|
||||
"Number of iterations : \(.iters)",
|
||||
"Absolute error (approx) : \((.phi - phi) | length)";
|
||||
|
||||
phi(1e-5)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
function iterate_phi(limit::T) where T <: Real
|
||||
phi, oldphi, iters = one(limit), one(limit), 0
|
||||
while true
|
||||
phi = 1 + 1 / oldphi
|
||||
iters += 1
|
||||
abs(phi - oldphi) <= limit && break
|
||||
oldphi = phi
|
||||
end
|
||||
println("Final value of phi : $phi")
|
||||
println("Number of iterations : $iters")
|
||||
println("Error (approx) : $(phi - (1 + sqrt(T(5))) / 2)")
|
||||
end
|
||||
|
||||
iterate_phi(1 / 10^5)
|
||||
iterate_phi(1 / big(10)^25)
|
||||
14
Task/Golden-ratio-Convergence/M4/golden-ratio-convergence.m4
Normal file
14
Task/Golden-ratio-Convergence/M4/golden-ratio-convergence.m4
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
divert(-1)
|
||||
|
||||
define(`iterate',`define(`iterations',0)`'_$0(`$1')')
|
||||
define(`_iterate',
|
||||
`define(`iterations',eval(iterations + 1))`'dnl
|
||||
pushdef(`phi1',eval(10000 + ((10000 * 10000) / ($1))))`'dnl
|
||||
pushdef(`diff',ifelse(eval((phi1 - ($1)) < 0),1,dnl
|
||||
eval(($1) - phi1),eval(phi1 - ($1))))`'dnl
|
||||
ifelse(eval(diff < 1),1,phi1,`_iterate(phi1)')`'dnl
|
||||
popdef(`phi1',`diff')')
|
||||
|
||||
divert`'dnl
|
||||
eval(iterate(10000) / 10000)`.'eval(iterate(10000) % 10000)
|
||||
iterations `iterations'
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
iterate(phi) := 1 + (1 / phi)$
|
||||
|
||||
phi0: 1$
|
||||
phi1: iterate(phi0)$
|
||||
for n: 1 step 1 while abs(phi1 - phi0) > 1e-5 do
|
||||
block(phi0: phi1, phi1: iterate(phi0), iterations: n + 1)$
|
||||
|
||||
display (float(phi1))$
|
||||
display (iterations)$
|
||||
display (float(phi1 - (0.5 * (1 + sqrt(5)))))$
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
%%% -*- mode: mercury; prolog-indent-width: 2; -*-
|
||||
|
||||
:- module golden_ratio_convergence_mercury.
|
||||
|
||||
:- interface.
|
||||
:- import_module io.
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
:- import_module float.
|
||||
:- import_module int.
|
||||
:- import_module math.
|
||||
:- import_module string.
|
||||
|
||||
:- pred iterate(float::in, float::out, int::in, int::out) is det.
|
||||
iterate(!Phi, !N) :-
|
||||
Phi1 = (1.0 + (1.0 / !.Phi)),
|
||||
N1 = !.N + 1,
|
||||
(if (abs(Phi1 - !.Phi) =< 1.0e-5)
|
||||
then (!:Phi = Phi1, !:N = N1)
|
||||
else (iterate(Phi1, !:Phi, N1, !:N))).
|
||||
|
||||
main(!IO) :-
|
||||
iterate(1.0, Phi, 0, N),
|
||||
print("Result: ", !IO),
|
||||
print(from_float(Phi), !IO),
|
||||
print(" after ", !IO),
|
||||
print(from_int(N), !IO),
|
||||
print(" iterations", !IO),
|
||||
nl(!IO),
|
||||
print("The error is approximately ", !IO),
|
||||
print(from_float(Phi - (0.5 * (1.0 + (sqrt(5.0))))), !IO),
|
||||
nl(!IO).
|
||||
|
||||
:- end_module golden_ratio_convergence_mercury.
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import io, util
|
||||
|
||||
procedure main ()
|
||||
local phi0, phi1, count
|
||||
|
||||
count := 1
|
||||
phi0 := 1.0
|
||||
while abs ((phi1 := 1.0 + (1.0 / phi0)) - phi0) > 1.0e-5 do
|
||||
{
|
||||
phi0 := phi1
|
||||
count +:= 1
|
||||
}
|
||||
io.write ("Result: ", phi1, " after ", count, " iterations")
|
||||
io.write ("The error is approximately ",
|
||||
phi1 - (0.5 * (1.0 + Math.sqrt (5.0))))
|
||||
end
|
||||
25
Task/Golden-ratio-Convergence/Ol/golden-ratio-convergence.ol
Normal file
25
Task/Golden-ratio-Convergence/Ol/golden-ratio-convergence.ol
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(import (scheme base)
|
||||
(scheme write)
|
||||
(scheme inexact))
|
||||
|
||||
(define (iterate phi n)
|
||||
(let ((phi1 (+ 1 (/ phi)))
|
||||
(n1 (+ n 1)))
|
||||
(if (<= (abs (- phi1 phi)) 1/100000)
|
||||
(list phi1 n1)
|
||||
(iterate phi1 n1))))
|
||||
|
||||
(let* ((result (iterate 1 0))
|
||||
(phi (car result))
|
||||
(n (cadr result)))
|
||||
(display "Result: ")
|
||||
(display phi)
|
||||
(display " (")
|
||||
(display (inexact phi))
|
||||
(display ") after ")
|
||||
(display n)
|
||||
(display " iterations")
|
||||
(newline)
|
||||
(display "The error is approximately ")
|
||||
(display (inexact (- phi (* 0.5 (+ 1.0 (sqrt 5.0))))))
|
||||
(newline))
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/* REXX
|
||||
* Compute the Golden Ratio using iteration
|
||||
* 20230604 Walter Pachl
|
||||
*/
|
||||
Numeric Digits 16
|
||||
Parse Value '1 1 1e-5' With oldPhi phi limit
|
||||
Do iterations=1 By 1 Until delta<=limit
|
||||
phi=1+1/oldPhi /* next approximation */
|
||||
delta=abs(phi-oldPhi) /* difference to previous */
|
||||
If delta>limit Then /* not small enough */
|
||||
oldPhi=phi /* proceed with new value */
|
||||
End
|
||||
actualPhi=(1+rxCalcsqrt(5,16))/2 /* compute the real value */
|
||||
Say 'Final value of phi : ' phi /* our approximation */
|
||||
Say 'Actual value : ' actualPhi /* the real value */
|
||||
Say 'Error (approx) :' (phi-actualPhi) /* the difference */
|
||||
Say 'Number of iterations:' iterations
|
||||
Exit
|
||||
::REQUIRES RXMATH library
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
use strict; use warnings;
|
||||
use constant phi => (1 + sqrt 5) / 2;
|
||||
|
||||
sub GR { my $n = shift; $n == 1 ? 2 : 1 + 1 / GR($n - 1) }
|
||||
|
||||
my $i;
|
||||
while (++$i) {
|
||||
my $dev = abs phi - GR($i);
|
||||
(printf "%d iterations: %8.6f vs %8.6f (%8.6f)\n", $i, phi, GR($i), $dev and last) if $dev < 1e-5;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">Phi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">iter</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">Pi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Phi</span>
|
||||
<span style="color: #000000;">Phi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">Phi</span>
|
||||
<span style="color: #000000;">iter</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">until</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Phi</span><span style="color: #0000FF;">-</span><span style="color: #000000;">Pi</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">1e-5</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Result: %.14f after %d iterations\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">Phi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">iter</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Error: %.14f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">Phi</span><span style="color: #0000FF;">-(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
iterate(Phi0, N0, Phi, N) :-
|
||||
Phi1 = (1.0 + (1.0 / Phi0)),
|
||||
N1 is N0 + 1,
|
||||
((abs(Phi1 - Phi0) =< 1.0e-5)
|
||||
-> (Phi = Phi1, N = N1)
|
||||
; iterate(Phi1, N1, Phi, N)).
|
||||
|
||||
main :-
|
||||
iterate(1.0, 0, Phi, N),
|
||||
PhiApprox is Phi,
|
||||
Error is Phi - (0.5 * (1.0 + sqrt(5.0))),
|
||||
write('Final Phi = '),
|
||||
write(Phi),
|
||||
write('\n which is approximately '),
|
||||
write(PhiApprox),
|
||||
write('\n'),
|
||||
write(N),
|
||||
write(' iterations were required.'),
|
||||
write('\nThe error is approximately '),
|
||||
write(Error),
|
||||
write('\n'),
|
||||
halt.
|
||||
|
||||
:- initialization(main).
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
Procedure using_Float()
|
||||
Define iter.i = 0
|
||||
Define.f phi0 = 1.0, phi1, diff
|
||||
Repeat
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diff = Abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter + 1
|
||||
Until (1.0e-5 > diff)
|
||||
|
||||
PrintN("Using type Float --")
|
||||
PrintN("Result: " + FormatNumber((phi1), 10) + " after " + Str(iter) + " iterations")
|
||||
PrintN("The error is approximately " + FormatNumber((phi1 - (0.5 * (1.0 + Sqr(5.0)))), 10))
|
||||
EndProcedure
|
||||
|
||||
Procedure using_Double()
|
||||
Define iter.i = 0
|
||||
Define.d phi0 = 1.0, phi1, diff
|
||||
Repeat
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diff = Abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter + 1
|
||||
Until (1.0e-5 > diff)
|
||||
|
||||
PrintN("Using type Double --")
|
||||
PrintN("Result: " + FormatNumber((phi1), 10) + " after " + Str(iter) + " iterations")
|
||||
PrintN("The error is approximately " + FormatNumber((phi1 - (0.5 * (1.0 + Sqr(5.0)))), 10))
|
||||
EndProcedure
|
||||
|
||||
OpenConsole()
|
||||
using_Float()
|
||||
PrintN("")
|
||||
using_Double()
|
||||
Input()
|
||||
CloseConsole()
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import math
|
||||
|
||||
oldPhi = 1.0
|
||||
phi = 1.0
|
||||
iters = 0
|
||||
limit = 1e-5
|
||||
while True:
|
||||
phi = 1.0 + 1.0 / oldPhi
|
||||
iters += 1
|
||||
if math.fabs(phi - oldPhi) <= limit: break
|
||||
oldPhi = phi
|
||||
|
||||
print(f'Final value of phi : {phi:16.14f}')
|
||||
actualPhi = (1.0 + math.sqrt(5.0)) / 2.0
|
||||
print(f'Number of iterations : {iters}')
|
||||
print(f'Error (approx) : {phi - actualPhi:16.14f}')
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
SUB iterate
|
||||
iter = 0
|
||||
phi0 = 1!
|
||||
DO
|
||||
phi1 = 1! + (1! / phi0)
|
||||
diff = ABS(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter = iter + 1
|
||||
LOOP UNTIL (.00001 > diff)
|
||||
|
||||
PRINT "Result: "; phi1; " after "; iter; " iterations"
|
||||
PRINT "The error is approximately "; phi1 - (.5 * (1! + SQR(5!)))
|
||||
END SUB
|
||||
|
||||
CALL iterate
|
||||
END
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
program grconv
|
||||
integer count
|
||||
real phi0, phi1, diff
|
||||
|
||||
count = 0
|
||||
phi0 = 1.0
|
||||
diff = 1e+20
|
||||
while (1e-5 < diff)
|
||||
{
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diff = abs (phi1 - phi0)
|
||||
phi0 = phi1
|
||||
count = count + 1
|
||||
}
|
||||
|
||||
write (*,'("Result:", F9.6, " after", I3, " iterations")') _
|
||||
phi1, count
|
||||
write (*,'("The error is approximately ", F9.6)') _
|
||||
phi1 - (0.5 * (1.0 + sqrt (5.0)))
|
||||
end
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/* REXX
|
||||
* Compute the Golden Ratio using iteration
|
||||
* 20230604 Walter Pachl
|
||||
*/
|
||||
Numeric Digits 16
|
||||
Parse Value '1 1 1e-5' With oldPhi phi limit
|
||||
Do iterations=1 By 1 Until delta<=limit
|
||||
phi=1+1/oldPhi /* next approximation */
|
||||
delta=abs(phi-oldPhi) /* difference to previous */
|
||||
If delta>limit Then /* not small enough */
|
||||
oldPhi=phi /* proceed with new value */
|
||||
End
|
||||
actualPhi=(1+sqrt(5,16))/2 /* compute the real value */
|
||||
Say 'Final value of phi : ' phi /* our approximation */
|
||||
Say 'Actual value : ' actualPhi /* the real value */
|
||||
Say 'Error (approx) :' (phi-actualPhi) /* the difference */
|
||||
Say 'Number of iterations:' iterations
|
||||
Exit
|
||||
|
||||
sqrt: Procedure
|
||||
/* REXX *************************************************************
|
||||
* Return sqrt(x,precision) -- with the specified precision
|
||||
********************************************************************/
|
||||
Parse Arg x,xprec
|
||||
If x<0 Then /* a negative argument */
|
||||
Return 'nan' /* has no real square root */
|
||||
iprec=xprec+10 /* increase precision */
|
||||
Numeric Digits iprec /* for very safe results */
|
||||
r0= x
|
||||
r = 1 /* start value */
|
||||
Do Until r=r0 /* loop until no change of r */
|
||||
r0 = r /* previous value */
|
||||
r = (r + x/r) / 2 /* next approximation */
|
||||
End
|
||||
Numeric Digits xprec /* reset to desired precision*/
|
||||
Return (r+0) /* normalize the result */
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
constant phi = 1.FatRat, 1 + 1/* ... { abs($^a-$^b)≤1e-5 };
|
||||
|
||||
say "It took {phi.elems} iterations to reach {phi.tail}";
|
||||
say "The error is {{ ($^a - $^b)/$^b }(phi.tail, (1+sqrt(5))/2)}"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
φ_convergence = Enumerator.produce(1.0r){|prev| 1 + 1/prev}
|
||||
(_prev, c), i = φ_convergence.each_cons(2).with_index(1).detect{|(v1, v2), i| (v1-v2).abs <= 1E-5}
|
||||
|
||||
puts "Result after #{i} iterations: #{c.to_f}
|
||||
Error is about #{c - (0.5 * (1 + (5.0**0.5)))}."
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
(define (iterate phi n)
|
||||
(let ((phi1 (+ 1 (/ phi)))
|
||||
(n1 (+ n 1)))
|
||||
(if (<= (abs (- phi1 phi)) 1/100000)
|
||||
(values phi1 n1)
|
||||
(iterate phi1 n1))))
|
||||
|
||||
(call-with-values (lambda () (iterate 1 0))
|
||||
(lambda (phi n)
|
||||
(display "Result: ")
|
||||
(display phi)
|
||||
(display " (")
|
||||
(display (* 1.0 phi))
|
||||
(display ") after ")
|
||||
(display n)
|
||||
(display " iterations")
|
||||
(newline)
|
||||
(display "The error is approximately ")
|
||||
(display (- phi (* 0.5 (+ 1.0 (sqrt 5.0)))))
|
||||
(newline)))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(define iterate
|
||||
PHI N ->
|
||||
(let PHI1 (+ 1 (/ 1 PHI))
|
||||
N1 (+ N 1)
|
||||
DIFF (* 100000 (- PHI1 PHI))
|
||||
(if (and (<= -1 DIFF) (<= DIFF 1))
|
||||
[PHI1 N1 (- PHI1 1.618033988749895)]
|
||||
(iterate PHI1 N1))))
|
||||
|
||||
(print (iterate 1 0))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
SUB iterate
|
||||
LET iter = 0
|
||||
LET phi0 = 1
|
||||
DO
|
||||
LET phi1 = 1+(1/phi0)
|
||||
LET diff = abs(phi1-phi0)
|
||||
LET phi0 = phi1
|
||||
LET iter = iter+1
|
||||
LOOP until (.00001 > diff)
|
||||
PRINT "Result: "; phi1; " after "; iter; " iterations"
|
||||
PRINT "The error is approximately "; phi1-(.5*(1+sqr(5)))
|
||||
END SUB
|
||||
|
||||
CALL iterate
|
||||
END
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
import math
|
||||
|
||||
fn main() {
|
||||
limit := 1e-5
|
||||
mut old_phi := 1.0
|
||||
mut iters := 0
|
||||
mut phi, mut actual_phi := f64(0), f64(0)
|
||||
for {
|
||||
phi = 1 + 1 / old_phi
|
||||
iters++
|
||||
if math.abs(phi - old_phi) <= limit {break}
|
||||
old_phi = phi
|
||||
}
|
||||
println("Final value of phi : ${phi:.14f}")
|
||||
actual_phi = (1.0 + math.sqrt(5.0)) / 2.0
|
||||
println("Number of iterations : ${iters}")
|
||||
println("Error (approx) : ${phi - actual_phi:.14f}")
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import "./fmt" for Fmt
|
||||
|
||||
var oldPhi = 1
|
||||
var phi
|
||||
var iters = 0
|
||||
var limit = 1e-5
|
||||
while (true) {
|
||||
phi = 1 + 1 / oldPhi
|
||||
iters = iters + 1
|
||||
if ((phi - oldPhi).abs <= limit) break
|
||||
oldPhi = phi
|
||||
}
|
||||
Fmt.print("Final value of phi : $16.14f", phi)
|
||||
var actualPhi = (1 + 5.sqrt) / 2
|
||||
Fmt.print("Number of iterations : $d", iters)
|
||||
Fmt.print("Error (approx) : $16.14f", phi - actualPhi)
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
include xpllib; \for Print
|
||||
|
||||
real OldPhi, Phi, Limit, ActualPhi;
|
||||
int Iters;
|
||||
[OldPhi := 1.0;
|
||||
Iters := 0;
|
||||
Limit := 1e-5;
|
||||
loop [Phi := 1.0 + 1.0/OldPhi;
|
||||
Iters := Iters+1;
|
||||
if abs(Phi-OldPhi) <= Limit then
|
||||
quit;
|
||||
OldPhi := Phi;
|
||||
];
|
||||
Print("Final value of phi : %2.14f\n", Phi);
|
||||
ActualPhi := (1.0 + sqrt(5.0)) / 2.0;
|
||||
Print("Number of iterations : %d\n", Iters);
|
||||
Print("Error (approx) : %2.14f\n", Phi-ActualPhi);
|
||||
]
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
iterate()
|
||||
end
|
||||
|
||||
sub iterate()
|
||||
iter = 0
|
||||
phi0 = 1.0
|
||||
repeat
|
||||
phi1 = 1.0 + (1.0 / phi0)
|
||||
diferencia = abs(phi1 - phi0)
|
||||
phi0 = phi1
|
||||
iter = iter + 1
|
||||
until (1.0e-5 > diferencia)
|
||||
|
||||
print "Result: ", phi1, " after ", iter, " iterations"
|
||||
e$ = str$(phi1 - (0.5 * (1.0 + sqrt(5.0))), "%2.10f")
|
||||
print "The error is approximately ", e$
|
||||
end sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue