lecture8.mw

> Sequences

>

>

> Can you produce the following sequences?

> 1,1,1,1,1

> 2,2,2,2,2

> 3,3,3,3,3

> 4,4,4,4,4

> 5,5,5,5,5

> ...

> A := [0,0,0,0,0];

A := [0, 0, 0, 0, 0]

> for i to 5 do
 A[i] := A[i] + 1;

end;

A[1] := 1

A[2] := 1

A[3] := 1

A[4] := 1

A[5] := 1

> A;

[1, 1, 1, 1, 1]

> Next  := proc(A)
 local i;

 for i to 5 do;

   A[i] := A[i] + 1;

 end;

 return A;

end;

Next := proc (A) local i; for i to 5 do A[i] := A[i]+1 end do; return A end proc

> Next(A);

Error, (in Next) illegal use of a formal parameter

> Something does not work. The reason is that Maple does not like if you assign
values to list entries. You should better use a data structure called an Array:

> T := Array(A);

T := Array([1, 1, 1, 1, 1])

> Next(T);

Array([2, 2, 2, 2, 2])

> Next(T);

Array([3, 3, 3, 3, 3])

> Next(T);

Array([4, 4, 4, 4, 4])

> Next(T);

Array([5, 5, 5, 5, 5])

> T := Array(5);

T := Array({}, datatype = anything, storage = rectangular, order = Fortran_order)

> T := Array([0,0,0,0,0]);

T := Array([0, 0, 0, 0, 0])

> for i to 10 do Next(T); end;

Array([1, 1, 1, 1, 1])

Array([2, 2, 2, 2, 2])

Array([3, 3, 3, 3, 3])

Array([4, 4, 4, 4, 4])

Array([5, 5, 5, 5, 5])

Array([6, 6, 6, 6, 6])

Array([7, 7, 7, 7, 7])

Array([8, 8, 8, 8, 8])

Array([9, 9, 9, 9, 9])

Array([10, 10, 10, 10, 10])

> OK, that was easy, except for the fact that we have to use arrays.

> Now, let's do something more interesting.
Let's count like the mileage counter in a car:

> [0,0,0,0,0]

> [0,0,0,0,1]

> [0,0,0,0,2]

> ...

> [0,0,0,0,9]

> [0,0,0,1,0]

> [0,0,0,1,1]

> ....

> and so forth. Can you write a procedure to increase the milage counter by one?

> Here we go:

> Next := proc(A)
 local l, i;

 l := nops(A);

 i := l;

 while i >= 1 and A[i] = 9 do

   A[i] := 0;

   i := i - 1;

 end;

 if i >= 1 then

   A[i] := A[i] + 1;

 end;

 return A;

end;

>

>

>

Next := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := nops(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end proc

> A := Array([0,0,0,0,0]);

A := Array([0, 0, 0, 0, 0])

> Next(A);

>

Array([0, 0, 0, 1, 0])

> Hmmm, that is unexpected. What is the problem? Maybe the nops stuff?

> nops(A);

4

> Why 4, shouldn' it be 5? OK, lets look up the help for Array.
We will find that there is a different command for the length of an array.

Hold on, it's name is ArrayNumElems !

> ArrayNumElems(A);

5

> # OK, we can rewrite the function:

> Next := proc(A)
 local l, i;

 l := ArrayNumElems(A);

 i := l;

 while i >= 1 and A[i] = 9 do

   A[i] := 0;

   i := i - 1;

 end;

 if i >= 1 then

   A[i] := A[i] + 1;

 end;

 return A;

end;

Next := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 9 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end proc

> A := Array([0,0,0,0,0]);

A := Array([0, 0, 0, 0, 0])

> Next(A);

Array([0, 0, 0, 0, 1])

> Next(A);

Array([0, 0, 0, 0, 2])

> for i to 15 do Next(A); end;

Array([0, 0, 0, 0, 3])

Array([0, 0, 0, 0, 4])

Array([0, 0, 0, 0, 5])

Array([0, 0, 0, 0, 6])

Array([0, 0, 0, 0, 7])

Array([0, 0, 0, 0, 8])

Array([0, 0, 0, 0, 9])

Array([0, 0, 0, 1, 0])

Array([0, 0, 0, 1, 1])

Array([0, 0, 0, 1, 2])

Array([0, 0, 0, 1, 3])

Array([0, 0, 0, 1, 4])

Array([0, 0, 0, 1, 5])

Array([0, 0, 0, 1, 6])

Array([0, 0, 0, 1, 7])

> # OK, it seems to work.
# How about counting up in binary.

# That menas the digits should be either zero or one.

> Next := proc(A)
 local l, i;

 l := ArrayNumElems(A);

 i := l;

 while i >= 1 and A[i] = 1 do

   A[i] := 0;

   i := i - 1;

 end;

 if i >= 1 then

   A[i] := A[i] + 1;

 end;

 return A;

end;

Next := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end procNext := proc (A) local l, i; l := ArrayNumElems(A); i := l; while 1 <= i and A[i] = 1 do A[i] := 0; i := i-1 end do; if 1 <= i then A[i] := A[i]+1 end if; return A end proc

> A := Array([0,0,0,0,0]);

A := Array([0, 0, 0, 0, 0])

> Next(A);

Array([0, 0, 0, 0, 1])

> Next(A);

Array([0, 0, 0, 1, 0])

> Next(A);

Array([0, 0, 0, 1, 1])

> Next(A);

Array([0, 0, 1, 0, 0])

> A := Array([0,0,0,0,0]);

A := Array([0, 0, 0, 0, 0])

> for i to 40 do Next(A); end;

Array([0, 0, 0, 0, 1])

Array([0, 0, 0, 1, 0])

Array([0, 0, 0, 1, 1])

Array([0, 0, 1, 0, 0])

Array([0, 0, 1, 0, 1])

Array([0, 0, 1, 1, 0])

Array([0, 0, 1, 1, 1])

Array([0, 1, 0, 0, 0])

Array([0, 1, 0, 0, 1])

Array([0, 1, 0, 1, 0])

Array([0, 1, 0, 1, 1])

Array([0, 1, 1, 0, 0])

Array([0, 1, 1, 0, 1])

Array([0, 1, 1, 1, 0])

Array([0, 1, 1, 1, 1])

Array([1, 0, 0, 0, 0])

Array([1, 0, 0, 0, 1])

Array([1, 0, 0, 1, 0])

Array([1, 0, 0, 1, 1])

Array([1, 0, 1, 0, 0])

Array([1, 0, 1, 0, 1])

Array([1, 0, 1, 1, 0])

Array([1, 0, 1, 1, 1])

Array([1, 1, 0, 0, 0])

Array([1, 1, 0, 0, 1])

Array([1, 1, 0, 1, 0])

Array([1, 1, 0, 1, 1])

Array([1, 1, 1, 0, 0])

Array([1, 1, 1, 0, 1])

Array([1, 1, 1, 1, 0])

Array([1, 1, 1, 1, 1])

Array([0, 0, 0, 0, 0])

Array([0, 0, 0, 0, 1])

Array([0, 0, 0, 1, 0])

Array([0, 0, 0, 1, 1])

Array([0, 0, 1, 0, 0])

Array([0, 0, 1, 0, 1])

Array([0, 0, 1, 1, 0])

Array([0, 0, 1, 1, 1])

Array([0, 1, 0, 0, 0])

> # OK, that was all 32 possibilities for binary strings of length 5 and then we started over from 0

> # let us print both, the counter and the binary string:

> A := Array([0,0,0,0,0]);

A := Array([0, 0, 0, 0, 0])

> for i to 40 do print(i, A); Next(A); end:

1, Array([0, 0, 0, 0, 0])

2, Array([0, 0, 0, 0, 1])

3, Array([0, 0, 0, 1, 0])

4, Array([0, 0, 0, 1, 1])

5, Array([0, 0, 1, 0, 0])

6, Array([0, 0, 1, 0, 1])

7, Array([0, 0, 1, 1, 0])

8, Array([0, 0, 1, 1, 1])

9, Array([0, 1, 0, 0, 0])

10, Array([0, 1, 0, 0, 1])

11, Array([0, 1, 0, 1, 0])

12, Array([0, 1, 0, 1, 1])

13, Array([0, 1, 1, 0, 0])

14, Array([0, 1, 1, 0, 1])

15, Array([0, 1, 1, 1, 0])

16, Array([0, 1, 1, 1, 1])

17, Array([1, 0, 0, 0, 0])

18, Array([1, 0, 0, 0, 1])

19, Array([1, 0, 0, 1, 0])

20, Array([1, 0, 0, 1, 1])

21, Array([1, 0, 1, 0, 0])

22, Array([1, 0, 1, 0, 1])

23, Array([1, 0, 1, 1, 0])

24, Array([1, 0, 1, 1, 1])

25, Array([1, 1, 0, 0, 0])

26, Array([1, 1, 0, 0, 1])

27, Array([1, 1, 0, 1, 0])

28, Array([1, 1, 0, 1, 1])

29, Array([1, 1, 1, 0, 0])

30, Array([1, 1, 1, 0, 1])

31, Array([1, 1, 1, 1, 0])

32, Array([1, 1, 1, 1, 1])

33, Array([0, 0, 0, 0, 0])

34, Array([0, 0, 0, 0, 1])

35, Array([0, 0, 0, 1, 0])

36, Array([0, 0, 0, 1, 1])

37, Array([0, 0, 1, 0, 0])

38, Array([0, 0, 1, 0, 1])

39, Array([0, 0, 1, 1, 0])

40, Array([0, 0, 1, 1, 1])

> # OK, that's right, at least until 32.

> # after that, we have an overflow. That could happen in old cars, too.

> # Some people were hiding a 100000 miles this way, for example.

> # (at least in Germany they did that, that was easier b/c they

> # use kilometers over there, and 1 mile is 1.6 kilometers, so

> # 100000 kilometers is only so and so many miles.

> # how much is that by the way?

> 100000 / 1.6;

62500.00000

> # OK, 62 thousand miles. Well it is hard to guess if a car has

> # 62 thousand miles more or less, isn't it?

>

> # HOMEWORK
# hw 1)

> # write a procedure which takes an integer and produces
# the binary representation of that number (as a list of coefficients)

# EXAMPLE if I give you 1 you give me [1],

# if I give you    you give me

# 2                [1,0]

# 3                [1,1]

# 4                [1,0,0]

# 5                [1,0,1]

# 6                [1,1,0]

# 7                [1,1,1]

# 8                [1,0,0,0]

# and so forth

# Using a for loop, convert the numbers 60-70 into binary.

# What is 9999 in binary?

> # hw 2)

> # write a procedure which gets a list of 0's and 1's and produces the
# integer in decimals (i.e. the integer

# whose binary representation is the given list).

# (I.e. the reverse procedure to the above).

# using the previous loop, compute the binary representations of

# the numbers from 60 to 70, and compute back the original number in decimal from

# the binary representation.

# What is [1,0,1,0,1,0,1,0] in decimal?

>