Introduction to GAP – Exercise sheet 1
7th April 2025
Tutorial questions
(Easy)
-
(1)
Compute the sum of the first numbers using a for loop. Can you also do it using a while loop?
-
(2)
(*) Write a GAP function that computes the th power of a given number. Specifically, the function should take a number and an integer , and return without using the operator
^. Can you write a non-recursive solution without using^? -
(3)
Given a list of integers, return the sublist consisting of those that are powers of two or three.
-
(4)
Define a function such that, given an integer , it returns if , if , and otherwise. Compute the value of such a function on the interval . How many times do you get a as an answer?
(Medium)
-
(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.
-
•
-
(6)
Return the first ten pairs of prime numbers satisfying .
-
(7)
(*) The Collatz conjecture.
-
(a)
Define the “Collatz" function .
-
(b)
Define a function such that given a positive integer , it returns such that . Here, denotes the composition of with itself times. It is conjectured that such a always exists for any positive integer.
-
(a)
(Hard)
-
(8)
(*) Find (if possible) the smallest solution of
Hint: Use the Chinese reminder theorem.
Hint 2: Use the documentation.
-
(9)
The Look and Say sequence. Look at this sequence of numbers.
Can you guess the function that computes the first 100 terms?
Compare your answer with https://oeis.org/A005150.
More exercises
-
(9)
Given a list of integers, return the sublist consisting of those that are powers of two or three.
-
(10)
Define the Euler Totient function. Remember that Euler Totient function (often denoted as ) counts the number of positive integers up to that are coprime to .
-
(11)
Define a function that inputs a positive integer and returns its double factorial.
-
(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.
-
(13)
Define a function such that, given a list, it returns a new list obtained by reversing the second half of the input list.
-
(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.
-
(15)
Given a set of elements in a given group , return a smaller subset of consisting of -conjugate representatives (lying in ).
Hint: Use
IsConjugate.