This material is on sequences defined by induction, where previous numbers in the sequence are used to define new numbers in the sequence. It will give you the opportunity to code in the C++ style, and use loops and variables in your C++ program.
Definition.
One related to (the so-called Golden Section) is the sequence where , and .
Obviously, if you know that is enough to calculate . Therefore knowing is going to be enough to calculate the whole sequence. What's more, we can do this with a computer program and a fixed number of variables since we never have to remember more than the last value.
Definition.
A similar looking sequence is where , and
Definition.
If we use a constant the two preceding sequences can be combined into one definition where , and
Note that is where and is where .
Definition.
Another interesting sequence is defined using a constant . Define by , , and .
The case of the sequence is similar. It can be calculated by remembering and the two last values.
Here is a C++ program to calculate values of .
#include <iostream> using namespace std; int main() { // an example value for the parameter beta double beta = 0.8660254037844386467637; int maxn; cout << "maximum index? "; cin >> maxn; double c = 0; // current value double c_prev = beta; // previous value double c_prev_prev = 1.0; // one before that cout << "c_0" << " == " << c_prev_prev << endl; cout << "c_1" << " == " << c_prev << endl; int n = 1; while (n < maxn) { n = n + 1; c = 2*beta*c_prev - c_prev_prev; cout << "c_" << n << " == " << c << endl; c_prev_prev = c_prev; c_prev = c; } return 0; }
You can copy it and compile it. The value used here for beta is which is (to sufficiently high accuracy) about 0.8660254037844386467637. You can try other values of such as 0, 1, 0.5, but values between -1 and +1 might be best. (But why? try others too.)
Exercise.
(Possibly tricky, unless you spot a good idea.) Can you give an expression for the value for a given value in the range from to ?
Exercise.
Program the sequence in the same way.
Exercise.
Do the sequences and/or converge? If so, to what?
Note: you can if you want change the program to accept a value beta (or alpha) from the user when the program is run. For example,
double beta = 1; cout << "Please give value for beta: " ; cin >> beta;
Definition.
The Fibonacci numbers are defined as the real numbers where , and for all .
So according to this definition the Fibonacci number with subscript is , the Fibonacci number with subscript is , the Fibonacci number with subscript is , and so on with values .
(Note that there is a common alternative definition with . These definitions are obviously very similar but incompatible. Please use mine.)
Here are some variants on the Fibonacci numbers.
Definition.
The Tribonacci numbers are the numbers , i.e. the case of in the sequence where , , and for all .
Definition.
The Padovan sequence is that of the numbers , i.e. the case of in the sequence where , , and for all (so the sequence temporarily "forgets about" the last value).
Definition.
The Perrin sequence has the same recurrence relation as the Padovan sequence but starts with , , .
Remark.
In all cases here, there is some variation where the sequence "starts" i.e. which value gets the index zero. Use my definitions please. They are not the only ones but seem to be at least as good as any other.
This is your opportunity to use C++ directly, without using any special macros for register machines, etc.
For the first two exercises, you might want to start with my program for as a model. Be careful to follow the instructions carefully, using int or double where required. Quiz questions ask about these, so it is worth saving these programs separately.
Exercise.
Write a program to make a table of Fibonacci numbers, similar to the program given for the . Use int variables to store the numbers. You will of course need to save the previous two Fibonacci values as well as the current one (that's three int variables, as well as variables for the index and the maximum index). Save your program as fib_int.cpp and test it.
Exercise.
Modify your program for Fibonacci numbers so that it uses double variables to store the numbers. (it will continue to use int variables for the index and the maximum index). Print to 20 significant figures using cout.precision(20);. Save your program as fib_double.cpp and test it.
It is all very well, running programs and getting them to type data to the console. However, for assessment purposes this really doen't work. The problem is I'd have to say exactly what to type, and it would have to be exactly rigt to the letter for my program to correctly read your program's answer. In my experience, only about 50% of students will successfully carry out that to the level of detail needed (no spelling errors, or blank lines, or spurious blank characters, no data swapped or given in the wrong order, etc.)
C++ has a mechanism to define and run functions and this is a much better way to do input and output and get two programs talking to one another. I am not expecting you to write your own functions this week (but you will do so next week) so instead I have provided an auxillary file, sa3.h, which you can download and save in the working folder that you are using, next to your main .cpp file, just like you did for register machines. It's a fully fledged set of functions for input/output for your program, not a set of macros, but you can use it just like "regmac.h". It will be explained in the lectures.
In both cases, for the Tribonacci numbers or the Padovan sequence, you should use "int" for the index variable(s) and "double" for all other real values.
Example.
When this example calculates and prints the th triangle numbers 0,1,3,6,10,.... The general formula is given by and . It also computes the ratio of the th and st such numbers. You should find that this ratio tends to . (Why?)
#include <iostream> #include "sa3.h" using namespace std; int main() { double gamma = 0; int maxn; // the next command gets the value of gamma get_gamma_value( gamma ); // the next command gets the value of maxn get_final_index( maxn ); int n = 0; double a = gamma; double aprev = 0.0; // "previous value" while ( n < maxn ) { // compute a_{n+1} from a == a_n and n aprev = a; a = aprev + n + 1; // update n n = n + 1; } // print a_n, the nth triangle number print_answer( n, a ); // print a_n/a_{n-1} or a/aprev which is // the ratio of consecutive triangle numbers // note the order: n then a_{n-1} then a_n print_ratio( n, aprev, a ); return 0; }
The idea is that if you use the four special commands shown for input and output then your program can be tested correctly on another computer. It also means that you can put as many additional cout << ... statements in your program as you need and this will not get in the way of the testing. (But do not use any cin >> ... statements and make sure you use the four special commands in sa3.h in exactly the way used here, only once for each.)
Exercise.
Modify the programs you have already got to program the Tribonacci numbers with an arbitrary parameter using the commands in sa3.h as shown. Investigate the limit of the ratio of consecutive terms for different values of , especially for .
Exercise.
Modify the programs you have already got to program the Padovan numbers with an arbitrary parameter using the commands in sa3.h as shown. Investigate the limit of the ratio of consecutive terms for different values of , especially for .
The assessment will ask you to submit one of these programs. Make sure you read it and follow the instructions and specification exactly.
Always test all your programs carefully with as many possible input values you can think of, checking that the program gives answers that you expect (or at least can explain). Use a calculator or any other mathematics to check your program's output.
This was your first encounter with C++ in the official style. You should have learnt a lot about C++ in this exercise. In particular you have learnt about,