CHAPTER 5

Sequences


Sequences (and series) are important concepts that appear all over the place. A sequence is just a list of numbers, such as:

   1, 2, 3, 4, 5, 19, 7, 8, 12
   20, 18, 16, 14, 12, 10, 8
   1, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001

The individual numbers or members of the sequence are called the terms of the sequence. Usually the sequence is created with some rhyme or reason, such as the last two above. The second one was created by using the formula 22 - 2*N for N = 1 to 7, while the last used 10/10N for N = 1 to 7. Your computer is good at creating sequences. The following simple program illustrates this. Insert your own formula (involving N) in the second line.

Listing 5.1

LIST

   10 REM equence generator
   20 MODE 1:COLOUR 3:PRINT ' TAB(11);"S
equence generator"':@%=10
   30 PRINT "This program produces seque
nces."'
   40 PRINT "Enter your formula involvin
g N, for     example"'
   50 PRINT "        (N*N-N+4/2"'
   60 INPUT "Formula involving N: ";N$
   70 DEF FNFormula(N)=EVAL(N$)
   80 COLOUR 1:PRINT '"Here are the firs
t ten terms:"':COLOUR 2
   90 FOR N=1 TO 10
  100  IF 38-POS<LEN(STR$(FNFormula(N)))
 THEN PRINT
  110  PRINT ;FNFormula(N);:IF N<10 THEN
 PRINT ", ";
  120 NEXT
  130 PRINT
  140 COLOUR 3:PRINT CHR$(7) ' TAB(10);"
Another go? Y or N ";
  150 REPEAT:G$=GET$:UNTIL G$="Y" OR G$=
"N"
  160 IF G$="Y" THEN RUN
  170 CLS:PRINT '"Bye for now.":END


RUN

           Sequence generator

This program produces sequences.

Enter your formula involving N, for     
example

        (N*N-N+4/2

Formula involving N: ?(N*N-N +10)/2

Here are the first ten terms:

5, 6, 8, 11, 15, 20, 26, 33, 41, 50

          Another go? Y or N 


Here are some sequences produced by this program for various different formulae. Can you see what formula was used in each case? Check your guess by inserting the formula in the above program. (The answers are given later on in this section and further examples are given in the subsequent sections.)

   (a)   1, 6, 11, 16, 21, 26, 31, 36, 41, 46
   (b)   1, 2, 4, 8, 16, 32, 64, 128, 256, 512
   (c)   1, 4, 9, 16, 25, 36, 49, 64, 81, 100
   (d)   1, 2, 4, 7, 11, 16, 22, 29, 37,46
   (e)   4, 4, 8, 12, 20, 32, 52, 84, 136, 220

The formulae for the first few sequences are not too difficult to determine. For (a) it is 5*N - 4, for (b) it is 2­N/2 while for (c) it is N*N. The fourth one (d) is not quite so easy to guess, it is (N*N - N + 2)/2. Finally, the formula for the fifth one (e) is impossible to guess unless you've seen it before, in fact it is:

   4*INT((0.5 + SQR(5)/2)­N - (0.5 - SQR(5)/2)­N)/SQR(5))

Here are some other formulae that you might like to try out.

   1 + (-1)­N
   N * (-1)­N
   INT( SIN(N) * 10)

Arithmetic sequences

An arithmetic sequence or arithmetic progression is a sequence in which each term of the sequence is the sum of the preceding term and a constant. Sequence (a) from the previous section is an example of an arithmetic sequence. The general formula for an arithmetic sequence is given by

   A + (N - 1)*D

where A is the first term of the sequence and D is the common difference. Here are some further examples of arithmetic sequences.

5, 10, 15, 20, 25, 30, 35, 40, 45, 50(A = 5, D = 5)
1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5(A = 1, D = 0.5)
0, 2, 4, 6, 8, 10, 12, 14, 16, 18(A = 0, D = 2)

The following program may help to you to analyse arithmetic sequences. You enter the first term of the sequence, the common difference and the number of terms required. Notice that a formula is not required since the computer does the calculation iteratively. In addition the program adds up all the terms in the sequence and gives you the answer.

Listing 5.2
LIST

   10 REM Arithmetic sequences
   20 MODE 1:COLOUR 3:PRINT ' TAB(10);"A
rithmetic sequences"':@%=10
   30 PRINT "This produces arithmetic se
quences."'
   40 PRINT "Enter the first term, the c
ommon differ-ence and the number of term
s required."'
   50 COLOUR 1:INPUT "First term: ";A
   60 INPUT '"Common difference: ";D
   70 REPEAT
   80  INPUT '"Number of terms: ";N
   90  IF N<1 OR N<>INT(N) THEN COLOUR 3
:PRINT '"Try again please.":COLOUR 1
  100 UNTIL N>0 AND N=INT(N)
  110 COLOUR 1:PRINT '"Here is the seque
nce:"':COLOUR 2
  120 TERM=A:SUM=0
  130 FOR I=1 TO N
  140  IF 38-POS<LEN(STR$(TERM)) THEN PR
INT
  150  PRINT ;TERM;:IF I<N THEN PRINT ",
 ";
  160  SUM=SUM+TERM:TERM=TERM+D
  170 NEXT
  180 PRINT ''"The sum is: ";SUM
  190 COLOUR 3:PRINT CHR$(7) ' TAB(10);"
Another go? Y or N ";
  200 REPEAT:G$=GET$:UNTIL G$="Y" OR G$=
"N"
  210 IF G$="Y" THEN RUN
  220 CLS:PRINT '"Bye for now.":END

RUN

          Arithmetic sequences

This produces arithmetic sequences.

Enter the first term, the common differ-
ence and the number of terms required.

First term: ?5

Common difference: ?5

Number of terms: ?10

Here is the sequence:

5, 10, 15, 20, 25, 30, 35, 40, 45, 50

The sum is: 275

          Another go? Y or N 


Which would you prefer?

Suppose the publisher of a book offered you a job and then asked how you would like to be paid: "Which would you prefer? Start at £3000 per six months with a rise of £120 after every 6 months or start at £6120 a year with a rise of £240 after every year.' The amount of money received each year with either choice follows an arithmetic progression. But one choice is far better than the other - the first choice. Can you see why? Look at the following calculations:
First offerSecond offer
First year(First 6 months £3000)
(Second 6 months £3120)
£6120£6120
Second year(First 6 months £3240)
(Second 6 months £3360)
£6600£6360
Third year(First 6 months £3480)
(Second 6 months £3600)
£7080£6600

You can see that if you were to stay at the job for more than one year then the first offer is better. Notice that in the first offer the amount received during the first 6 months each year increases by £240, and so the annual increase is in fact £480. The annual salary for the first offer fits into the arithmetic sequence with the formula

   6120 + (N - 1)*480

while the second has the formula

   6120 + (N - 1)*240

Suppose you were given a third alternative: 'Start with £1440 a quarter with a rise of £60 at the end of every 3 months.' Which offer would you prefer now? Hopefully the answer should be clear to you. The calculation would go as follows:

Third offer
First year(First 3 months £1440)
(Second 3 months £1500)
(Third 3 months £1560)
(Fourth 3 months £1620)£6120
Second year(First 3 months £1680)
(Second 3 months £1740)
(Third 3 months £1800)
(Fourth 3 months £1860)£7080

If you compare any 3 monthly period from year to year then the increase is £240, but you get this every 3 months. Thus, with this new offer the annual increase is £960.

Geometric sequences

Another common type of sequence is the geometric sequence or geometric progression. In a geometric sequence the ratio proceeds with a constant ratio, for instance

   2, 6, 18, 54, 162

where every term (except the first) is three times the previous term. The general formula for a geometric sequence is given by

   A * R­(N-1)

where A is the first term and R is the common ratio.
   Here are some further examples of geometric sequences.

   4, 2, 1, 0.5, 0.25, 0.125, 0.0625   (A = 4, R = 0.5)
   2, -4, 8, -16, 32, -64, 128   (A = 2, R= -2)

The next program may help you analyse geometric sequences. You enter the first term of the sequence, the common ratio and the number of terms required. Notice that a formula is not required since the computer does the calculation iteratively. In addition the program adds up all the terms in the sequence and gives you the answer.

Listing 5.3
LIST

   10 REM Geometric sequences
   20 MODE 1:COLOUR 3:PRINT ' TAB(10);"G
eometric sequences"':@%=10
   30 PRINT "This program creates geomet
ric sequences "
   40 PRINT "Enter the first term, the c
ommon ratio and the number of terms requ
ired."'
   50 COLOUR 1:INPUT "First term: ";A
   60 INPUT '"Common ratio: ";R
   70 REPEAT
   80  INPUT '"Number of terms: ";N
   90  IF N<1 OR N<>INT(N) THEN COLOUR 3
:PRINT '"Try again please.":COLOUR 1
  100 UNTIL N>0 AND N=INT(N)
  110 COLOUR 1:PRINT '"Here is the seque
nce:"':COLOUR 2
  120 TERM=A:SUM=0
  130 FOR I=1 TO N
  140  IF 38-POS<LEN(STR$(TERM)) THEN PR
INT
  150  PRINT ;TERM;:IF I<N THEN PRINT ",
 ";
  160  SUM=SUM+TERM:TERM=TERM*R
  170 NEXT
  180 PRINT ''"The sum is: ";SUM
  190 COLOUR 3:PRINT CHR$(7) ' TAB(10);"
Another go? Y or N ";
  200 REPEAT:G$=GET$:UNTIL G$="Y" OR G$=
"N"
  210 IF G$="Y" THEN RUN
  220 CLS:PRINT '"Bye for now.":END


RUN

          Geometric sequences

This program creates geometric sequences
 
Enter the first term, the common ratio a
nd the number of terms required.

First term: ?2

Common ratio: ?-2

Number of terms: ?8

Here is the sequence:

2, -4, 8, -16, 32, -64, 128, -256

The sum is: -170

          Another go? Y or N 


Interest

On 1 January a woman puts £100 in a bank which gives 6% interest each year (at the end of the year). To what amount will the woman's £100 grow after 10 years in the bank?
   After one year she will have

      100 + 100 * 6/100
         = 100 + 100*0.06
         = 100 * 1.06

which is £106. At the end of two years she will have

   100 * 1.06 + (100 * 1.06) * 0.06
      = 100 * 1.06 * 1.06

and you should be able to observe that after 10 years she will have

   100 * 1.06­10.

The total amount at the end of each year forms a geometric sequence as shown below.

   100 * 1.06, 100 * 1.06­2, 100 * 1.06­3, 100 * 1.06­4
      100 * 1.065­5, 100 * 1.065­6, 100 * 1.065­7, 100 * 1.065­8
         100 * 1.065­9, 100 * 1.065­10

More generally, if you start with an amount A and receive interest 1% per annum then after N years your original amount has grown to the following amount:

   A * (1 + 1/100)­N

Daily interest

If £1000 is deposited in a savings bank paying 6% interest at the end of each year then at the end of one year the total will become

   1000 * 1.06

assuming that no further deposits or withdrawals are made. If, on the other hand, the bank paid interest every 6 months (and paid interest on the interest given, ie compounded the interest) then the total at the end of the year would be

   1000 * (1.03)­2.

More generally, if the bank paid 6% interest compounded N times a year then at the end of one year the £1000 would grow to:

   1000 * (1 + 0.06/N)­N

The table below illustrates the different amounts depending on how often interest is compounded.

N6% compoundedTotal at end of year
(to nearest penny)
1yearly£1060.00
2semiannually£1060.90
4quarterly£1061.36
6bimonthly£1061.52
12monthly£1061.68
52weekly£1061.80
365daily£1061.83
8760hourly£1061.84

The above table was prepared with the BBC and Electron micros using the following simple program.

Listing 5.4
LIST

   10 REM Compound interest via formula
   20 MODE 1:COLOUR 3:PRINT ' TAB(11);"C
ompound interest"':@%=&02020A
   30 PRINT "This program shows the effe
ct on `1000  at 6% compounded a number o
f times per  year."''
   40 PRINT "Enter number of times inter
est is to be compounded.":COLOUR 1
   50 REPEAT
   60  INPUT '"Number: ";N
   70  IF N<1 OR N<>INT(N) THEN COLOUR 3
:PRINT '"Try again please.":COLOUR 1
   80 UNTIL N>0 AND N=INT(N)
   90 T=1000*(1 + 0.06/N)^N
  100 COLOUR 1:PRINT '"The compounded va
lue is ";:COLOUR 2:PRINT ;T
  110 COLOUR 3:PRINT CHR$(7) '' TAB(10);
"Another go? Y or N ";
  120 REPEAT:G$=GET$:UNTIL G$="Y" OR G$=
"N"
  130 IF G$="Y" THEN RUN
  140 CLS:PRINT '"Bye for now.":END


RUN

           Compound interest

This program shows the effect on `1000  
at 6% compounded a number of times per  
year.


Enter number of times interest is to be 
compounded.

Number: ?52

The compounded value is 1061.80


          Another go? Y or N 


You could make some additions and alterations to allow for other interest rates. Such a program appears next.

Listing 5.5
LIST

   10REM Compound interest via formula
   20MODE 1:COLOUR 3:PRINT ' TAB(11);"Co
mpound interest"':@%=&02020A
   30PRINT "This program shows the effec
t on `1000  at 6% compounded a number of
 times per  year."''
   40PRINT "Enter the interest rate.":CO
LOUR 1
   50REPEAT
   60INPUT '"Interest rate: ";I
   70IF I<=0 OR I>=100 THEN COLOUR 3:PRI
NT '"Be reasonable.":COLOUR 1
   80UNTIL I>0 AND I<100
   90COLOUR 3:PRINT '"Enter number of ti
mes interest is to be compounded.":COLOU
R 1
  100REPEAT
  110 INPUT '"Number: ";N
  120 IF N<1 OR N<>INT(N) THEN COLOUR 3:
PRINT '"Try again please.":COLOUR 1
  130UNTIL N>0 AND N=INT(N)
  140T=1000*(1 + I/100/N)^N
  150COLOUR 1:PRINT '"The compounded val
ue is ";:COLOUR 2:PRINT ;T
  160COLOUR 3:PRINT CHR$(7) '' TAB(10);"
Another go? Y or N ";
  170REPEAT:G$=GET$:UNTIL G$="Y" OR G$="
N"
  180IF G$="Y" THEN RUN
  190CLS:PRINT '"Bye for now.":END


RUN

           Compound interest

This program shows the effect on `1000  
at 6% compounded a number of times per  
year.


Enter the interest rate.

Interest rate: ?10

Enter number of times interest is to be 
compounded.

Number: ?52

The compounded value is 1105.06

         Another go? Y or N

Double or quit

Some people believe that you need never lose when gambling.
   To illustrate this look at the following gambling game: 'A fair coin is tossed, meanwhile you place your bet. If the coin shows a head then you get your money back plus an equivalent amount.'
   To show that you need never lose the argument goes as follows. Start by betting £1. If you win, quit. If you lose, play again with a stake of £2. Each time you lose, double your stake and play again. Stop as soon as you win and you will be in pocket.
   Suppose for example that you lose the first four times and win on the fifth. The table below illustrates what happens.

StakeLossWin
First toss£1£1
Second toss£2£2
Third toss£4£4
Fourth toss£8£8
Fifth toss£16£16
TOTAL£15£16
NET GAIN = £1

The sequence that arises is a geometric sequence. Do you believe the argument over why you would never lose?

Fibonacci sequences

At the beginning of this chapter we had the sequence

   (e) 4, 4, 8, 12, 20, 32, 52, 84, 136, 220

which is given by the formula

   4*INT(((0.5 + SQR(5)/2)­N - (0.5 - SQR(5)/2)­N) / SQR(5)).

Rather than use this formula there is a more obvious way of creating the sequence. Every term except the first two is the sum of the two previous ones.

   4 + 4 = 8
   4 + 8 = 12
   8 + 12 = 20

and so on.
   Sequences of numbers created in this way we called Fibonacci sequences. It was in 1202 that Leonard of Pisa, nicknamed Fibonacci, observed such a sequence of numbers associated with the breeding of rabbits.
   Here are two other Fibonacci sequences:

   2, 5, 7, 12, 19, 31, 50
   3, 5, 8, 13, 21, 34, 55

The next program will produce Fibonacci sequences ad nauseum.

Listing 5.6
LIST

   10REM Fibonacci sequences
   20MODE 1:COLOUR 3:PRINT ' TAB(10);"Fi
bonacci sequences"':@%=10
   30PRINT "This program creates Fibonac
ci sequences "'
   40PRINT "Enter two integers, separate
d by a comma":COLOUR 1
   50REPEAT
   60 INPUT '"Two numbers: ";U,V
   70 IF U<>INT(U) OR V<>INT(V) THEN COL
OUR 3:PRINT '"Integers please.":COLOUR 1
   80UNTIL U=INT(U) AND V=INT(V)
   90REPEAT
  100 INPUT '"How many terms do you want
: ";N
  110 IF N<1 OR N<>INT(N) THEN COLOUR 3:
PRINT '"Try again please.":COLOUR 1
  120UNTIL N>0 AND N=INT(N)
  130COLOUR 1:PRINT '"Here is the sequen
ce:"':COLOUR 2
  140FOR I=1 TO N
  150 IF 38-POS<LEN(STR$(U)) THEN PRINT
  160 PRINT ;U;:IF I<N THEN PRINT ", ";
  170 W=U+V:U=V:V=W
  180NEXT
  190COLOUR 3:PRINT CHR$(7) '' TAB(10);"
Another go? Y or N ";
  200REPEAT:G$=GET$:UNTIL G$="Y" OR G$="
N"
  210IF G$="Y" THEN RUN
  220CLS:PRINT '"Bye for now.":END


RUN

          Fibonacci sequences

This program creates Fibonacci sequences
 

Enter two integers, separated by a comma

Two numbers: ?3,5

How many terms do you want: ?7

Here is the sequence:

3, 5, 8, 13, 21, 34, 55


          Another go? Y or N 


Here is an exercise that you may like to do. Write a short program for your computer.
   'Write down any two integers. Form the Fibonacci sequence by adding pairs of terms to form a third term. Find the ratio of each term in the sequence with the one immediately before it. What happens to this ratio as the number of terms gets large? Work out the value of 0.5 + SQR(5)/2'.