How to resolve the algorithm Continued fraction step by step in the C programming language
How to resolve the algorithm Continued fraction step by step in the C 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 C programming language
This C program calculates approximations for three different continued fractions: the square root of 2, the Napier constant, and pi.
Continued fractions are a way of representing real numbers as an infinite nested fraction. For example, the square root of 2 can be represented as the continued fraction
sqrt(2) = 1 + 1/(2 + 1/(2 + 1/(2 + ...)))
This program calculates a finite number of terms of this continued fraction to approximate the value of sqrt(2).
The program first defines the calc
function, which takes two coefficient series, f_a
and f_b
, and the number of expansions to calculate. It then iterates through the expansions, calculating the next term of the continued fraction and adding it to the result.
Next, the program defines three coefficient series: one for sqrt(2), one for the Napier constant, and one for pi. Each coefficient series is represented by two functions, f_a
and f_b
, which return the next coefficient in the series.
Finally, the program calls the calc
function for each of the three coefficient series, passing in 1000 as the number of expansions to calculate. The results are then printed to the console.
Here is a breakdown of the key parts of the program:
- The
calc
function takes two coefficient series,f_a
andf_b
, and the number of expansions to calculate. It then iterates through the expansions, calculating the next term of the continued fraction and adding it to the result. - The
sqrt2_a
,sqrt2_b
,napier_a
,napier_b
,pi_a
, andpi_b
functions are the coefficient series for sqrt(2), the Napier constant, and pi, respectively. - The
main
function calls thecalc
function for each of the three coefficient series, passing in 1000 as the number of expansions to calculate. The results are then printed to the console.
Source code in the c programming language
/* calculate approximations for continued fractions */
#include <stdio.h>
/* kind of function that returns a series of coefficients */
typedef double (*coeff_func)(unsigned n);
/* calculates the specified number of expansions of the continued fraction
* described by the coefficient series f_a and f_b */
double calc(coeff_func f_a, coeff_func f_b, unsigned expansions)
{
double a, b, r;
a = b = r = 0.0;
unsigned i;
for (i = expansions; i > 0; i--) {
a = f_a(i);
b = f_b(i);
r = b / (a + r);
}
a = f_a(0);
return a + r;
}
/* series for sqrt(2) */
double sqrt2_a(unsigned n)
{
return n ? 2.0 : 1.0;
}
double sqrt2_b(unsigned n)
{
return 1.0;
}
/* series for the napier constant */
double napier_a(unsigned n)
{
return n ? n : 2.0;
}
double napier_b(unsigned n)
{
return n > 1.0 ? n - 1.0 : 1.0;
}
/* series for pi */
double pi_a(unsigned n)
{
return n ? 6.0 : 3.0;
}
double pi_b(unsigned n)
{
double c = 2.0 * n - 1.0;
return c * c;
}
int main(void)
{
double sqrt2, napier, pi;
sqrt2 = calc(sqrt2_a, sqrt2_b, 1000);
napier = calc(napier_a, napier_b, 1000);
pi = calc(pi_a, pi_b, 1000);
printf("%12.10g\n%12.10g\n%12.10g\n", sqrt2, napier, pi);
return 0;
}
You may also check:How to resolve the algorithm Formatted numeric output step by step in the Ring programming language
You may also check:How to resolve the algorithm Variable size/Set step by step in the Racket programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Ursala programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the F# programming language