Ive asked this everywer already but peopl seem too lame to make even a simple program like so i hope the mighty anon programers of 4chan are more skilled
what im trying to do is to Write a method taking two parameters x and n that computes the nth power of x without using ^ or declaring a new variable inside the method or using Math lib.
and it has to be coded in JAVA (i know it sux but dont ask why i use dat shit)
hep plox!
>>8 But yeah, I also can't do it without declaring a new variable.
You could use recursion:
class file {
public static void main(String [] args) {
System.out.println (power(2, 2));
}
public static double power(double x, int n) {
if (n == 0) return 1.0;
else if(n % 2 == 1) return x * power(x, n - 1);
else return power(x * x, n / 2);
}
}
I'd like to believe that the regulars know the answer, and instead of answering this clueless idiot, they are instead just trolling him. However, given some of the subpar responses about C and Unix, I'm starting to think some of them are just as cluess as the OP.
class ProgUtils {
public static long superpower(long x) {
long result;
switch (x) {
case 0: result = 1; break;
case 1: result = x; break;
case 2: result = x*x; break;
case 3: result = x*x*x; break;
case 4: result = x*x*x*x; break;
case 5: result = x*x*x*x*x; break;
case 6: result = x*x*x*x*x*x; break;
case 7: result = x*x*x*x*x*x*x; break;
case 8: result = x*x*x*x*x*x*x*x; break;
case 9: result = x*x*x*x*x*x*x*x*x; break;
case 10: result = x*x*x*x*x*x*x*x*x*x; break;
case 11: result = x*x*x*x*x*x*x*x*x*x*x; break;
case 12: result = x*x*x*x*x*x*x*x*x*x*x*x; break;
case 13: result = x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 14: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 15: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 16: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 17: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 18: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 19: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 20: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 21: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 22: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 23: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 24: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 25: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 26: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 27: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 28: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 29: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 30: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 31: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 32: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 33: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 34: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 35: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 36: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 37: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 38: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 39: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 40: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 41: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 42: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 43: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 44: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 45: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 46: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 47: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 48: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 49: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 50: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 51: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 52: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 53: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 54: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 55: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 56: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 57: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 58: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 59: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 60: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 61: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 62: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 63: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
case 64: result = x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x*x; break;
}
return result;
}
}
int final(int x, int n){
if(x < 0)
throw new UnsupportedException; /* runtime so no need to declare it */
switch(x){
case 0:
return 1;
case 1:
return x;
} /* default: */
return x * final(x, --n);
}
Name:
Anonymous2011-12-28 17:28
wouldn't it be something like this?
public int power(int x, int n)
{
if(n == 0)
return 1;
else if( n < 0){
while(n != 1){
x = x/x;
n++;
}
}else{
while(n != 1){
x = x*x;
n--;
}
}
return x;
}
unsigned long long power(unsigned long long x, unsigned long long p) {
if(p == 0) {
return x;
} else if(p & 1) { // is p odd?
return power(x, p & ~1)*x;
} else {
int square_root_of_answer = power(x, p>>1);
return square_root_of_answer*square_root_of_answer;
}
}
unsigned long long power(unsigned long long x, unsigned long long p) {
if(p == 1) { // MY BAD...
return x;
} else if(p & 1) { // is p odd?
return power(x, p & ~1)*x;
} else {
int square_root_of_answer = power(x, p>>1);
return square_root_of_answer*square_root_of_answer;
}
}
Name:
Anonymous2011-12-28 17:50
>>10 is the best answer, simple with recursion. >>31 creates new variable >>29 creates new variable
unsigned long long square(unsigned long long x) {
return x*x;
}
unsigned long long power(unsigned long long x, unsigned long long p) {
if(p == 1) {
return x;
} else if(p & 1) { // is p odd?
return power(x, p & ~1)*x;
} else {
return square(power(x, p>>1));
}
}
A method computing x^n without declaring variables or using Math libraries (or even integers): λx.λn.nx GEEZ THAT WAS SO DIFFICULT
Name:
Anonymous2011-12-28 18:15
ENTERPRISE QUALITY JAVA
502 /**
503 * Returns the value of the first argument raised to the power of the
504 * second argument. Special cases:
505 *
506 * <ul><li>If the second argument is positive or negative zero, then the
507 * result is 1.0.
508 * <li>If the second argument is 1.0, then the result is the same as the
509 * first argument.
510 * <li>If the second argument is NaN, then the result is NaN.
511 * <li>If the first argument is NaN and the second argument is nonzero,
512 * then the result is NaN.
513 *
514 * <li>If
515 * <ul>
516 * <li>the absolute value of the first argument is greater than 1
517 * and the second argument is positive infinity, or
518 * <li>the absolute value of the first argument is less than 1 and
519 * the second argument is negative infinity,
520 * </ul>
521 * then the result is positive infinity.
522 *
523 * <li>If
524 * <ul>
525 * <li>the absolute value of the first argument is greater than 1 and
526 * the second argument is negative infinity, or
527 * <li>the absolute value of the
528 * first argument is less than 1 and the second argument is positive
529 * infinity,
530 * </ul>
531 * then the result is positive zero.
532 *
533 * <li>If the absolute value of the first argument equals 1 and the
534 * second argument is infinite, then the result is NaN.
535 *
536 * <li>If
537 * <ul>
538 * <li>the first argument is positive zero and the second argument
539 * is greater than zero, or
540 * <li>the first argument is positive infinity and the second
541 * argument is less than zero,
542 * </ul>
543 * then the result is positive zero.
544 *
545 * <li>If
546 * <ul>
547 * <li>the first argument is positive zero and the second argument
548 * is less than zero, or
549 * <li>the first argument is positive infinity and the second
550 * argument is greater than zero,
551 * </ul>
552 * then the result is positive infinity.
553 *
554 * <li>If
555 * <ul>
556 * <li>the first argument is negative zero and the second argument
557 * is greater than zero but not a finite odd integer, or
558 * <li>the first argument is negative infinity and the second
559 * argument is less than zero but not a finite odd integer,
560 * </ul>
561 * then the result is positive zero.
562 *
563 * <li>If
564 * <ul>
565 * <li>the first argument is negative zero and the second argument
566 * is a positive finite odd integer, or
567 * <li>the first argument is negative infinity and the second
568 * argument is a negative finite odd integer,
569 * </ul>
570 * then the result is negative zero.
571 *
572 * <li>If
573 * <ul>
574 * <li>the first argument is negative zero and the second argument
575 * is less than zero but not a finite odd integer, or
576 * <li>the first argument is negative infinity and the second
577 * argument is greater than zero but not a finite odd integer,
578 * </ul>
579 * then the result is positive infinity.
580 *
581 * <li>If
582 * <ul>
583 * <li>the first argument is negative zero and the second argument
584 * is a negative finite odd integer, or
585 * <li>the first argument is negative infinity and the second
586 * argument is a positive finite odd integer,
587 * </ul>
588 * then the result is negative infinity.
589 *
590 * <li>If the first argument is finite and less than zero
591 * <ul>
592 * <li> if the second argument is a finite even integer, the
593 * result is equal to the result of raising the absolute value of
594 * the first argument to the power of the second argument
595 *
596 * <li>if the second argument is a finite odd integer, the result
597 * is equal to the negative of the result of raising the absolute
598 * value of the first argument to the power of the second
599 * argument
600 *
601 * <li>if the second argument is finite and not an integer, then
602 * the result is NaN.
603 * </ul>
604 *
605 * <li>If both arguments are integers, then the result is exactly equal
606 * to the mathematical result of raising the first argument to the power
607 * of the second argument if that result can in fact be represented
608 * exactly as a {@code double} value.</ul>
609 *
610 * <p>(In the foregoing descriptions, a floating-point value is
611 * considered to be an integer if and only if it is finite and a
612 * fixed point of the method {@link #ceil ceil} or,
613 * equivalently, a fixed point of the method {@link #floor
614 * floor}. A value is a fixed point of a one-argument
615 * method if and only if the result of applying the method to the
616 * value is equal to the value.)
617 *
618 * <p>The computed result must be within 1 ulp of the exact result.
619 * Results must be semi-monotonic.
620 *
621 * @param a the base.
622 * @param b the exponent.
623 * @return the value {@code a}<sup>{@code b}</sup>.
624 */
625 public static double pow(double a, double b) {
626 return StrictMath.pow(a, b); // default impl. delegates to StrictMath
627 }
Name:
Anonymous2011-12-28 18:15
final answer here:
int method(int x, int n){
if(n < 0)
throw new UnsupportedOperationException();
switch(n){
case 0:
return 1;
case 1:
return x;
}
return method(x, n-1) * x;
}