N-th power

Report a typo

It is possible to find a n-th power much quicker than by making n multiplications!

To do this you need to use the following recurrence relations:

an=(a2)n/2 a^n = (a^2)^{n/2} for even n,

an=aan1 a^n = a * a^{n-1} for odd n.

Implement the algorithm of quick exponentiating using a recursion method.

Sample Input 1:

2.0
1

Sample Output 1:

2.0

Sample Input 2:

1.5
10

Sample Output 2:

57.665
Write a program in Java 17
import java.util.Scanner;

public class Main {

public static double pow(double a, long n) {
// write your code here
}

/* Do not change code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final double a = Double.parseDouble(scanner.nextLine());
final int n = Integer.parseInt(scanner.nextLine());
System.out.println(pow(a, n));
}
}
___

Create a free account to access the full topic