Python - Functions with numbers P84591


Statement
 

pdf   zip

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 xx and a natural pp, returns xx raised to pp, that is, xpx^p.

  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 nn-th element of the Fibonacci sequence using the recursive algorithm according to its definition (f(0)=0f(0)=0, f(1)=1f(1)=1, f(n)=f(n1)+f(n2)f(n)=f(n-1)+f(n-2) for n2n\ge 2).

  5. Write a function @quickFib(n)@ that, returns the nn-th element of the Fibonacci sequence using a faster algorithm.

Scoring

Each function scores 20 points.

Sample session

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