How to resolve the algorithm Continued fraction step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Continued fraction step by step in the OCaml 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 OCaml programming language
Source code in the ocaml programming language
let pi = 3, fun n -> ((2*n-1)*(2*n-1), 6)
and nap = 2, fun n -> (max 1 (n-1), n)
and root2 = 1, fun n -> (1, 2) in
let eval (i,f) k =
let rec frac n =
let a, b = f n in
float a /. (float b +.
if n >= k then 0.0 else frac (n+1)) in
float i +. frac 1 in
Printf.printf "sqrt(2)\t= %.15f\n" (eval root2 1000);
Printf.printf "e\t= %.15f\n" (eval nap 1000);
Printf.printf "pi\t= %.15f\n" (eval pi 1000);
You may also check:How to resolve the algorithm Abstract type step by step in the OCaml programming language
You may also check:How to resolve the algorithm CUSIP step by step in the Quackery programming language
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the MiniScript programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the V (Vlang) programming language