How to resolve the algorithm Continued fraction step by step in the Lambdatalk programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Continued fraction step by step in the Lambdatalk programming language
Table of Contents
Problem Statement
The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients: For the square root of 2, use
a
0
= 1
{\displaystyle a_{0}=1}
then
a
N
= 2
{\displaystyle a_{N}=2}
.
b
N
{\displaystyle b_{N}}
is always
1
{\displaystyle 1}
. For Napier's Constant, use
a
0
= 2
{\displaystyle a_{0}=2}
, then
a
N
= N
{\displaystyle a_{N}=N}
.
b
1
= 1
{\displaystyle b_{1}=1}
then
b
N
= N − 1
{\displaystyle b_{N}=N-1}
. For Pi, use
a
0
= 3
{\displaystyle a_{0}=3}
then
a
N
= 6
{\displaystyle a_{N}=6}
.
b
N
= ( 2 N − 1
)
2
{\displaystyle b_{N}=(2N-1)^{2}}
.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Continued fraction step by step in the Lambdatalk programming language
Source code in the lambdatalk programming language
{def gcf
{def gcf.rec
{lambda {:f :n :r}
{if {< :n 1}
then {+ {car {:f 0}} :r}
else {gcf.rec :f
{- :n 1}
{let { {:r :r}
{:ab {:f :n}}
} {/ {cdr :ab}
{+ {car :ab} :r}} }}}}}
{lambda {:f :n}
{gcf.rec :f :n 0}}}
{def phi
{lambda {:n}
{cons 1 1}}}
{gcf phi 50}
-> 1.618033988749895
{def sqrt2
{lambda {:n}
{cons {if {> :n 0} then 2 else 1} 1}}}
{gcf sqrt2 25}
-> 1.4142135623730951
{def napier
{lambda {:n}
{cons {if {> :n 0} then :n else 2} {if {> :n 1} then {- :n 1} else 1} }}}
{gcf napier 20}
-> 2.7182818284590455
{def fpi
{lambda {:n}
{cons {if {> :n 0} then 6 else 3} {pow {- {* 2 :n} 1} 2} }}}
{gcf fpi 500}
-> 3.1415926 516017554
// only 8 exact decimals for 500 iterations
// A very very slow convergence.
// Here is a quicker version without any obvious pattern
{def pi
{lambda {:n}
{cons {A.get :n {A.new 3 7 15 1 292 1 1 1 2 1 3 1 14 2 1 1}} 1}}}
{gcf pi 15}
-> 3.1415926 53589793
// Much quicker, 15 exact decimals after 15 iterations
You may also check:How to resolve the algorithm Comma quibbling step by step in the XBS programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Sleep step by step in the AWK programming language
You may also check:How to resolve the algorithm Copy a string step by step in the i programming language
You may also check:How to resolve the algorithm Sleep step by step in the Erlang programming language