CS 1073 Introductory Programming
for Scientific Applications
Homework 2: Constants
|
Due Date:
Wednesday, 29 September 2004, at start of class.
Overview:
This homework asks you are to calculate three different
mathematical constants using loops and several algorithms. The idea is to not use any fancy
library functions (except for Math.sqrt
in parts 2 and 5 below),
but to calculate the constants "from scratch".
In fact the constants you are to calculate below are
readily available in Java as:
| Name | Java | Value |
| e | Math.E |
2.718281828459045 |
| pi | Math.PI |
3.141592653589793 |
| phi | (0.5)*(1.0 + Math.sqrt(5.0)) |
1.618033988749895 |
We would like to see how many iterations it takes to get 15 digits of
accuracy. You can use the following examples as models:
General directions:
- There are 5 calculations below, so you should produce 5 short Java programs.
- In each case, at the end of the programs you should print
the final approximation to the constant,
either e,
pi, or
phi.
- Each of the formulas calls for infinitely
many terms, but in each case you need to use just some finite number of terms.
- In each case you should use the smallest number of iterations needed
to produce roughly 15 digits of accuracy in the constant.
This "smallest number" is an interesting result of each part, since it
shows how rapidly the method converges to the constant.
- Each program should print the intermediate results obtained at each interation
of the algorithm.
List of constants and algorithms:
Here are the 5 algorithms that you should use for this assignment.
- e -- the base of natural logarithms:
- Use the following formula:

- Hint:
-
pi -- the ratio of circumference to diameter of a circle:
- Use the following formula:

- Hint: Pretty much the same as the previous one. However, it doesn't converge
nearly as fast as I thought it would. For this series, you should determine how many
terms are needed to get pi accurate to 3 significant
digits, that is: 3.14.
-
pi -- the ratio of circumference to diameter of a circle:
- Use the following formula:

- Hint: Don't panic; it's not really very complicated.
-
phi -- the golden ratio = (1/2)(1 + sqrt(5)):
Now write p4 in terms of p3, that is p4 = some expression
involving p3.
Write p3 in terms of p2, that is p3 = some expression
involving p2.
Write p2 in terms of p1, that is p2 = some expression
involving p1.
Write p1 in terms of p0, that is p1 = some expression
involving p0.
Do you see the simple pattern?
-
pi -- the ratio of circumference to diameter of a circle:
What to turn in:
Turn in computer printed copy containing:
- Source listings (the Java code) for each of the 5 programs.
- Results of runs for each of the 5 programs.
Note: You should use netBeans, though it is permissible to use Java from
another source. In netBeans, the simplest way to make up the material to
print is to just copy from netBeans (using ctrl-C) and paste into
Word or WordPad (using ctrl-V). Then print the resulting document.
You can assemble all 5 programs into a single document to print.
In case you have written a program for one part, but it doesn't produce
output or doesn't produce the correct output, you should still turn
in a listing for part credit.
Extras for Experts:
For those bored with this assignment, here is another equation for
pi:

Here is another equation for e (here the pattern
continues as 2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1,...):
