0% found this document useful (0 votes)
36 views2 pages

Lab6 Part B Iteration

The document contains instructions for several programming exercises involving iteration and functions: 1) Write a program to calculate the total grains of wheat and total weight owed to an inventor based on a chessboard reward doubling scheme. 2) Use a while loop to iteratively compute pi by calculating terms of the Leibniz formula, and observe the results for different numbers of iterations. 3) Write a function to calculate the greatest common divisor (GCD) of two input numbers. 4) Write a function to generate and print the Collatz sequence starting from a given input number. 5) Write a function to count the number of digits in a given input number, and find a scenario where

Uploaded by

samuel yonas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Lab6 Part B Iteration

The document contains instructions for several programming exercises involving iteration and functions: 1) Write a program to calculate the total grains of wheat and total weight owed to an inventor based on a chessboard reward doubling scheme. 2) Use a while loop to iteratively compute pi by calculating terms of the Leibniz formula, and observe the results for different numbers of iterations. 3) Write a function to calculate the greatest common divisor (GCD) of two input numbers. 4) Write a function to generate and print the Collatz sequence starting from a given input number. 5) Write a function to count the number of digits in a given input number, and find a scenario where

Uploaded by

samuel yonas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Addis Ababa institute of Technology

Center of Information Technology and Scientific Computing


Fundamentals of computer science and programming

Lab 06(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?

2. π: The following is the Leibniz formula for pi

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

4. The Collatz conjecture


Let’s look at a simple sequence that has fascinated mathematicians for many years.
They still cannot answer even quite simple questions about this.

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

CLUE: to print on the same line use


print(<printable object>, end=’, ’)

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?

6. Write a function that counts the frequency of a given digit in a number.


frequency_of_num(15213323231790, 3) will print 4.

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:

You might also like