Python Nested Loops and Exercises

0% found this document useful (0 votes)
103 views2 pages
The document provides instructions for 8 exercises involving nested loops, functions, and other programming concepts in Python. The exercises include writing programs to: calculate mathemati…

Uploaded by

samuel yonas
  • Exercises
  • Lab Quiz

Fundamentals of CS and Programming

Lab 06: Nested loops & Functions

Exercises
1. Write a python program that computes the values of the following series. Your program should
accept the value of n from the user.
a. 1 + 4 + 9 + 16 + . . . n2
b. 1 + 4 + 27 + . . . nn
c. 1! + 2! + 3! + … + n!
2. Write python programs that print the following patterns to the screen using as few print
statements as possible.
a) * b) * * * * * c) * * * * * d) *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *

e) 1 * * * * * * * * * 1
1 2 * * * * * * * 2 1
1 2 3 * * * * * 3 2 1
1 2 3 4 * * * 4 3 2 1
1 2 3 4 5 * 5 4 3 2 1

3. Write a python program that generates all prime numbers less than 1000.
4. Write a python program that generates all perfect numbers less than 1000.
5. Write a python program that approximates the value of the mathematical constant e by using the
formula:

Prompt the user for the desired accuracy of e (i.e., the number of
terms in the summation).

6. Write a python program that estimates the value of ex by using the formula

Prompt the user for the desired accuracy of e (i.e., the number of
terms in the summation).

7. Write a python program that consists of two functions. The first function should compute
factorial of a number passed to it as argument. And the second function should accept
three numbers from the user and print their factorials by calling the first function.
8. Write a program that prints the first N Fibonacci numbers. Your program should have
two functions: one that determines the ith number of the sequence and a main function
that accepts a value for N and prints the sequence.
LAB QUIZE:

1. Write a program to play the Einstein game. Your program should request the user to enter
a three digit number such that the hundreds and ones digits differ by at least two. It then
will computes the reverse of the number and prints it, compute the difference b/n the
number and its reverse (this should always be positive ). Finally it concluded by stating
the sum of the difference with its reverse is 1089. Your program should behave as
follows.

2. 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?
c) And if the wheat was to be placed over the surface of Ethiopia how deeply Ethiopia
would be covered with that quantity of wheat. Prompt for the area of the region and
then output the depth, including the units you use.

Common questions

Powered by AI

The mathematical constant 'e' can be approximated using the series 1 + 1/1! + 1/2! + 1/3! + ..., and so on. In Python, this can be implemented by iterating until the desired accuracy is reached. Use a loop to compute factorials and their reciprocals, updating the sum iteratively. Prompt the user for the number of terms for desired accuracy to terminate the loop effectively .

An efficient algorithm to generate prime numbers less than 1000 is the Sieve of Eratosthenes. Initialize a boolean array of size 1000 with all entries as true. Starting from the first prime number, mark all its multiples as false. Continue with the next true entry, marking its multiples, until the square root of 1000. Finally, collect indices marked as true to get primes. This reduces unnecessary checks and improves efficiency .

The wheat problem is an example of geometric progression, where each square has double the grains of the previous, starting with one. This results in a total calculated as a sum of powers of two: 2^0 + 2^1 + ... + 2^63. The total can be computed using the formula for a geometric series sum, S = a(r^n - 1)/(r - 1), for the checkerboard with 64 squares, with r=2 and a=1 .

The Einstein game entails reversing a three-digit number whose hundreds and ones digits differ by at least two, finding the positive difference between the original and its reverse, and showing that the sum of this difference and its reverse equals 1089. Programmatically, this involves digit manipulation, arithmetic operations to ensure absolute value differences and sum calculations to prove the invariant of 1089. The logical sequence aligns the game elements with program steps, inspecting consistently for these criteria .

A factorial function computes factorial values via recursive or iterative methods, central for product series problems. For multi-number inputs, define a primary function for factorial computation and another driving function accepting multiple user inputs. This solution design extracts modular logic where each input is processed through a loop calling the factorial function, illustrating procedural abstraction and reuse .

To generate perfect numbers less than 1000, use a nested loop structure—outer loop to iterate over numbers to test, and inner loop to identify their divisors and compute their sums. Perfect numbers are those where the sum of divisors (excluding the number itself) equals the number. Carefully managing nested loop iterations and sum checks are crucial for the correct detection of perfect numbers .

To compute the series 1 + 4 + 9 + ... + n^2 in Python, you need a loop that iterates from 1 to n and computes the square of each integer, then sums these values. Pseudocode for this could be: initialize total as 0, for each i from 1 to n, compute i^2 and add it to total, finally print total. This involves understanding of loops, arithmetic operations, and user input handling in Python, as detailed in the series exercise .

e^x is approximated using the series 1 + x + x^2/2! + x^3/3! + ..., and so forth. In a Python program, collect input for x and desired terms, loop to calculate each term as x^i/i!, add to a running sum until reaching the specified number of terms. This allows higher precision by controlling term counts as directly solicited from user input .

To output star patterns with minimal print statements, use loops. For each row, use a single print statement inside a loop to either accumulate stars or spaces and print them. Iterate over rows with loop variables controlling the number of stars printed, adjusting indices as needed to switch patterns. Compact logic and precise loop control are required to keep print statements minimal yet effective in delivering intricate patterns .

Compute the total number of grains from the geometric series solution. With each grain weighing approximately 50 mg, multiply total grains by this unit weight. Further, this weight can be used to calculate distribution depth over an area by dividing the total volume (weight divided by density) by the stipulated area, giving the depth of coverage .

Fundamentals of CS and Programming 
Lab 06: Nested loops & Functions 
Exercises 
1. Write a python program that computes the
LAB QUIZE: 
1. Write a program to play the Einstein game. Your program should request the user to enter 
a three digit number

You might also like