How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the C programming language

Table of Contents

Problem Statement

The purpose of this task is to write a function

r 2 c f

(

i n t

{\displaystyle {\mathit {r2cf}}(\mathrm {int} }

N

1

,

i n t

{\displaystyle N_{1},\mathrm {int} }

N

2

)

{\displaystyle N_{2})}

, or

r 2 c f

(

F r a c t i o n

{\displaystyle {\mathit {r2cf}}(\mathrm {Fraction} }

N )

{\displaystyle N)}

, which will output a continued fraction assuming: The function should output its results one digit at a time each time it is called, in a manner sometimes described as lazy evaluation. To achieve this it must determine: the integer part; and remainder part, of

N

1

{\displaystyle N_{1}}

divided by

N

2

{\displaystyle N_{2}}

. It then sets

N

1

{\displaystyle N_{1}}

to

N

2

{\displaystyle N_{2}}

and

N

2

{\displaystyle N_{2}}

to the determined remainder part. It then outputs the determined integer part. It does this until

a b s

(

N

2

)

{\displaystyle \mathrm {abs} (N_{2})}

is zero. Demonstrate the function by outputing the continued fraction for:

2

{\displaystyle {\sqrt {2}}}

should approach

[ 1 ; 2 , 2 , 2 , 2 , … ]

{\displaystyle [1;2,2,2,2,\ldots ]}

try ever closer rational approximations until boredom gets the better of you: Try : Observe how this rational number behaves differently to

2

{\displaystyle {\sqrt {2}}}

and convince yourself that, in the same way as

3.7

{\displaystyle 3.7}

may be represented as

3.70

{\displaystyle 3.70}

when an extra decimal place is required,

[ 3 ; 7 ]

{\displaystyle [3;7]}

may be represented as

[ 3 ; 7 , ∞ ]

{\displaystyle [3;7,\infty ]}

when an extra term is required.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the C programming language

The code you provided is a C program that implements a function called r2cf to convert a fraction to a continued fraction. A continued fraction is a representation of a number as a sequence of fractions, where each fraction has a numerator and a denominator.

The r2cf function takes two pointers to integers as arguments, representing the numerator and denominator of a fraction. It uses integer division to compute the quotient of the numerator by the denominator, and then updates the numerator and denominator to be the remainder of the division and the original denominator, respectively. The function returns the quotient.

The main function of the program runs the r2cf function on a series of example fractions, including the square root of 2 and pi. It prints the results of the function, which are the sequence of quotients that represent the continued fraction expansion of the original fraction, For example, the output for the fraction 1/2 is "0 1", which means that the continued fraction expansion of 1/2 is 0 + 1/(1 + 0). The output for the square root of 2 is "1 4 1 1 4 1 1 4 ...", which means that the continued fraction expansion of the square root of 2 is 1 + 1/(4 + 1/(1 + 1/(4 + 1/(1 + 1/(4 + ...))))).

Here is a breakdown of the code:

  • The fraction struct is used to represent a fraction with a numerator and a denominator. Several arrays of fraction structs are defined to represent different examples, including the square root of 2 and pi.

  • The r2cf function implements the algorithm to convert a fraction to a continued fraction. It takes two pointers to integers as arguments, representing the numerator and denominator of a fraction. It uses integer division to compute the quotient of the numerator by the denominator, and then updates the numerator and denominator to be the remainder of the division and the original denominator, respectively. The function returns the quotient.

  • The main function of the program runs the r2cf function on a series of example fractions, including the square root of 2 and pi. It prints the results of the function, which are the sequence of quotients that represent the continued fraction expansion of the original fraction.

Source code in the c programming language

#include<stdio.h>

typedef struct{
	int num,den;
	}fraction;

fraction examples[] = {{1,2}, {3,1}, {23,8}, {13,11}, {22,7}, {-151,77}}; 
fraction sqrt2[] = {{14142,10000}, {141421,100000}, {1414214,1000000}, {14142136,10000000}};
fraction pi[] = {{31,10}, {314,100}, {3142,1000}, {31428,10000}, {314285,100000}, {3142857,1000000}, {31428571,10000000}, {314285714,100000000}};

int r2cf(int *numerator,int *denominator)
{
	int quotient=0,temp;
	
	if(denominator != 0)
	{
		quotient = *numerator / *denominator;
		
		temp = *numerator;
		
		*numerator = *denominator;
		
		*denominator = temp % *denominator;
	}
	
	return quotient;
}

int main()
{
	int i;
	
	printf("Running the examples :");
	
	for(i=0;i<sizeof(examples)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",examples[i].num,examples[i].den);
		
		while(examples[i].den != 0){
			printf(" %d ",r2cf(&examples[i].num,&examples[i].den));
			}
	}
	
	printf("\n\nRunning for %c2 :",251); /* 251 is the ASCII code for the square root symbol */
	
	for(i=0;i<sizeof(sqrt2)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",sqrt2[i].num,sqrt2[i].den);
		
		while(sqrt2[i].den != 0){
			printf(" %d ",r2cf(&sqrt2[i].num,&sqrt2[i].den));
			}
	}
	
	printf("\n\nRunning for %c :",227); /* 227 is the ASCII code for Pi's symbol */
	
	for(i=0;i<sizeof(pi)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",pi[i].num,pi[i].den);
		
		while(pi[i].den != 0){
			printf(" %d ",r2cf(&pi[i].num,&pi[i].den));
			}
	}
	
	
	
	return 0;
}


  

You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the dc programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Zeckendorf arithmetic step by step in the 11l programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the Elixir programming language
You may also check:How to resolve the algorithm Arrays step by step in the NS-HUBASIC programming language