Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public class RecursPower {
  2.  
  3. public static void main(String[] args) {
  4. System.out.println(recursPower(2,3));
  5. }
  6.  
  7. public static double recursPower(double x, int n) {
  8. if(n == 0) {
  9. return 1;
  10. }
  11. else {
  12. double recurs = recursPower(x, n-1);
  13. double result = x * Math.pow(x, (n-1));
  14. // double result = Math.pow((Math.pow(x, n/2)), 2); // just for even (n) numbers
  15. System.out.println(recurs+" " +n+ " "+ x+" r = "+result);
  16. return result;
  17. }
  18. }
  19. }
  20.  
  21. /**else {
  22. double recurs = recursPower(x, n-1);
  23. if (n%2 == 0) {
  24. double result = Math.pow((Math.pow(x, n/2)), 2);
  25. System.out.println("1 =="+result);
  26. return result;
  27. }
  28. else {
  29. double result = x * Math.pow(x, (n-1));
  30. System.out.println("2 === "+result);
  31. return result; */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement