> | 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]; |
> | for i to 5 do
A[i] := A[i] + 1; end; |
> | A; |
> | Next := proc(A)
local i; for i to 5 do; A[i] := A[i] + 1; end; return A; end; |
> | 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); |
> | Next(T); |
> | Next(T); |
> | Next(T); |
> | Next(T); |
> | T := Array(5); |
> | T := Array([0,0,0,0,0]); |
> | for i to 10 do Next(T); end; |
> | 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; |
> |
> |
> |
> | A := Array([0,0,0,0,0]); |
> | Next(A); |
> |
> | Hmmm, that is unexpected. What is the problem? Maybe the nops stuff? |
> | nops(A); |
> | 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); |
> | # 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; |
> | A := Array([0,0,0,0,0]); |
> | Next(A); |
> | Next(A); |
> | for i to 15 do Next(A); end; |
> | # 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; |
> | A := Array([0,0,0,0,0]); |
> | Next(A); |
> | Next(A); |
> | Next(A); |
> | Next(A); |
> | A := Array([0,0,0,0,0]); |
> | for i to 40 do Next(A); end; |
> | # 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]); |
> | for i to 40 do print(i, A); Next(A); end: |
> | # 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; |
> | # 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? |
> |