While iterations P30196


Statement
 

pdf   zip

html

You have to program several functions. Do not use the math module.

  1. Write an integer function int_root(n) that given a natural number n returns ⌊√n⌋.
  2. Write a function int_log(a, b) that given natural numbers a greater than one and b greater than zero returns natural k such that akb < ak + 1.
  3. Write a function gcd_lcm(a, b) that given natural numbers a and b such that a≠ 0 or b≠0 returns the greatest common divisor and the least common multiple. Your code has to implement the Euclid’s algorithm.
  4. Write a boolean function is_prime(n) that given a natural number n returns True if and only if n is prime.
  5. In order to play table games at the casino you need some tokens. Red tokens cost 7 euros and yellow tokens cost 4. Write a function buy_tokens(n) that given a number n of euros such that n ≥ 20, it returns the equivalence in tokens. When several equivalences are possible the function returns the one minimizing the total number of tokens.
  6. Write a string function max_overlap(s, t) that given two strings s and t returns the longest string that is a common prefix of s and t.

Scoring

The first function counts 15 points. Other ones count 17 point each one.

Sample session
>>> int_root(19)
4
>>> int_log(3, 20)
2
>>> gcd_lcm(12,18)
(6, 36)
>>> is_prime(51)
False
>>> buy_tokens(50)
(6, 2)
>>> max_overlap('bugs', 'bunny')
bu
Information
Author
Jorge Castro
Language
English
Translator
Original language
Catalan
Other languages
Catalan Spanish
Official solutions
Python
User solutions
Python