Maple & Combinatorics

advertisement
Counters
The value assigned to a variable can be changed. For instance, consider the following code that
changes the value of a variable, counter, each time the loop is executed:
> counter:=0;
for i from 1 to 10 do
counter:=counter+1
od;
More usefully, a counter incorporated into a piece of code can allow us to evaluate the number of
times a particular condition is satisfied. Then, many interesting questions can be answered. For
example, how many prime numbers less than 1000 are there?
> countprimes:=0;
for i from 1 to 1000 do
if (isprime(i)) then
countprimes:=countprimes+1
fi;
od;
countprimes;
Exercises:
1. Write a piece of code to count the number of primes between 100 and 200.
2. Write a piece of code to count the number of even integers between 100 and 299.
3. Write a piece of code to count the number of integers between 1 and 100 that are divisible by
7.
4. Use a for loop to square the integers from 25 to 50. Then modify your code to count how
many of these squares are divisible by 3.
Maple & Combinatorics
Recall that in mathematics: A permutation of n distinct objects is an arrangement or ordering of the
n objects. The number of such arrangements is denoted nPn.
An r-permutation of n distinct objects is an arrangement using r of the n objects.
The number of possible r-permutations is denoted nPr.
The combinat package in Maple contains commands useful for combinatorics such as commands to
compute numbers of particular arrangements etc.: the command with(combinat) is used to load the
package.
Consider the set {1, 3, 6, 7, 8, 9} and suppose we are interested in the permutations of this set. The
set can be stored in Maple as follows:
> S:={1, 3, 6, 7, 8, 9};
All possible permutations of the numbers in the set are then given by
> with(combinat):
permute(S);
Often times just the number of permutations is of interest rather than the permutations themselves.
Maple has a command which will calculate the number of permutations of a set directly:
> numbperm(S);
720
If arrangements of only r of the n items in a set are of interest, they can be enumerated just as easily.
For instance, suppose we would like to know how many four digit numbers can be formed from the
set {1, 3, 6, 7, 8, 9} (without repetition of digits). Then the following command can be used:
> numbperm(S,4);
360
These can be listed using
> permute(S,4);
In order to access a particular permutation from this list, we first store the result using the name
fourdigitno. The ith permutation in the list is then given by fourdigitno[i] and the jth digit in that
permutation is given by fourdigitno[i][j].
> fourdigitno:=permute(S,4):
fourdigitno[1];
fourdigitno[1][3];
[1,3,6,7]
6
Exercises:
1. Consider all four digit numbers which can be formed from the set {1, 3, 6, 7, 8, 9} (without
repetition of digits).
(i) Ask Maple to list (print on the screen) those four digit numbers which are less than 7000.
(ii) Ask Maple to list those four digit numbers which are even.
(iii) Ask Maple to count those four digit numbers which are less than 7000.
(iv) Ask Maple to count those four digit numbers which are even.
2. Suppose a PIN number (personal identification number) consists of 3 digits in which no digit can
be repeated.
How many of these PIN numbers end in 0?
How many of these PIN numbers start with 0?
Download