How to resolve the algorithm Continued fraction step by step in the Clojure programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Continued fraction step by step in the Clojure 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 Clojure programming language
Source code in the clojure programming language
(defn cfrac
[a b n]
(letfn [(cfrac-iter [[x k]] [(+ (a k) (/ (b (inc k)) x)) (dec k)])]
(ffirst (take 1 (drop (inc n) (iterate cfrac-iter [1 n]))))))
(def sq2 (cfrac #(if (zero? %) 1.0 2.0) (constantly 1.0) 100))
(def e (cfrac #(if (zero? %) 2.0 %) #(if (= 1 %) 1.0 (double (dec %))) 100))
(def pi (cfrac #(if (zero? %) 3.0 6.0) #(let [x (- (* 2.0 %) 1.0)] (* x x)) 900000))
You may also check:How to resolve the algorithm Dot product step by step in the Sidef programming language
You may also check:How to resolve the algorithm Greyscale bars/Display step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the EMal programming language
You may also check:How to resolve the algorithm A+B step by step in the Raku programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Lasso programming language