COS 126 Conditionals and Loops Practice Programming Exercises

These exercises are for practice with loops and conditionals—they are not a component of your course grade. Exercise 1 involves a single loop and the Math library; Exercise 2 involves nested loops and conditionals.

1. Pepys problem. In 1693, Samuel Pepys asked Isaac Newton which was more likely: getting at least one 1 when rolling a fair die 6 times or getting at least two 1s when rolling a fair die 12 times. Write a program Pepys.java that uses simulation to determine the correct answer.

This exercise will be live-coded during the class meeting.

2. Generalized harmonic numbers. Write a program GeneralizedHarmonic.java that takes two integer command-line arguments n and r and uses a for loop to compute the nth generalized harmonic number of order r, which is defined by the following formula:

$$H(n, r) = \frac{1}{1^r} + \frac{1}{2^r} + \cdots + \frac{1}{n^r}.$$
For example, $$H(3, 1) = \frac{1}{1^1} + \frac{1}{2^1} + \frac{1}{3^1} = \frac{11}{6} \approx 1.833333$$ and $$H(3, 2) = \frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} = \frac{49}{36} \approx 1.361111$$.
 % java GeneralizedHarmonic 1 1 1.0 % java GeneralizedHarmonic 2 1 1.5 % java GeneralizedHarmonic 3 1 1.8333333333333333 % java GeneralizedHarmonic 1 2 1.0 % java GeneralizedHarmonic 2 2 1.25 % java GeneralizedHarmonic 3 2 1.3611111111111112

Note: you may assume that n is a positive integer.

Hint: use Math.pow(x, y) to raise x to the power y.

Remark: the generalized harmonic numbers are closely related to the Riemann zeta function, which plays a central role in number theory.

3. Band matrices. Write a program BandMatrix.java that takes two integer command-line arguments n and width and prints an an n-by-n pattern like the ones below, with a zero (0) for each element whose distance from the main diagnonal is strictly more than width, and an asterisk (*) for each entry that is not. Here, distance means the minimum number of cells you have to move (either left, right, up, or down) to reach the main diagonal.
 % java BandMatrix 8 0 * 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 * %java BandMatrix 8 1 * * 0 0 0 0 0 0 * * * 0 0 0 0 0 0 * * * 0 0 0 0 0 0 * * * 0 0 0 0 0 0 * * * 0 0 0 0 0 0 * * * 0 0 0 0 0 0 * * * 0 0 0 0 0 0 * * % java BandMatrix 8 2 * * * 0 0 0 0 0 * * * * 0 0 0 0 * * * * * 0 0 0 0 * * * * * 0 0 0 0 * * * * * 0 0 0 0 * * * * * 0 0 0 0 * * * * 0 0 0 0 0 * * * % java BandMatrix 8 3 * * * * 0 0 0 0 * * * * * 0 0 0 * * * * * * 0 0 * * * * * * * 0 0 * * * * * * * 0 0 * * * * * * 0 0 0 * * * * * 0 0 0 0 * * * *

Note: you may assume that n and width are nonnegative integers.

Hint 1: Do not use arrays.

Hint 2: Devise an expression that determines the distance of element (i, j) from the main diagonal. For reference, the following diagram illustrates the distance of each element in an 8-by-8 matrix. Solution to hint.

Remark: band matrices are matrices whose nonzero entries are restricted to a diagnonal band. They arise frequently in numerical linear algebra.

Submission. Submit the file GeneralizedHarmonic.java and BandMatrix.java.