Lab6 Part B Iteration
Lab6 Part B Iteration
1. There is a popular myth about the man who invented chess. This man invented the chess
and present it to his king. The king was so pleased with the invention that he offered the
inventor a great reward in gold. The inventor suggested an alternative reward: he would
get one grain of wheat on the first square of the chess board, two grains on the second
square, four on the third, eight on the fourth, etc., doubling the number of grains each
time. The ruler saw that this must be a much better deal for him, and he accepted. The
board has 64 squares. Write a program to determine the following:
a) How many total grains of wheat did the ruler have to pay the inventor?
b) If a wheat grain weighs approximately 50 mg. How much did the wheat weigh?
Use a while loop to compute pi. Check what happens when the number of iteration is
fluctuated.
3. GCD
Write a function that calculates the gcd of two numbers that are given as parameters
The “computational rule” for creating the sequence is to start from some given n, and to
generate the next term of the sequence from n, either by halving n, (whenever n is
even), or else by multiplying it by three and adding 1. The sequence terminates when n
reaches 1.
Write a function that generates a sequence given a number n.
>>> collatz_conj(3)
3, 10, 5, 16, 8, 4, 2, 1
>>> collatz_conj(10)
10, 5, 16, 8, 4, 2, 1
collatz_conj(100)
100, 50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
5. Counting digits
Write a function that counts the number of digits a given number has. A call to the
function num_digits(710) will print 3.
Find a scenario where this function fails. Why would it fail?
7. Write a function named print_big_v (size), where size indicates the size of the pattern
that the function will print. Here is the output when the function is called with 6 as the
argument for size, then it should print the following: