Question

In: Computer Science

Write a function log2(x), which gives an integer approximation of the log base 2 of a...

Write a function log2(x), which gives an integer approximation of the log base 2 of a positive number x, but it does so using recursion. Use a base case where you return 0 if x is 1 or less. In the general case, add 1 to the answer and divide x by 2. (python 3)

Solutions

Expert Solution

Program:

Note: The following program is done in python 3. Match the indentation as program snip before run.

#log2 function
def log2(x):
#if condtion for recursive function
if(x<=1):
#return 0 value as result
return 0
#recursive function log2 calling it self again and again then add 1
return log2(x/2)+1
#input value x from user as integer
x=int(input("Enter a integer value: "))
#calling log2 function as store result in result variable
result=log2(x)
#displying result
print("The integer approximation of the log base 2 of a positive number x is:",result)

Sample Output:


Related Solutions

In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
1. Which of the following is the linear approximation of the function f ( x )...
1. Which of the following is the linear approximation of the function f ( x ) = 2e^sin (7x) at x = 0? Group of answer choices y=cos⁡(7)x+2 y=7x+2 y=14x+2 y=2x+7 y=e^7x+14 2. Recall that Rolle's Theorem begins, ``If f ( x ) is continuous on an interval [ a , b ] and differentiable on (a , b) and ___________, then there exists a number c …'' Find all values x = c that satisfy the conclusion of Rolle's...
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to...
Write a MATLAB function that uses my_sine(x,n) to plot the approximation of the sinefunction from−3π/2 to 3π/2 with increments equal to Δx,where Δx and n(the number ofterms used in the series approximation) should be input arguments of the function andshould be specified by the user.
For the following exercises, write a logarithmic equation corresponding to the graph shown. Use y = log2 (x) as the parent function.
For the following exercises, write a logarithmic equation corresponding to the graph shown.Use y = log2 (x) as the parent function.
a. The Log likelihood function is ?(?) = (a1 + a2) log(?) − ?(b1 + b2)  write...
a. The Log likelihood function is ?(?) = (a1 + a2) log(?) − ?(b1 + b2)  write this as a function of θ, by substituting in θ = log(λ). b. Write down the likelihood equation for θ, using the log-likelihood in part a, and hence determine θ^ the MLE for θ. c. Show that θˆlog = (λ^). Show this algebraically, what property of MLEs is this? d. Differentiate the LHS of the likelihood equation, obtain the expected information ?(?) = ?{??(?,...
use change of base formula log base 2/3(.155)
use change of base formula log base 2/3(.155)
For the following exercises, determine whether or not the given function f is continuous everywhere...f(x) = log2 (x)
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = log2 (x)
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation:...
Write a MATLAB function that calculates the approximate value of arctan(x) using the Maclaurin series approximation: arctan⁡(x)=x-x^3/3+x^5/5-x^7/7+⋯ The function should accept 3 parameters: value of x, number of significant figures accuracy i.e. n, and the maximum number of iterations. In the function, use ε_s=(0.5×〖10〗^(2-n ) )% in order to continue until the ε_a falls below this criteria. The function should return 3 values: the approximate value of arctan(x) at the end of the program, final ε_a and the number of...
Write code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT