Question: 1- Assume the availability of a function called fact. The function receives an argument containing an integer value and returns an integer value. The function

1- Assume the availability of a function called fact. The function receives an argument containing an integer value and returns an integer value. The function should return the factorial of the argument. That is, if the argument is one or zero, the function should return 1. Otherwise, it should return the product of all the integers from 1 to the argument. So the value of fact(4) is 1*2*3*4 and the value of fact(10) is 1*2*3*4*5*6*7*8*9*10. Additionally, assume that the variable k has been initialized with a positive integer value. Write a statement that assigns the value of fact(k) to a variable x. The solution must include multiplying the return value of fact by k.

2- Assume the availability of a function named printStars that can be passed a parameter containing a non-negative integer value. The function prints out the given number of asterisks. Write a function named printTriangle that receives a parameter that holds a non-negative integer value and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 askterisks, and then a line of n-2 asterisks, and so on. For example, if the function received 5, it would print: ***** **** *** ** * The function must not use a loop of any kind (for, while, do-while) to accomplish its job. The function should invoke printStars to accom;lish the task of printing a single line.

3- The Fibonacci series (0, 1, 1, 2, 3, 5, 8, 13, 21) has as its first two values 0 and 1; each successive value is then calculated as the sum of the previous two values. The first element in the series is considered the 0th element. The nth element in the series, written as fib(n), is thus defined as:

n if n=0 or n=1

fib(n-1)+fib(n-2) otherwise

Write a function fib, that takes a single parameter, n, which contains an integer value, and recursively calculates and returns the nth element of the Fibonacci series.

4- Write a definition of a function called product. The function receives two parameters containing integer values. You may assume that neither parameter is negative. The function returns the product of the parameters. So, product(3,5) returns 15. The function must not use a loop of any kind (for, while, do-while).

5- The sum of the numbers 1 to n can be calculated recursively as follows:

The sum from 1 to 1 is 1.

The sum from 1 to n is n more than the sum from 1 to n-1

Write a function named sum that accepts a variable containing an integer value as its parameter and returns the sum of the numbers from 1 to to the parameter (calculated recursively).

6-

Given non-negative integers x and n, x to the nth power can be defined as:

x to the 0th power is 1

x to the nth power can be obtained by multiplying x to the (n-1)th power with x

Write a function named power that accepts two parameters containing integer values (x and n, in that order) and recursively calculates and returns the value of x to the nth power.

Please use python 3.0 language.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!