CS 1073 Introductory Programming
for Scientific Applications
Homework 2: Constants


Due Date:


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:

NameJavaValue
eMath.E 2.718281828459045
piMath.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:


List of constants and algorithms: Here are the 5 algorithms that you should use for this assignment.

  1. e -- the base of natural logarithms:

  2. pi -- the ratio of circumference to diameter of a circle:

  3. pi -- the ratio of circumference to diameter of a circle:

  4. phi -- the golden ratio = (1/2)(1 + sqrt(5)):

  5. pi -- the ratio of circumference to diameter of a circle:


What to turn in: Turn in computer printed copy containing:
  1. Source listings (the Java code) for each of the 5 programs.

  2. 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,...):