CS 630 - Computing Fundamentals I - Fall 2005
Homework 4
Loyola College >
Department of Computer Science >
CS 630 >
Homework >
Homework 4
Due
Monday, October 10th at the beginning of class
Problems
Exercise (pp. 152-153)
3.2
3.3
3.7
(4) Using Math.Random write a statement that will produce a number between the following endpoints:
(a) 0 to 10
(b) 1 to 10
(c) -10 to 15
Programming Project
3.3 (pg. 153)
Factorial Approximation (myCodeMate DL0099)
Objectives
- to use functions from the Java Math class
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
This is the square root of 2 pi.
Eric Weisstein's World of Mathematics
has a page on
Stirling's Approximation.
Assignment
Create a program called FactorialApprox that prompts the
user to enter an integer n,
calculates the lower and upper bounds for n! using Stirling's
Approximation, and then outputs those bounds.
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 should give exact answers for 1 through 7. At some point
beyond 100 it will likely overflow; do not worry about this.
- 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 Java's precedence and type rules. (Ex: integer vs. double division)
- You can use the expression Math.PI for the value of pi.