Python - Functions with numbers P84591


Statement
 

pdf   zip

html

In this problem you must implement several functions in Python.

  1. Write a function absValue(x) that, given a number, returns its absolute value.
  2. Write a function power(x, p) that, given a number x and a natural p, returns x raised to p, that is, xp.
  3. Write a function isPrime(x) that, given a natural, returns a Boolean that tells whether it is a prime number or not.
  4. Write a function slowFib(n) that, returns the n-th element of the Fibonacci sequence using the recursive algorithm according to its definition (f(0)=0, f(1)=1, f(n)=f(n−1)+f(n−2) for n≥ 2).
  5. Write a function quickFib(n) that, returns the n-th element of the Fibonacci sequence using a faster algorithm.

Scoring

Each function scores 20 points.

Sample session
>>> absValue(-666)
666
>>> power(2, 3)
8
>>> isPrime(17)
True
>>> slowFib(5)
5
>>> quickFib(40)
102334155
Information
Author
Jordi Petit
Language
English
Translator
Jordi Petit
Original language
Catalan
Other languages
Catalan
Official solutions
Python
User solutions
Python