While iterations P30196


Statement
 

pdf   zip

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 nn returns n\lfloor\sqrt{n}\rfloor.

  2. Write a function @int_log(a, b)@ that given natural numbers aa greater than one and bb greater than zero returns natural kk such that akb<ak+1a^k \le b < a^{k + 1}.

  3. Write a function @gcd_lcm(a, b)@ that given natural numbers aa and bb such that a0a\not = 0 or b0b\not =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 nn returns True if and only if nn is prime.

  5. In order to play table games at the casino you need some tokens. Red tokens cost 77 euros and yellow tokens cost 44. Write a function @buy_tokens(n)@ that given a number nn of euros such that n20n \ge 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 ss and tt returns the longest string that is a common prefix of ss and tt.

Scoring

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

Sample session

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