Question

In: Computer Science

Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long...

Problem 3:

(a) Implement a sublinear running time complexity recursive function in Java public static long exponentiation(long x, int n) to calculate x^n.

Note: In your function you can use only the basic arithmetic operators (+, -, *, %, and /).

(b) What is the running time complexity of your function? Justify.

(c) Give a number of multiplications used by your function to calculate x^63.

Important Notes:

• For the item (a):

o You must add the main method in your program in order to test your implementation.

o You can assume that there are no data errors that need to be checked as all the data will be assumed correct.

o Your function you can use only the basic arithmetic operators (+, -, *, %, and /).

o Your program MUST be submitted only in source code form (.java file).

o Remember that a program that does not compile or does not run loses all correctness points.

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.



public class Exponentiation
{
    public static long exponentiation(long x, int n)
    {
        // Base cases here
            if (n == 0)
                return 1;

            if (n == 1)
                return x ;

            // recursive function
            long temp = exponentiation(x, n / 2);
            temp = (temp* temp);

            // if exponent is even value
            if (n % 2 == 0)
                return temp;
            else // if exponent is odd value
                return x*temp;
    }

    // test the method
    public static void main(String[] args)
    {
        System.out.println("3^4 is :"+exponentiation(3,4));
        System.out.println("2^10 is :"+exponentiation(2,10));
        System.out.println("10^3 is :"+exponentiation(10,3));
        System.out.println("123^3 is :"+exponentiation(123,3));
    }
}

======================

OUTPUT

  1. The time complexity is: O(Log2 N) N is the exponent
  2. To calculate x63, we will perform like this:
    • x63 = x31 * x31 * x


Related Solutions

There is a Java program that is missing one recursive function: public class Ackermann { /*...
There is a Java program that is missing one recursive function: public class Ackermann { /* / n+1 when m = 0 * ack(m,n) = | ack(m-1,1) when m > 0 and n = 0 * \ ack(m-1, ack(m, n-1)) otherwise */ public static int ack(int m, int n) { return 0; } /* Ackermann's Function Test Framework * * Be extremely careful with these test cases. Ackermann's grows very fast. * For example, ack(4, 0) = 13, but ack(5,0)...
There is a Java program that is missing one recursive function: public class BinarySearch { /*...
There is a Java program that is missing one recursive function: public class BinarySearch { /* / -1 when min > max * | mid when A[mid] = v * search(A, v, min, max) = | search(A,v,mid+1,max) when A[mid] < v * \ search(A,v,min,mid-1) otherwise * where mid = (min+max)/2 */ public static int search_rec(int[] A, int v, int min, int max) { return 0; } public static int search(int[] A, int v) { return search_rec(A, v, 0, A.length-1); }...
There is a Java program that is missing one recursive function: public class Factorial { /*...
There is a Java program that is missing one recursive function: public class Factorial { /* / 1 when n is 0; * fact(n) = | * \ n*fact(n-1) otherwise */ public static int fact(int n) { return 0; } /* Factorial Test Framework * * Notice the odd expected value for fact(20). It is negative because * fact(20) should be 2432902008176640000, but the maximum int is only * 2147483647. What does Java do when integers run out of range?...
There is a Java program that is missing one recursive function: public class GCD { /*...
There is a Java program that is missing one recursive function: public class GCD { /* There is a neat trick with recursive programming. The function described    * below requires that y is at most x. Rather than add this to the recursive    * function definition, we can add a front-end, helper function.    *    * I wrote this function for you and I called it gcd. The recursive logic goes    * in a function called...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
Question 15: Use the Master theorem to find the asymptotic complexity of the running time function...
Question 15: Use the Master theorem to find the asymptotic complexity of the running time function T(n) of the program in problem 14. --- Problem 14 --- def prob14(L): if len(L) <= 1: return 0 output = 0 for x in L: for y in L: output += x*y for x in L: output += x left = L[ 0 : len(L)//2 ] right = L[ len(L)//2 : len(L) ] return output + prob15(left) + prob15(right) ***Big-O, Omega, Theta complexity...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT