-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgaussExpansionSum.c
92 lines (82 loc) · 2.24 KB
/
gaussExpansionSum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* File: gaussInfiniteExpansionSum.c
* Author: Mudathir Lawal
* Email: mudathir.lawal@yahoo.com
* GitHub: github.com/mudathirlawal
* Last Edit: 3rd October, 2018.
* Version: 1.1 EN
*
* Purpose: To output the sum of the first n terms
* of Gauss's infinite series expansion.
* The question named gaussQuestion.png
* is attached herewith.
*/
#include <stdio.h>
#include <math.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE -1
int getFactorial ( int number );
int getPower ( double fbaseNum, int iexponent );
double Gauss ( int CONSTANT, int noOfTerms );
// main()
int main ( void )
{
Gauss ( 2, 39 );
return EXIT_SUCCESS;
}
// Gauss()
double Gauss ( const int CONSTANT, int noOfTerms )
{
double a, b, c, d;
int nthTerm = 0;
double expansionSum;
printf ( "\n" );
while ( nthTerm <= ( noOfTerms - 1 ) )
{
a = getPower ( -1, nthTerm );
b = ( nthTerm + 1 );
c = getPower ( CONSTANT, nthTerm );
d = getFactorial ( nthTerm );
expansionSum = ( ( a * b * c ) / d );
printf ( "\t( %.0fx ^ %i ) / %i! ", ( a * b ), nthTerm, nthTerm );
nthTerm = nthTerm + 1;
printf ( "\tnthTerm: %i; expansionSum: %f\n", nthTerm, expansionSum );
}
printf ( "\n" );
printf ( "\tNotice that Gauss' Infinite Expansion terminates after the 32nd term.\n" );
printf ( "\texpansionSum: %f\n\n", expansionSum );
return expansionSum;
}
// getPower()
int getPower ( double fbaseNum, int iexponent )
{
int loopVariable = 0;
int ibaseNum = ( int ) fbaseNum;
int product = ibaseNum;
short loopEntryFlag = ( loopVariable <= ( iexponent - 2 ) );
while ( loopEntryFlag )
{
loopVariable = loopVariable + 1;
loopEntryFlag = ( loopVariable <= ( iexponent - 2 ) );
product = product * ibaseNum;
}
//printf ( "\n\t%i ^ %i = %i\n\n", ibaseNum, iexponent, product ); //A USEFUL SCAFFOLDING
return product;
}
// getFactorial ()
int getFactorial ( int number )
{
int countStart = 1;
int countEnd = number - 1;
double deFactorial = number;
//printf ( "\n" );
while ( countStart <= countEnd )
{
number = number - 1;
deFactorial = deFactorial * ( number );
countStart = countStart + 1;
//printf ( "\tFactorial = %.0f\n", deFactorial );
}
//printf ( "\n\tFactorial = %.0f\n\n", deFactorial );
return deFactorial;
}