CS 630 - Computing Fundementals I - Spring 2004
Problem 1 - Math Functions
Loyola College >
Department of Computer Science >
CS 630 >
Homework >
Problem 1
Objectives
- to use C++ Math functions
Introduction
For any integer n > 0, n! is defined as the product
1 * 2 * ... * n. It is sometimes useful to have a closed-form
instead; for this purpose an approximation can be used. In 1730 J. Stirling
came up with such an approximation, which can be expressed as
Eric Weisstein's World of Mathematics
has a page on
Stirling's Approximation.
Assignment
Create a program called FactorialApproximator that prompts the
user to enter an integer n,
calls functions which calculate the lower and upper bounds for n! using Stirling's
Approximation, and then displays those bounds in the terminal.
The message should look something like this
(of course, the actual numbers will depend on the user's input):
1.551104102634359E25 <= 25! <= 1.5511212799620605E25
Hints
- Your program will be easier to debug if you use some intermediate
values instead of trying to compute the bounds in a single expression.
If you are not getting the correct results then you can display the
results of your intermediate values and compare them to what you get
when you do the calculations by hand.
- Pay heed to C++'s precedence and type rules. (Ex: integer vs. double division)
- You can use the following function to for the value of pi (Cut and paste this code into your program).
double pi() {
double series = 0;
int sign = 1;
for (int i = 0, j = 1; i < 100; i++, j+=2) {
series += sign * 1 / (pow(3, i) * j);
sign *= -1;
}
double result = 2 * sqrt(3) * series;
return result;
}