java - How to raise a BigInteger to the power of something without pow() -
my java professor wants me write program user gives value x , value y, , computes value of x^y (x power of y), , prints console. i'm not allowed use pow() method this. x , y int values.
my code, after user gives x , y values:
biginteger solution = biginteger.valueof(0); (int i=0; i<y; i++) { solution = solution.add(biginteger.valueof((x+solution.intvalue())*x)); }
but never works properly. answer gives waaaay off. if has code correct problem, assistance appreciated!
you need multiply x y times.
biginteger solution; if (y == 0) { solution = biginteger.valueof(1); } else if (y > 0) { solution = biginteger.valueof(x); (int i=0; i<y-1; i++) { solution = solution.multiply(biginteger.valueof(x)); } } else { // negative powers left exercise reader }
Comments
Post a Comment