Computer Science Courses
Related sites
HW 2 |
This problem was a part of the year 2007 final examination paper.
The bunch of non-negative integer numbers written in a shapeof the triangle:
is called the Euler–Bernoulli triangle and can be constructedusing the following algorithm:
The importance of this construction lies in the fact, that all non-zero numbers on the left side of the triangle represent the so-called Euler numbers, while all non-zero numbers on the right side are the so-called Bernoulli numbers. Both are important sequences in mathematics, and the described triangle gives the simplest algorithm to calculate them.
Your task is to write a program which will calculate the first n rows of the Euler–Bernoulli triangle. You may use a two-dimensional integer array, and your program will assign its elements values according to the above algorithm. Do not write code which will print the triangle as it's done above (there are formatting problems when the numbers become multi-digit). However, using the constructed array, make your code to print all Euler and Bernoulli numbers contained in the calculated triangle...
Perhaps, this problem was a bit too complex for the exam (so I will nottry something equally daunting this time), but this is not what you haveto do in this Homework. The thing is that I had not been careful and had givenone student a code with my solution. So you can all have it,.
Download it and study. Compile and run it. It should be done as follows:
> java EulerBernoulliTriangle 21
where the command line argument, 21 in the above example, must be an odd positivenumber greater than 2. The output will look as follows:
> java EulerBernoulliTriangle 21 The height of the triangle is 21The Euler numbers:115611385505212702765199360981193915121452404879675441That's 10 Euler numbersThe Bernoulli numbers:112162727936353792223682561903757312209865342976That's 10 Bernoulli numbers
But attempts to calculate larger Euler and Bernoulli (EB) numbers soon runinto a problem: if you use the value of command line argument 27 and higher,you will see that the EB stop growing and even become negative (which shouldn'thappen — check the algorithm for their calculation). This is theconsequence of the finite range of long primitive type which I usedin my program.
Your task is to modify the above program so it can calculate the EB numbersof arbitrary order. You should replace the long type onto a differentone (which?!) and make appropriate changes in other places of the code.
Test the correctness of your output by comparing it with the Euler numbersfrom the picture:
Hint: look up the Java API docs, the package java.math.
You will get up to two marks, if you present a solution to the Homeworkexercise during the next week lab.
HW 2 |
Updated: Sun 12 Jun 2016 17:27:37 AEST • Responsible Officer: JavaScript must be enabled to display this email address. • Page Contact:
+61 2 6125 5111
The Australian National University, Canberra
CRICOS Provider : 00120C ABN : 52 234 063 906