Quiz - Extras¶
Josephus problem. Imagine you have n
people arranged in a circle. Suppose these people are numbered from 1 to n
clockwise. Starting with person number 1,
run the circle clockwise and eliminate every m
-th person, as long as the circle has two or more people. The challenge is to develop the josephus
function
that takes n
and m
as parameters, both positive integers. The function should return the number of the person in the circle who survived.
Implement the Sieve of Eratosthenes algorithm.
Sieve of Eratosthenes
The challenge is to develop the eratosthenes_sieve
function that takes n
, a positive integer, as a parameter
and returns the number of prime numbers in the interval [2, n]. Use the Sieve of Eratosthenes to solve this problem.
Examples:
eratosthenes_sieve(20)
-> 8
eratosthenes_sieve(12)
-> 5
Project Euler Problem 2. Problem
First, check what the yield
keyword does in Python. Then, solve the adaptation of Project Euler Problem 2 below:
Develop the p_euler
function that takes n
, a positive integer, as an argument, and returns the sum of all even numbers less than n
in the Fibonacci sequence of n
.
Example:
p_euler(10)
-> 10
Explanation: The first ten terms of the Fibonacci sequence are:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89
Therefore, the even numbers in this sequence are:
2, 8, 34
However, only 2
and 8
meet the condition of being less than n
(in this case less than 10). The sum of these is 10
.
In the book “The Man Who Counted,” by Malba Tahan, a character wanted to win the grains of wheat that were distributed on a chessboard as follows:
one grain in the first square of the board, double (2) in the second, again double (4) in the third, again double (8) in the fourth, and so on up to the sixty-fourth square of the board.
Write an algorithm that calculates the total number of wheat grains needed to make this distribution. The sum_grains
function will return
this amount. The parameter n
will be the value 64
, representing the number of squares on a chessboard.
You are given n
tiles with dimensions of 10cm x 10cm. With them, you must assemble a set of squares using all the given tiles. To do this, you must follow the following rules:
The first square you build should use as many tiles as possible.
If there are tiles left, the next square should also be built using the largest possible number of tiles that remain.
Follow this same pattern until all tiles are used.
Then, the form_squares
function returns a dictionary representing the set of formed squares. Its keys represent the size, in tiles, of the side of the
formed square, while the values represent the quantity of squares of that size that were formed.
Example:
form_squares(31)
-> {5:1, 2:1, 1:2}
Explanation: The formed set will have 4 squares: 1 square with 5 tiles per side, 1 with 2 tiles per side, and 2 with 1 tile per side.