fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int exp(int x, int n){
  11. if(n==0)
  12. return 1;
  13. if(n==1)
  14. return x;
  15. if(n%2==0)
  16. return exp(x*x, n/2);
  17. else
  18. return x * exp(x*x, (n-1)/2);
  19. }
  20.  
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23. System.out.println(exp(0,0));
  24. System.out.println(exp(1,0));
  25. System.out.println(exp(0,1));
  26. System.out.println(exp(2,1));
  27. System.out.println(exp(2,2));
  28. System.out.println(exp(2,3));
  29. System.out.println(exp(2,4));
  30. System.out.println(exp(3,2));
  31. System.out.println(exp(3,4));
  32. System.out.println(exp(3,5));
  33. }
  34. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
1
1
0
2
4
8
16
9
81
243