Golden ratio/Convergence: Difference between revisions

Added solution for EDSAC
(Added solution for EDSAC)
Line 731:
print "Error: " & phi - (1 + sqrt 5) / 2
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
By working with 1 - phi_n instead of phi_n, we bring the values into the EDSAC range [-1,1) without a change of scale.
<syntaxhighlight lang="edsac">
[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]
</syntaxhighlight>
{{out}}
<pre>
AIMING AT 1 - PHI
ITERATIONS FINAL VALUE ABS ERROR
14 -0.6180327869 0.0000012019
</pre>
 
=={{header|Fortran}}==
113

edits