fork(3) 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. public static int countGbl = 0;
  11.  
  12.  
  13. public static double raise(double base, int exp) {
  14.  
  15. double b = base;
  16.  
  17. for (int i = 1; i < exp; i++){
  18. countGbl += 1;
  19. base = base * b;
  20. }
  21.  
  22. return base;
  23. }
  24.  
  25.  
  26. public static double pow1(double base, int exp) {
  27.  
  28. if (base == 0.0) return Double.POSITIVE_INFINITY;
  29. else if (base == 0.0 && exp >= 0) return 0.0;
  30. else if (exp == 1) return base;
  31. else if (base > 0 && exp == 0) return 1.0;
  32. else{
  33. if (exp < 0){
  34. base = 1.0/base;
  35. exp = -exp;
  36. }
  37.  
  38. if (exp % 2 == 0){
  39. countGbl++;
  40. return pow1(base, exp/2) * pow1(base, exp/2);
  41. }
  42. else {
  43. countGbl += 2;
  44. return pow1(base, exp/2) * pow1(base, exp/2) * base;
  45. }
  46. }
  47. }
  48.  
  49.  
  50. public static double pow2(double base, int exp) {
  51. double temp = raise(base, exp/2);
  52. double retval = temp*temp;
  53. if (exp % 2 == 1){
  54. countGbl++;
  55. retval *= temp;
  56. }
  57. return retval;
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. public static void main(String[] args) {
  65.  
  66. Scanner s = new Scanner(System.in);
  67.  
  68. System.out.println("Enter base: ");
  69. double base = Integer.parseInt(s.nextLine());
  70. System.out.println("Enter exp: ");
  71. int exp = Integer.parseInt(s.nextLine());
  72.  
  73. System.out.println(pow2(base, exp));
  74. System.out.println(countGbl);
  75.  
  76. }
  77. }
Success #stdin #stdout 0.1s 29492KB
stdin
4
5
stdout
Enter base: 
Enter exp: 
4096.0
2