如何编写一个可以用Java计算function的函数。 没有循环

我一直在尝试用Java编写一个简单的函数,它可以在不使用循环的情况下计算第n个幂的数字。
然后我发现了Math.pow(a,b)类……或者方法仍然无法区分这两个对理论不太好。 所以我写了这个..

public static void main(String[] args) { int a = 2; int b = 31; System.out.println(Math.pow(a, b)); } 

然后我想制作我自己的Math.pow而不使用循环我希望它看起来比循环更简单,就像使用某种类型的重复我做了大量的研究,直到我遇到commons-lang3包我尝试使用StringUtils.repeat
到目前为止,我认为这是语法: –

 public static String repeat(String str, int repeat) StringUtils.repeat("ab", 2); 

过去24小时或更长时间遇到的问题StringUtils.repeat(String str,int 2); 重复字符串而不是输出或数字或计算。
有什么我可以做的来克服这个问题,还是有任何其他更好的方法来创建一个计算能力的函数?
不使用循环或Math.pow

这可能很有趣,但我花了很多时间才发现StringUtils.repeat只重复字符串,这就是我试图克服它的方式。 这有帮助

  public static int repeat(int cal, int repeat){ cal = 2+2; int result = StringUtils.repeat(cal,2); return result; } 

我可以不使用递归也许这样的事情

 public static RepeatThis(String a) { System.out.println(a); RepeatThis(a); } 

只是尝试理解部门中的java感谢您的所有评论,即使存在语法错误,只要逻辑被理解为对我有好处 🙂

尝试使用递归:

 int pow(int base, int power){ if(power == 0) return 1; return base * pow(base, --power); } 

具有O(Log(n))复杂性的另一种实现

 public static long pow(long base, long exp){ if(exp ==0){ return 1; } if(exp ==1){ return base; } if(exp % 2 == 0){ long half = pow(base, exp/2); return half * half; }else{ long half = pow(base, (exp -1)/2); return base * half * half; } } 

处理具有O(log(n))复杂度的+/-指数的函数。

 double power(double x, int n){ if(n==0) return 1; if(n<0){ x = 1.0/x; n = -n; } double ret = power(x,n/2); ret = ret * ret; if(n%2!=0) ret = ret * x; return ret; 

}

我认为在生产递归中并不能提供高端性能。

 double power(double num, int exponent) { double value=1; int Originalexpn=exponent; double OriginalNumber=num; if(exponent==0) return value; if(exponent<0) { num=1/num; exponent=abs(exponent); } while(exponent>0) { value*=num; --exponent; } cout << OriginalNumber << " Raised to " << Originalexpn << " is " << value << endl; return value; 

}

使用此代码。

 public int mypow(int a, int e){ if(e == 1) return a; return a * mypow(a,e-1); } 

当然,创建自己的递归函数:

 public static int repeat(int base, int exp) { if (exp == 1) { return base; } return base * repeat(base, exp - 1); } 

Math.pow(a,b)

Math是类, pow是方法, ab是参数。

这个处理负指数:

 public static double pow(double base, int e) { int inc; if(e <= 0) { base = 1.0 / base; inc = 1; } else { inc = -1; } return doPow(base, e, inc); } private static double doPow(double base, int e, int inc) { if(e == 0) { return 1; } return base * doPow(base, e + inc, inc); } 

这是一个计算数字幂的O(log(n))代码。 使用的算法技术是分而治之的。 它也接受负幂,即x ^( – y)

 import java.util.Scanner; public class PowerOfANumber{ public static void main(String args[]){ float result=0, base; int power; PowerOfANumber calcPower = new PowerOfANumber(); /* Get the user input for the base and power */ Scanner input = new Scanner(System.in); System.out.println("Enter the base"); base=input.nextFloat(); System.out.println("Enter the power"); power=input.nextInt(); result = calcPower.calculatePower(base,power); System.out.println(base + "^" + power + " is " +result); } private float calculatePower(float x, int y){ float temporary; /* Termination condition for recursion */ if(y==0) return 1; temporary=calculatePower(x,y/2); /* Check if the power is even */ if(y%2==0) return (temporary * temporary); else{ if(y>0) return (x * temporary * temporary); else return (temporary*temporary)/x; } } } 

递归方法最简单:

 int power(int base, int exp) { if (exp != 1) { return (base * power(base, exp - 1)); } else { return base; } } 

其中base是数字, exp是exponenet