How to resolve the algorithm Continued fraction step by step in the Go programming language
How to resolve the algorithm Continued fraction step by step in the Go 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 Go programming language
The provided code is written in Go and implements continued fraction (CF) expansions for approximating the values of sqrt(2), Napier's constant (Napier), and pi. Continued fractions represent real numbers as an infinite series of fractions, providing a way to approximate irrational numbers.
Here's a breakdown of the code:
-
Data Structures:
cfTerm
: A struct representing a term in a continued fraction, consisting of two integersa
andb
.cf
: An array or slice ofcfTerm
structs, representing a continued fraction.
-
Continued Fraction Expansions:
cfSqrt2
,cfNap
,cfPi
: These functions create and initialize continued fractions for approximating sqrt(2), Napier's constant, and pi, respectively. They set the initial terms based on specific formulas for each constant.
-
Real Value Calculation:
(f cf) real()
: This method calculates the real (approximate) value of the continued fractionf
. It uses an iterative process, starting with the last term and working backward to compute the fractional value.
-
Main Function:
- In the
main
function, continued fractions are created for sqrt(2), Napier's constant, and pi, and their real values are printed to the console.
- In the
Here's how the code works:
-
Continued Fraction Initialization: The
cfSqrt2
,cfNap
, andcfPi
functions initialize the continued fractions with appropriate terms based on formulas for each constant. -
Real Value Calculation: The
real
method of thecf
type computes the real value of the continued fraction using an iterative algorithm. It starts with the last term and calculates each fractional value backward, resulting in an approximation of the real number. -
Main Function: In the
main
function, continued fractions for sqrt(2), Napier's constant, and pi are created with 20 terms each. Their real values are calculated and printed to the console.
This code provides a simplified implementation of continued fractions for approximating irrational numbers. By using a fixed number of terms, it provides a finite approximation of the constants sqrt(2), Napier's constant, and pi.
Source code in the go programming language
package main
import "fmt"
type cfTerm struct {
a, b int
}
// follows subscript convention of mathworld and WP where there is no b(0).
// cf[0].b is unused in this representation.
type cf []cfTerm
func cfSqrt2(nTerms int) cf {
f := make(cf, nTerms)
for n := range f {
f[n] = cfTerm{2, 1}
}
f[0].a = 1
return f
}
func cfNap(nTerms int) cf {
f := make(cf, nTerms)
for n := range f {
f[n] = cfTerm{n, n - 1}
}
f[0].a = 2
f[1].b = 1
return f
}
func cfPi(nTerms int) cf {
f := make(cf, nTerms)
for n := range f {
g := 2*n - 1
f[n] = cfTerm{6, g * g}
}
f[0].a = 3
return f
}
func (f cf) real() (r float64) {
for n := len(f) - 1; n > 0; n-- {
r = float64(f[n].b) / (float64(f[n].a) + r)
}
return r + float64(f[0].a)
}
func main() {
fmt.Println("sqrt2:", cfSqrt2(20).real())
fmt.Println("nap: ", cfNap(20).real())
fmt.Println("pi: ", cfPi(20).real())
}
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Rust programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Haskell programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Yabasic programming language