Question

In: Computer Science

Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float)...

Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float) and pow (int) > = 1, and it returns the result root raised to power pow. The recursion of the power function is defined as: Base case: rootpow = root, if pow = 1 Recursive case: rootpow = root* rootpow-1 , otherwise. b) Develop a greatestC( ) function that determines the greatest common divisor of two integer numbers. Test greatestC() in a simple main program on the following numbers: 12 and 24; 50 and 10; 33649 and 17043. The recursive definition of the gcd function is as follows: Base case: greatestC(x, y) = x if y = 0 Recursive case: greatestC(x, y) = greatestC(x, x % y) otherwise, where % is the modulus operator.

Solutions

Expert Solution

SOLUTION FOR ROOT POWER

def power(root,rootpow):
if(rootpow==1 or root==1 or root==0):
return root
elif(rootpow<=0):
return 1
else:
return root*power(root,rootpow-1)
print("power(1,2)",power(1,2))
print("power(5,0)",power(5,0))
print("power(9,1)",power(9,1))
print("power(2,3)",power(2,3))
print("power(7,-2)",power(7,-1))

"""Here, we are defining power function with 2 parameters of root and power and checking if the power is 1 or the root (base) is 1 or 0 then we'll simply return the root without any computation. Then we'll check if the power is less than or equal to 0, simply returns the number itself or we'll compute the power of the number"""

SOLUTION FOR GCD (GREATEST COMMON DIVISOR) OR HCF (HIGHEST COMMON FACTOR)

def greatestC(x,y):
if (y==0):
return x
else:
return greatestC(y,x%y)
print("greatestC(12,24)",greatestC(12,24))
print("greatestC(50,10)",greatestC(50,10))
print("greatestC(33649,17043)",greatestC(33649,17043))

"""Here, we are defining greatestC function with 2 parameters of number 1 and number 2 and checking if the number 2 simply return the number 1 after operating with modulo function of number2"""


Related Solutions

(python) Write a function that involves two arguments, named changeTheCase(myFile, case),thattakes, as arguments, the name of...
(python) Write a function that involves two arguments, named changeTheCase(myFile, case),thattakes, as arguments, the name of a file, myFile, and the case, which will either be"upper"or"lower".If case is equal to "upper" the function will open the file, convert all characters on each line to upper case, write each line to a new file, named "upperCase.txt", and return the string "Converted file to upper case."If case is equal to "lower" the function will open the file, convert all characters on each...
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Make a python code. Write a function named max that accepts two integer values as arguments...
Make a python code. Write a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Write the program as a loop that...
Using C++: Write a function that recursively calculates the sum of an array. The function should...
Using C++: Write a function that recursively calculates the sum of an array. The function should have 2 parameters, the array and an integer showing the number of elements (assume they are all integers) in the array. The function will use a recursive call to sum the value of all elements. You must prompt for a series of integers to be entered in the array. As your program reads the numbers in, increment a count so it knows how many...
Write a Python program: The function is named validity(). It receives 2 floating point parameters, min...
Write a Python program: The function is named validity(). It receives 2 floating point parameters, min and max, from the program that invoked it. The function asks the user to enter a float number. Using a while loop it checks whether the number is valid (between min and max, inclusive). If not valid, the while loop uses this statement to prompt for a new number: num = float (input (" Enter a number that is at least :"+ str(min) +...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Write an algorithm for a function named finder that recursively goes through a list and returns...
Write an algorithm for a function named finder that recursively goes through a list and returns the index of the first occurrence of an item of interest. The function takes three parameters: 1. The first position in the list (or 0) 2. The list we want to iterate over 3. The item we want to find. If the item is not there in the list and we have iterated over the whole list, then the function should return 0.
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT