Loops (for, while and do-while) and Several Related Algorithms
This lab is to gain competency in using loops to design programs. We shall complete the following problems. Due to the time limit of the lab, it is expected that you complete some of these on your own.
- Printing a multiplication table
- Printing number pyramid
- Finding the Greatest Common Divisor
- Finding the root of an equation
- Simple Monte Carlo simulations to esimate
- Checking whether a String is a Palindrome
- Checking if a number is a Prime number
- Converting numerals from one system to another (Project 4 – skipped)
Printing Multiplication Table.
You are to create a program called MultiplicationTable
that prints out a multiplicaion table on the standard
output. The table shall be formatted according to the example below,
$ java MultiplicationTable
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0 0
1 0 1 2 3 4 5 6 7 8 9
2 0 2 4 6 8 10 12 14 16 18
3 0 3 6 9 12 15 18 21 24 27
4 0 4 8 12 16 20 24 28 32 36
5 0 5 10 15 20 25 30 35 40 45
6 0 6 12 18 24 30 36 42 48 54
7 0 7 14 21 28 35 42 49 56 63
8 0 8 16 24 32 40 48 56 64 72
9 0 9 18 27 36 45 54 63 72 81
Printing Number Pyramid
You are to create a program called PowerNumberPyramid
that print out a number pyramid whose height is entered
by the user. An example of the pyramid is as follows:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
where each number is a power of 2.
Finding the Greatest Common Divisor
Write a program called GreatestCommonDivisor
that prompts the user to enter two positive integers and finds their greatest common divisor. A simple albet an inefficient algorithm to solve this problem is as follows:
Let the two input integers be n1 and n2. You know number 1 is a common divisor, but it may not be the greatest commons divisor. So you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2, until k is greater than n1 or n2
The following are several test runs of the program:
$ java GreatestCommonDivisor
Enter two integers: 36 18
The greatest common divisor of 36 and 18 is 18.
$ java GreatestCommonDivisor
Enter two integers: 24 36
The greatest common divisor of 24 and 36 is 12.
$ java GreatestCommonDivisor
Enter two integers: 24 27
The greatest common divisor of 24 and 27 is 3.
$ java GreatestCommonDivisor
Enter two integers: 21 28
The greatest common divisor of 21 and 28 is 7.
$ java GreatestCommonDivisor
Enter two integers: 96 128
The greatest common divisor of 96 and 128 is 32.
$ java GreatestCommonDivisor
Enter two integers: 3 7
The greatest common divisor of 3 and 7 is 1.
The algorithm given above is a simple but inefficient one. An efficient algorithm that computes the greatest common divisor of two numbers was first described by Greek mathematician Euclid circa 300 BC. Those who are interested may want to search “Euclidean Algorithm” on the Web.
Finding the Root of an Equation
Suppose that the tuition for a university is 10,000 dollars this year and tuition increases 7% every year. In how many years will the tuition be doubled? This is in fact a root finding problem, i.e., find such that . For this problem, we need to find , such that, . A simple solution to this problem is as follows:
Compute tuition repeatedly for year 1, 2, …, until the tuition is greater than or equal to 20,000 dollars.
For this problem, you shall develop a program called TuitionPredictor
that prompts the user for an initial
tuition and an annual percentage rate of interest, determine in how many years the tuition would double, and
print the reuslt on the standard output. The following are several test runs of the programs:
$ java TuitionPredictor
Enter tuition and annual percentage rate: 10000 7
Given tuition 10000 and APR 7.00%, the tuition would double in 11 years.
$ java TuitionPredictor
Enter tuition and annual percentage rate: 20000 3.2
Given tuition 20000 and APR 3.20%, the tuition would double in 23 years.
$ java TuitionPredictor
Enter tuition and annual percentage rate: 15000 2.1
Given tuition 15000 and APR 2.10%, the tuition would double in 34 years.
$ java TuitionPredictor
Enter tuition and annual percentage rate: 9000 2
Given tuition 9000 and APR 2.00%, the tuition would double in 36 years.
$ java TuitionPredictor
Enter tuition and annual percentage rate: 10000 5.5
Given tuition 10000 and APR 5.50%, the tuition would double in 13 years.
$ java TuitionPredictor
Enter tuition and annual percentage rate: 12000 10
Given tuition 12000 and APR 10.00%, the tuition would double in 8 years.
Estimating using Simple Monte Carlo simulations
Write a program called PiEstimator
to estimate using a simulation.
The simulation is a Monte Carlo simulation where we use random numbers and probability
to solve the problem. Let’s consider
that we generate random points within a square, where each random point is represented by two random
numbers (x, y). The square bounds a circle. The proportion of the random points within the circle bounded
by the square is,
where is the number of random points within the bounded circle, the number of points within the square, the area of the circle is , and the area of the square is . Let’s assume we draw random x and y from interval , the bounded circle is thus a unit circle, and the bounding square is a unit square, then we have
It follows that,
Finally, we can estimate ,
Given this, the algorithm can be described as follows:
- Let be
- Set to 0
- repeat for times:
- generate a random number in for
- generate a random number in for
- if is within the bounded circle
- increment
- estimate
- print out the result on the standard output.
The following is a test run of the program:
$ java PiEstimator
The estimated PI is 3.14 after 10000000 random points.
Checking whether a String is a Palindrome
The problem is to write a program that prompts the user to enter a string and reports whether the string is a palindrome. A string is a palindrome if it reads the same forward and backward. The words “mom”, “dad”, “noon”, for instance, are all palindromes.
The problem is to write a program called CheckingPalindrome
that prompts the user to enter a string and reports whether the string is a palindrome. You may follow the algorithm below:
- Check whether the first character in the string is the same as the last character. If so, check whether the second character is the same as the second-to-last character.
- This process continues until a mismatch is found or all the characters in the string are checked, except for the middle character if the string has an odd number of characters.
The following are several test runs of the program:
$ java CheckingPalindrome
Enter a string (one string per line):
Is String <> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line):
Is String < > a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line):
Is String < > a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): a
Is String <a> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): dad
Is String <dad> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): moon
Is String <moon> a palindrone? false
$ java CheckingPalindrome
Enter a string (one string per line): cat
Is String <cat> a palindrone? false
$ java CheckingPalindrome
Enter a string (one string per line): da
Is String <da> a palindrone? false
$ java CheckingPalindrome
Enter a string (one string per line): moom
Is String <moom> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): lilil
Is String <lilil> a palindrone? true
$ java CheckingPalindrome
Enter a string (one string per line): liljl
Is String <liljl> a palindrone? false
Printing First 50 Prime Numbers
Write a program called PrintPrimes
that displays the first 50 prime numbers in five lines, each of which contains 10 numbers. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not.
Noting the first prime number is 2, we can break the problem into the following big steps:
- For number = 2, 3, 4, 5, 6, …,
- test whether the number is prime.
- count the prime numbers.
- print 10 prime numbers per line.
The following is a test run of the program:
$ java PrintPrimes
2 3 5 7 11 13 17 19 23 29
31 37 41 43 ...
...
Not to give all the 50 prime numbers, only 1st line and part of the 2nd line are shown in the above. Also note that to have a well-aligned output, we set the field width for each number as 4, separated by a space.