// MaclaurinSeries.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
// Copyright (c) Saturday, October 31, 2026
// by James Pate Williams, Jr., BA, BS, MSwE, PhD
#include <cmath>
#include <iomanip>
#include <iostream>
// iterative n-factorial function
static double iFactorial(int n)
{
double factorial = 1.0;
for (int i = 2; i <= n; i++)
factorial *= i;
return factorial;
}
static double Series1(double x, int n)
{
double x2 = x * x, x4 = x2 * x2, x6 = x2 * x4;
switch (n)
{
case 0:
return 1.0;
case 1:
return 1.0 - x2;
case 2:
return 1.0 - x2 + 9 * x4 / iFactorial(4);
case 3:
return 1.0 - x2 + 9 * x4 / iFactorial(4) - 225 * x6 / iFactorial(6);
case 4:
return 1.0 - x2 + 9 * x4 / iFactorial(4) - 225 * x6 / iFactorial(6) +
10825 * x6 * x2 / iFactorial(8);
default:
return 0.0;
}
}
static double Series2(double x, int n)
{
double y = x - 1;
double y2 = y * y, y3 = y * y2, y4 = y * y3, y5 = y * y4;
double y6 = y * y5, y7 = y * y6, y8 = y * y7, y9 = y * y8;
double y10 = y * y9;
switch (n)
{
case 0:
return log(1.0);
case 1:
return log(1.0) + y;
case 2:
return log(1.0) + y - y2 / iFactorial(2);
case 3:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3);
case 4:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3)
- 6.0 * y4 / iFactorial(4);
case 5:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3)
- 6.0 * y4 / iFactorial(4) + 24.0 * y5 / iFactorial(5);
case 6:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3)
- 6.0 * y4 / iFactorial(4) + 24.0 * y5 / iFactorial(5)
- 120.0 * y6 / iFactorial(6);
case 7:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3)
- 6.0 * y4 / iFactorial(4) + 24.0 * y5 / iFactorial(5)
- 120.0 * y6 / iFactorial(6) + 720.0 * y7 / iFactorial(7);
case 8:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3)
- 6.0 * y4 / iFactorial(4) + 24.0 * y5 / iFactorial(5)
- 120.0 * y6 / iFactorial(6) + 720.0 * y7 / iFactorial(7)
- 5040.0 * y8 / iFactorial(8);
case 9:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3)
- 6.0 * y4 / iFactorial(4) + 24.0 * y5 / iFactorial(5)
- 120.0 * y6 / iFactorial(6) + 720.0 * y7 / iFactorial(7)
- 5040.0 * y8 / iFactorial(8) + 40320 * y9 / iFactorial(9);
case 10:
return log(1.0) + y - y2 / iFactorial(2) + 2.0 * y3 / iFactorial(3)
- 6.0 * y4 / iFactorial(4) + 24.0 * y5 / iFactorial(5)
- 120.0 * y6 / iFactorial(6) + 720.0 * y7 / iFactorial(7)
- 5040.0 * y8 / iFactorial(8) + 40320 * y9 / iFactorial(9)
- 362880 * y10 / iFactorial(10);
default:
{
double sign = 1.0, sum = log(1.0);
for (int i = 1; i <= n; i++)
{
sum += sign * pow(y, i) / i;
sign *= -1.0;
}
return sum;
}
}
}
int main()
{
std::cout << "n" << '\t' << "n!" << std::endl;
for (int n = 0; n <= 10; n++)
std::cout << n << '\t' << iFactorial(n) << std::endl;
double x = 0.015;
std::cout << std::fixed << std::setprecision(10);
std::cout << "cos(arctan(x)) = (1 + x^2)^-1/2" << std::endl;
std::cout << "Maclaurin Series Expansion of (1 + x^2)^-1/2" << std::endl;
std::cout << "# is the number + 1 of nonzero terms in the summation\r\n";
std::cout << "x = " << x << std::endl;
std::cout << "#" << '\t' << "Series" << "\t\t" << "(1 + x^2)^-1/2";
std::cout << std::endl;
for (int n = 0; n <= 4; n++)
{
std::cout << n << '\t';
std::cout << std::fixed << std::setprecision(10);
std::cout << Series1(x, n) << '\t';
std::cout << pow(1.0 + x * x, -0.5) << std::endl;
}
x = 0.125;
std::cout << "Maclaurin Series Expansion of (1 + x^2)^-1/2" << std::endl;
std::cout << "x = " << x << std::endl;
std::cout << "#" << '\t' << "Series" << "\t\t" << "(1 + x^2)^-1/2";
std::cout << std::endl;
for (int n = 0; n <= 4; n++)
{
std::cout << n << '\t';
std::cout << std::fixed << std::setprecision(10);
std::cout << Series1(x, n) << '\t';
std::cout << pow(1.0 + x * x, -0.5) << std::endl;
}
x = 0.5;
std::cout << "Maclaurin Series Expansion of (1 + x^2)^-1/2" << std::endl;
std::cout << "x = " << x << std::endl;
std::cout << "#" << '\t' << "Series" << "\t\t" << "(1 + x^2)^-1/2";
std::cout << std::endl;
for (int n = 0; n <= 4; n++)
{
std::cout << n << '\t';
std::cout << std::fixed << std::setprecision(10);
std::cout << Series1(x, n) << '\t';
std::cout << pow(1.0 + x * x, -0.5) << std::endl;
}
x = 1.25;
std::cout << "Taylor Series Expansion of log(x)" << std::endl;
std::cout << "About x = 1" << std::endl;
std::cout << "x = " << x << std::endl;
std::cout << "#" << '\t' << "Series" << "\t\t" << "ln(x)";
std::cout << std::endl;
for (int n = 0; n <= 15; n++)
{
std::cout << n << '\t';
std::cout << std::fixed << std::setprecision(10);
std::cout << Series2(x, n) << '\t';
std::cout << std::log(x) << std::endl;
}
x = 1.5;
std::cout << "Taylor Series Expansion of log(x)" << std::endl;
std::cout << "About x = 1" << std::endl;
std::cout << "x = " << x << std::endl;
std::cout << "#" << '\t' << "Series" << "\t\t" << "ln(x)";
std::cout << std::endl;
for (int n = 0; n <= 30; n++)
{
std::cout << n << '\t';
std::cout << std::fixed << std::setprecision(10);
std::cout << Series2(x, n) << '\t';
std::cout << std::log(x) << std::endl;
}
return 0;
}
Author: jamespatewilliamsjr
My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.
View all posts by jamespatewilliamsjr