Sequences (CSCI 2824, Spring 2015)In this lecture we will cover the basic notation for sequences and summation. Concepts learned:
SequencesWe have seen this puzzle. Some one gives you a sequence of numbers and asks you to guess the next number. Often it is easy enough to do that. Sometimes, it can get really tricky! Guess the next number(s) in the following sequence:
A common trick (a lot of IQ tests use) is to mix two simple ones into a seemingly complex one: How about this one? Working hard on a sequence, try this cool site: Online Encylopedia of Integer Sequences! :-) The underlying theme of ‘‘guessing the next number’’ puzzles is pattern recognition. Mathematical language is a useful tool to help us reason when it comes to solving such puzzles. So how do we talk about sequences mathematically? FormalitiesWe use the notation We usually call an element
Ways to specify sequencesOften times, sequences come in one of these two common forms:
Mathematically, these two different forms specify a sequences in two different ways:
We will now give examples to illustrate both ways in detail starting with the recurrence for a sequence. Recursive FormulaeEach recursive formula or recurrence has two parts to it.
This will be clear once we work through some examples. Let us start with some examples of recurrences. Example-1 (Even Numbers)We have the following recurrence for even numbers: with the base case: Do not forget the base case and note that recurrence holds only for n >= 1 We can think of a recurrence as a simple recursive program. Here is how the recurrence above translates into C/C code: Code for Recurrence Relation
/* Recursive formula is implemented by a recursive function */ int A( int n) { assert(n >= 0); if ( n == 1) return 2; return A(n-1) + 2; } Question: What happens to the code above, if we forgot the base case? We saw an example where Example 2: Fibonacci NumbersFibonacci Numbers were invented by the famous Italian mathematician Leonardo Fibonacci. Interestingly these numbers turn up all over the place, often for reasons that are not well understood. They are closely related to the golden ratio. For more Fibonacci-ology see the Wikipedia Article. Fibonacci numbers can be written as the recurrence: We have to give two base cases: Question to class: Why do we need to specify Notice that since we have to give base cases for If you think in code (like me), then consider this very simple piece of code to compute the Fibonacci numbers: Fibonacci Number Code
/* CS majors: Coding up Fibonacci numbers is a common job interview (trap) question */ /* If you are ever challenged to do so, do not write this code below. */ int F ( int n){ assert ( n >= 1); if (n == 1) return 1; if (n == 2) return 1; return F(n-1) + F(n-2); } Delete the ’'if’’ statement that handles the base case for Coding Question: The code above is a really really inefficient way to compute Fibonacci numbers. Can you say why?
Hint Try computing A More Complex RecurrenceLet us try a more complex recurrence (more complex than the ones we have seen so far, at least): Base Cases for the RecurrenceWhat bases cases should be specified for this recurrence? To answer this, let us ’'run’’ this recurrence for some values of Compute Now let us expand We have no rule for expanding Compute Again, we have no rule for expanding Putting it togetherwith the base cases given by We obtain the sequence: Looks complicated, but it is actually two sequences ’'zipped’’ together. Collatz RecurrenceThis is one more famous (and crazy!!) recurrence defined as follows: Base case is: Claim: No one has been able to prove it. To see why let us try an example: Let us try one more: Here is the code for printing out the the sequence of indices encountered through the recurrence above: Collatz's Recurrence
#include <stdio.h> int C (int n) { assert ( n >= 1); printf ("Collatzing through %d \n", n); if (n == 1) return 1; /* n is even? */ if (n %2 == 0) return C(n/2); /* n is odd */ return C( 3 * n + 1); } The claim above can be interpreted as saying that Collatz's recurrence code above always halts and outputs
Exercise: Try running the code above for Random Number GeneratorHere is an interesting fact: sequences defined using recurrence relations are used inside computers to generate pseudo random numbers. The most common sequence used is a Linear Congruential Generator. Ill Defined (Nonsensical) RecurrencesNote that we have carefully avoided the topic of “well-definedness” of a recurrence. We provide a quick introduction here. It turns out that not all recurrences or recursive formulae are well defined. A formula defining Some of them are plain ’'nonsense’’. Here are some examples. Can you spot the problems? 1. 2. As a good rule of thumb: a recurrence is ill defined if writing it out as a program in CCJava or whatever, causes the code
to go into an infinite loop for some number Diversionary Note: Finding out if a recurrence is well-defined is a really hard problem (it is equivalent to the halting problem for computers). Closed Form RepresentationAnother way of representing sequences is through the closed form. In the closed form
representation, we simply express Here is the closed form representation for the sequence of even numbers: Simple, no fuss. However, closed form representations are not that easy to get always. Let us try some examples. SquaresConsider the recurrence for with base case Let us try computing some values. The closed form for this recurrence is In a couple of weeks, we will look at induction proofs of this fact Fibonacci NumbersClosed forms are not always ’'simpler’’ than the original recurrences. Recall the recurrence for Fibonacci numbers: with base case The closed form for the You will be asked to prove this fact. In fact, you will find that it is not all that hard once we really get going in this course!! Here is the cool fact. The number has a special name. It is called the Golden Ratio. Among other things, the rectangle whose sides are in the golden ratio seems to be the most ’'aesthetically pleasing’’ to the human eye!! Using this fact, we can write Fibonacci numbers as: Diversionary Note: We can use this formula to write a computer program that is truly efficient for
computing See the GeeksforGeeks page for a discussion on various methods of computing Fibonacci numbers! Sequences that Cannot be Represented EasilyIt needs to be mentioned that there are many sequences that are not easily represented either as a closed form or as a recurrence. The sequence of prime numbers is one such sequence. We do not have a ’'easy’’ closed form for the
|