Esercizi di Programmazione Avanzata

Exercises on Functional Programming and Other Silly Things


Exercise 1: A Few of Math with Lists.

Write the solutions for the following quizzes by using functional programming:

  1. Sum all the natural numbers below one thousand that are multiples of 3 or 5.
  2. Calculate the smallest number divisible by each of the numbers 1 to 20.
  3. Calculate the sum of the figures of 2^1000
  4. Calculate the first term in the Fibonacci sequence to contain 1000 digits.
Exercise 2: Frequencies.

Let's write a module (a pool of functions) that given a quite large text (over than 2000 words) counts how frequent each word occurs in the text. In particular the module should provide the function freqs that given a filename and a number would return a list of words (with their frequencies) that occur more than the given number; the list is sorted by frequency with the higher first.

The text is read from a file and it is a real text with punctuation (i.e., commas, semicolons, ...) that shouldn't be counted.

Note that words that differ only for the case should be considered the same.

Exercise 3: Approximated Trigonometric Functions.

sin(x) can be approximate by the Taylor's series:

Let's write a library to implement sin(x, n) by using the Taylor's series (where n is the level of approximation, i.e., 1 only one item, 2 two items, 3 three items and so on).

Let's compare your function with the one implemented in the math module at the growing of the approximation level.

Hint. Use a generator for the factorial and a comprehension for the series.

Exercise 4: Engineers' Paradises.

Consider the number 6. The divisors of 6 are: 1, 2, 3 and 6.

Every number from 1 up to and including 6 can be written as a sum of distinct divisors of 6: 1=1, 2=2, 3=1+2, 4=1+3, 5=2+3, 6=6.

A number n is called a practical number if every number from 1 up to and including n can be expressed as a sum of distinct divisors of n.

A pair of consecutive prime numbers with a difference of six is called a sexy pair (since "sex" is the Latin word for "six"). The first sexy pair is (23, 29).

We may occasionally find a triple-pair, which means three consecutive sexy prime pairs, such that the second member of each pair is the first member of the next pair.

We shall call a number n such that:

an engineers' paradise.

Find the sum of the first four engineers' paradises.

Walter Cazzola

Didactics

Publications

Funded Projects

Research Projects

Related Events








Valid XHTML 1.0 Transitional