Question

(a) Implement a sublinear running time complexity recursive function in Java public static long long x, int n) to calculate X 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 x63.

Please code this in Java, thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer (a)

PROGRAM :

import java.io.*;

public class ExponentialPower
{
  
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

ExponentialPower func = new ExponentialPower();

System.out.println("Enter the value of x (Number) :"); // Taking Input
long x = Integer.parseInt(br.readLine());

System.out.println("Enter the value of n (Power :"); // Taking Input
int n = Integer.parseInt(br.readLine());

System.out.println("\n" +x +"^" +n +" = "+func.exponentiation(x,n)); // Method Called
}


public static long exponentiation(long x, int n)
{
if(n==0)
return 1;

else if(n==1)
return x;

else
return x*exponentiation(x,n-1);
}

}

Answer (b)

TIME COMPLEXITY :

Lets now assume n is of the form n-2k where k E N Λ k > 0 (e.g. n is a power of 2). We then get a recurrence relation like s

Answer (c)

/*


Let us take x = 2 and n = 63.
Then, multiplication used by the functions :
2*exp(2,62)
2*2*exp(2,61)
2*2*2*exp(2,60)
2*2*2*2*exp(2,59)
.
.
.
.
. and so on upto
2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*exp(2,1)

2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2 = 2^63 = 9.223372e+18

*/

OUTPUTS :

Output - JavaApplication1 (run) X Enter the value of x (Number) Enter the value of n (Power ЕЗ 2 63 BUILD SUCCESSFUL (total t

Output JavaApplication1 (run) X Enter the value of x (Number) Enter the value of n (Power: 5^3 = 125 BUILD SUCCESSFUL (total

Add a comment
Know the answer?
Add Answer to:
Please code this in Java, thank you! (a) Implement a sublinear running time complexity recursive function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • write the following code in java thank you and if you can, could you please write...

    write the following code in java thank you and if you can, could you please write the code in replit and sent me a link to it replit is basically like a online compiler and you can write java code on it thank you The first part is to implement one of the questions from your examination. /* A Range objects represents an integer range, such as 1-10 or 50701-50799. The lower and upper bounds of a Range are given...

  • Question 1 (25 pts) Find the running time complexity for the following code fragments. Express yo...

    Question 1 (25 pts) Find the running time complexity for the following code fragments. Express your answers using either the Big-O or Big-Θ notations, and the tightest bound possible. Justify your answers. for(int count O , i -0; i < n* n; i++) for(int i0 ; j <i; j++) count++ for(int count O , i -0; i

  • java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods...

    java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods commented with: // TODO: TIP: Do not just go from memory of your assignment implementation, be sure to consider carefully the constructor and method implementation provided to you. NOTE: You do not have to provide an implementation for any methods intentionally omitted public class Heap PriorityQueue implements PriorityQueue private final static int DEFAULT SIZE 10000 private Comparable [ ] storage private int currentSize: public...

  • Use java code please not C++ or Python. Thank you. Write a recursive algorithm that counts...

    Use java code please not C++ or Python. Thank you. Write a recursive algorithm that counts the number of nodes in a linked list. Analyze the runtime of your algorithm

  • Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x....

    Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x. Assume w=32. * For example 0xFF00 -> 0x8000, and 0x6600 --> 0x4000. * If x = 0,then return 0. */ int leftmost_one(unsigned x); Your function should follow the above bit-level integer coding rules, except that you may assume that data type int has w=32 bits. Your code should contain a total of at most 15 arithmetic, bit-wise, and logical operations. In C++ and has...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Write simplistic recursive Java code for Sierpinski I made a psuedo code tell me if anything...

    Write simplistic recursive Java code for Sierpinski I made a psuedo code tell me if anything wrong with it: // x - left edge, y - right edge, z - peak, num - number of times the program is supposed to run public void drawTriangle (int x, int y, int z, int num) maketriangle(x,y,z); //imaginary method to create triangle using coordinates. if (num == 0) { return; } else { int midxy = (y-x)/2; int midxz = (z-x)/2; int midyz...

  • Java code efficiency: Look at the implementation of the method called intervalSums below. Design and implement...

    Java code efficiency: Look at the implementation of the method called intervalSums below. Design and implement a more efficient runtime algorithm for intervalSums. public static long intervalSums(intll A, intl0 B) long count = 0; for (int i = 0; i < A.|ength; i++) { for (int j = i; j < A.length; j++) { int sum = 0; for (int k = i; k <= j; k++) { sum += A[k]; count += 1; B[i][j] = sum; if (j >...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT