Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package laboration1;
- public class Prog4 {
- public static void main(String[] args){
- double x;
- int n;
- x = Double.parseDouble(args[0]);
- n = Integer.parseInt(args[1]);
- System.out.println("x= "+ x + " and k = "+ n +", x^k= "+ rec_raise(x,n));
- System.out.println("x= "+ x + " and k = "+ n +", x^k= "+ rec_raise_eff(x,n));
- }
- public static double rec_raise(double x, int n){
- if(n == 0){
- return 1.0;
- }else{
- return (x * rec_raise(x,n-1));
- }
- }
- public static double rec_raise_eff(double x, int n){
- double temp;
- if (n == 0){
- return 1.0;
- }
- if (n%2 == 0){
- temp = rec_raise_eff(x,n/2);
- return temp * temp;
- }else{
- temp = rec_raise_eff(x,(n-1)/2);
- return x * temp * temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement