
Jennifer Q.
asked 03/28/22JAVA QUESTION Write a method that computes the following series below: HINT: Use a loop and Math.pow() to raise t to the nth power.
1 Expert Answer
LOC H. answered 04/06/22
20+ years of proficiency in Java, SQL, Python, C#, Algorithms
You can use the built-in Java Math.pow(), but if you have to build your own methods, the below are recursion methods:
Assume: n is not negative integer
// O(log(N)) solution
public static double pow_logn(double t, int n) {
if (n == 0) return 1;
//if (n == 1) return t;
double p = pow_logn(t, n / 2);
return (n % 2 == 0) ? p * p : p * p * t;
}
// O(log3(N)) solution -> Faster than pow_logn
public static double pow_log3n(double t, int n) {
if (n == 0) return 1;
//if (n == 1) return t;
double p = pow_log3n(t, n / 3);
int r = n % 3;
if (r == 0) return p * p * p;
if (r == 1) return p * p * p * t;
// r = 2
return p * p * p * t * t;
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Donald W.
What's the series that the method should compute?03/29/22