|
|
GAP Instructional MaterialReturn to IndexBasic programming in GAPWe assume that the reader has a computer on which GAP is installed. The tutorial which follows develops some basic programming with the GAP language. It does not use any functions concerning groups, rings, or fields.
GAP code is shown [Press ?? to see GAP output] You can also choose to see a page showing ALL GAP output.
The following commands need no explanation.
Let us comment here regarding spaces in the input. We could (and often do) leave spaces round the contents of brackets. For example
More spaces (although we wouldn't in general suggest that anyone does this) are still OK.
We can even leave a space before the (.
However, we obviously can't leave spaces in the middle of commands or numbers. For example the following will produce an error.
We return to giving commands for the manipulation of numbers.
We can find Greatest Common Divisors of integers
and their Least Common Multiples.
Given two integers a, b with greatest common divisor d we can find x, y such that xa + yb = d with Note that "rec" indicates a record with various components.
Notice the use of To work mod n we can use the obvious method.
However, to compute large powers mod n it is better to use
gives the same result as
What do you think So if we want to compute 1000251291 mod 251291 we should use
Is this the answer you expect? Try
What well known theorem does this illustrate? What about
Another useful function for working with integers is
finds all the square roots of 4 mod 15. It has the same effect as
For the cube roots of 4 mod 15 use
In fact the
GAP writes its lists with [ ]. We can define a list using a range.
This list r1 consists of all the integers from 1 to 10. For example:
The list r1 contains
GAP will also use ranges to produce an arithmetic progression. For example:
We can get GAP to display the elements of the list.
More examples:
We can also write out all the elements to define a list:
GAP will do a form of arithmetic with lists.
We can add or subtract lists -- even of different lengths
We can add new elements to lists. GAP does not return any output to the screen.
But we can see what has happened:
The command
GAP has various commands for copying and
We can filter out elements from a list with particular properties. For example to find all the elements in the list t which are prime we input
To filter out the elements which have a given property, one must use
GAP will sometimes consider a list as a set, but often needs to convert it before applying set operations.
We test if it is a set:
GAP will only count a list of integers as a set if it is ordered increasingly and contains no repetitions. The next example has a repeated entry.
Of course s and t are lists and we can create sets from them:
Notice that this operation removes repetitions and puts the elements into increasing order. The usual set operations work and GAP treats the lists as if they were sets before applying the operations:
To add an element to the set ss we use
We begin with elementary loops.
Notice that the output is 12345678910. Suppose we want to print a space between consecutive numbers.
Suppose we want to print each number on a separate line. We use
A slightly more useful loop might be:
Notice that we have put spaces in " squared is " so that it displays properly.
We now introduce conditionals into our loop. We test to see if an integer i is prime and if so we print it.
Of course most even numbers cannot be prime so we could reduce the number of times the loop is run by only looking at odd numbers. However, 2 will not be listed!
The next loop prints the primes in the first 50 terms of the Fibonacci sequence.
We can test whether numbers less than 1000 are primes in another way, rather than use
Note however that Primes only contains the 168 primes less than 1000. For example
We move on to functions. The following code defines a function that simply squares the argument it is given.
If you want to see the function written out fully, you can ask GAP to
Try
to see the result. There are simpler ways to write easy functions like this. For example
We can produce lists of cubes as follows:
This is a very useful construction. Here are a few more examples.
A more complicated example
If we define a function which returns true or false, we may use this to filter a list.
Next we write a function which returns n/2 if n is even and 3n+1 if n is odd. There is a famous open question which asks whether one always reaches 1 if one starts with any number and successively applies that rule. For example, if we start with 3 we get 3, 10, 5, 16, 8, 4, 2, 1
Let us now write a function t to return the number of steps that are required to reach 1. Note that we define local variables to use in the function. We do this so that if variables with the same name are used elsewhere their value will not be affected by the function. Note that there is no semi-colon before the local variables are listed.
When we wrote the code for the function we put extra spaces at the beginning of each line so that it was easier to see where conditional clauses and loops began and ended. Let us produce a sequence with ith entry the number of steps required to reach 1 starting from i.
If you don't want to see the list s then end the line with ;;
Notice the fact that lots of adjacent entries in s are equal. So often beginning with consecutive numbers we find that the same number of steps is required to reach 1. As we reach larger numbers we see three consecutive numbers with this property etc. We construct a list len whose nth entry will be the number of times n consecutive numbers take the same number of steps. Note that when we reach the end of the list, we do not know whether the next entry would have been the same as the last one and so we do not count this run.
Remove the trailing 0's in len
We again note that we added spaces in the strings in the Another thing that one can investigate about this problem is the highest number reached by the sequence before it collapses to 1.
We now investigate Stirling numbers of the second kind. First we list the these numbers for a fixed n, take n = 12.
Find the maximum value
and its position in the list
Let us now construct a list stir from these lists of Stirling numbers so that the nth entry of stir is the list of Stirling numbers for a set of n elements. This would give a seriously large output, so we suppress it.
Now find where the maximum occurs for each n.
It is not easy to predict where the maximum will occur for a given n. However, there is an asymptotic result known, namely that as n
We now examine a problem which involves elementary number theory.
First note that
It is easy to find the number of divisors of n.
For ease of typing let us define f(n) to be the number of divisors of n.
Let us look for pairs of consecutive integers with the same number of divisors.
We can easily move to finding three consecutive integers with the same number of divisors.
Then we can look for even longer runs. This code uses a different kind of loop.
As an exercise you may care to turn the above into a function with a parameter n which returns sequences of length n of integers with the same number of divisors.
Can one find such runs of arbitrary length? GAP can help, but won't tell you the answer! JOC/EFR January 2003 |
|
Home   |   Members   |   Pre-prints   |   Library   |   Reports   |   Seminars   |   Conferences   |   Research   |   Publicity   |  Pictures   |   GAP   |   Webmail |