Introduction to GAP – Exercise sheet 1

7th April 2025

Tutorial questions

  1. (Easy)

  2. (1)

    Compute the sum of the first 100100 numbers using a for loop. Can you also do it using a while loop?

  3. (2)

    (*) Write a GAP function that computes the nnth power of a given number. Specifically, the function should take a number xx and an integer nn, and return xnx^{n} without using the operator ^. Can you write a non-recursive solution without using ^?

  4. (3)

    Given a list of integers, return the sublist consisting of those that are powers of two or three.

  5. (4)

    Define a function such that, given an integer nn, it returns 3n3n if n0(mod3)n\equiv 0\pmod{3}, (n1)3(n-1)^{3} if n1(mod3)n\equiv 1\pmod{3}, and 0 otherwise. Compute the value of such a function on the interval [100,100][-100,100]. How many times do you get a 0 as an answer?

  6. (Medium)

  7. (5)

    (*) Write a GAP function that takes a matrix with integer coefficients as input and returns a new matrix where:

    • The odd coefficients are doubled.

    • The even coefficients remain unchanged.

  8. (6)

    Return the first ten pairs (p,q)(p,q) of prime numbers satisfying q=p+2q=p+2.

  9. (7)

    (*) The Collatz conjecture.

    1. (a)

      Define the “Collatz" function fC:f_{C}:\mathbb{N}\to\mathbb{N}.

    2. (b)

      Define a function such that given a positive integer nn, it returns kk such that fCk(n)=1f_{C}^{k}(n)=1. Here, fCkf_{C}^{k} denotes the composition of fCf_{C} with itself kk times. It is conjectured that such a kk always exists for any positive integer.

  10. (Hard)

  11. (8)

    (*) Find (if possible) the smallest solution of

    {x3mod10,x8mod15,x5mod84.\begin{cases}x\equiv 3\bmod 10,\\ x\equiv 8\bmod 15,\\ x\equiv 5\bmod 84.\end{cases}

    Hint: Use the Chinese reminder theorem.

    Hint 2: Use the documentation.

  12. (9)

    The Look and Say sequence. Look at this sequence of numbers.

    11121121111122131221113112221\begin{array}[]{l}1\\ 11\\ 21\\ 1211\\ 111221\\ 312211\\ 13112221\end{array}

    Can you guess the function that computes the first 100 terms?

    Compare your answer with https://oeis.org/A005150.

More exercises

  1. (9)

    Given a list of integers, return the sublist consisting of those that are powers of two or three.

  2. (10)

    Define the Euler Totient function. Remember that Euler Totient function (often denoted as ϕ(n)\phi(n)) counts the number of positive integers up to nn that are coprime to nn.

  3. (11)

    Define a function that inputs a positive integer and returns its double factorial.

  4. (12)

    Define a function such that, given a string, it returns fail if the string is empty, and replaces the last character of the string by the symbol &\& otherwise.

  5. (13)

    Define a function such that, given a list, it returns a new list obtained by reversing the second half of the input list.

  6. (14)

    Define a function such that, given two non-empty lists, it returns a new list obtained by interlacing the values of the input lists.

  7. (15)

    Given a set of elements SS in a given group GG, return a smaller subset of SS consisting of GG-conjugate representatives (lying in SS).

    Hint: Use IsConjugate.