Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

NEED URGENT HELP

Name: edo-chan 2011-12-28 11:11

sup /prog/

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!

Name: Anonymous 2011-12-28 11:27

Why are the gals so stupid?

Name: Anonymous 2011-12-28 11:32


public static Double anus(Double SGERFWGERHEEETHHHHHHHWETHG, int SDGIKgrnsjsnglrSGHERI){
  if (SDGIKgrnsjsnglrSGHERI == 0) return 1.0;
 
  Double jieurlhigosrlijhoegterlehogti = 1.0;
  int GSJRGerhglshergsehrgSGE = (SDGIKgrnsjsnglrSGHERI > 0) ? -1 : 1;
 
  do { jieurlhigosrlijhoegterlehogti *= SGERFWGERHEEETHHHHHHHWETHG;
  } while((SDGIKgrnsjsnglrSGHERI += GSJRGerhglshergsehrgSGE) != 0);
 
  return (GSJRGerhglshergsehrgSGE == -1) ? jieurlhigosrlijhoegterlehogti : 1.0 / jieurlhigosrlijhoegterlehogti;
}

Name: Anonymous 2011-12-28 11:56

>>3
lol but does it really work or you just made some shit?

Name: edo-chan 2011-12-28 12:03

as expected its not woking =(

Main.java:2: error: class, interface, or enum expected
    public static Double anus(Double SGERFWGERHEEETHHHHHHHWETHG, int SDGIKgrnsjsnglrSGHERI){
                  ^
Main.java:5: error: class, interface, or enum expected
      Double jieurlhigosrlijhoegterlehogti = 1.0;
      ^
Main.java:6: error: class, interface, or enum expected
      int GSJRGerhglshergsehrgSGE = (SDGIKgrnsjsnglrSGHERI > 0) ? -1 : 1;
      ^
Main.java:8: error: class, interface, or enum expected
      do { jieurlhigosrlijhoegterlehogti *= SGERFWGERHEEETHHHHHHHWETHG;
      ^
Main.java:9: error: class, interface, or enum expected
      } while((SDGIKgrnsjsnglrSGHERI += GSJRGerhglshergsehrgSGE) != 0);
      ^
Main.java:11: error: class, interface, or enum expected
      return (GSJRGerhglshergsehrgSGE == -1) ? jieurlhigosrlijhoegterlehogti : 1.0 / jieurlhigosrlijhoegterlehogti;
      ^
Main.java:12: error: class, interface, or enum expected
    }
    ^
7 errors

Name: Anonymous 2011-12-28 12:04

Here's an ENTERPRISE-COMPLIANT OBJECT-ORIENTED SOLUTION:

(defgeneric power (x n))
(defmethod power (x (n (eql 0))) 1)
(defmethod power (x n) (* x (power x (1- n))))

Name: Anonymous 2011-12-28 12:42

>>6
Beautiful.

Name: Anonymous 2011-12-28 12:43

>>3 is creating two new variables inside the method, I think it works, but it's not answering the challenge.


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;

    double i = 1.0;
    int b = (n > 0) ? -1 : 1;

    do {
      i *= x;
    } while((n += b) != 0);

    return (b== -1) ? i : 1.0 / i;
  }
}


I cleaned it up, and it looks like this, and it works, at least for the tests I tried.

But yeah, I also can't do it without declaring a new variable.

Beware that we need to:
- Use Java;
- Create a method that does x^n, x and n being its two only arguments;

Beware that we can't:
- Use Math library;
- Create a new variable inside the method;
- Do "x^n", whatever that means;

Name: Anonymous 2011-12-28 13:11

The jews are no longer after me.

Name: Anonymous 2011-12-28 13:12

>>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);
  }
}

Name: Anonymous 2011-12-28 13:12

>>8
space between function name (
The authors of ``Code Conventions for the Java Programming Language'' frown upon you.

Name: matth@eecs.berkeley.edu !!8TAzbhVOfn9F0d0 2011-12-28 13:16

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.

Name: matth@eecs.berkeley.edu !!8TAzbhVOfn9F0d0 2011-12-28 13:17

>>10
There's a much easier solution.

Name: Anonymous 2011-12-28 13:22

>>11
Also the class name should be in CamelCase.

Name: Anonymous 2011-12-28 13:22

You can use foldl' in sun java 1.8:

\x -> foldl' (const . (x*)) 1 . enumFromTo 1

Name: Anonymous 2011-12-28 13:28

>>13

then give it you rectum

Name: Anonymous 2011-12-28 13:28

or even cleaner with JBeans lib:

flip ((product .) . replicate)

Name: VIPPER 2011-12-28 14:00

Dont help ''noko``, let him do his own homework.

Name: Anonymous 2011-12-28 14:12

foldr ($) 1 . (`replicate` (*x))

Name: Anonymous 2011-12-28 14:35


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;
    }
}

Name: Anonymous 2011-12-28 14:50

>>20

nods and claps

Name: sage 2011-12-28 15:00

power(int x, int n){
return n < 1 ? x : power(--x, --n) * x;
}

Name: Anonymous 2011-12-28 16:42

>>22
ternary operator evaluates its arguments, fagstrum

Name: Anonymous 2011-12-28 16:55

>>23

No, that is still valid. The only difference between ternary op and if is that it gets to an expression and all.

Name: Anonymous 2011-12-28 17:02

int power(int x, int n)
{
  return (n == 1) ? x : x * power(x, n - 1);
}

Name: Anonymous 2011-12-28 17:13

>>25
This is not CORRECT.

First of all, it handles n=0 incorrectly. Secondly, the argument n should be unsigned. Fixed:

intmax_t power(intmax_t x, uintmax_t n) { return (n == 0) ? 1 : (n == 1) ? x : x * power(x, n - 1); }

Name: sage 2011-12-28 17:18

>>26
this is Java, there is no unsigned in Java

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: Anonymous 2011-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;
}

Name: Anonymous 2011-12-28 17:35

here is a zomg optimized log(n) multiplications:

identities being used:

(x)^(2n+1) = (x^(2n))*x
(x)^(2n) = (x^n)^2 = (x^n)*(x^n)


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;
  }
}

Name: Anonymous 2011-12-28 17:38

>>28
Nvm x changes value, would not work.

Name: Anonymous 2011-12-28 17:45

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: Anonymous 2011-12-28 17:50

>>10 is the best answer, simple with recursion.
>>31 creates new variable
>>29 creates new variable

Name: Anonymous 2011-12-28 18:03




7^(35) = 7^(1 + 2 + 32)
       = 7^1 * 7^2 * 7^32
       = 7^1 * (7^1 * 7^16)^2
       = 7^1 * (7^1 * (7^8)^2)^2
       = 7^1 * (7^1 * ((7^4)^2)^2)^2
       = 7^1 * (7^1 * (((7^2)^2)^2)^2)^2
       = 7^1 * (7^1 * ((((7^1)^2)^2)^2)^2)^2
       = 7 * (7 * (((7^2)^2)^2)^2)^2

square(x) return x*x

Name: Anonymous 2011-12-28 18:04

>>5
Do you even know java?

Is this your first java program?

Of course it doesn't work like that. all he did was provide a method. In order to compile it you need to put it in a class file you moron.

Name: Anonymous 2011-12-28 18:06

>>32

are helper functions allowed?


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));
  }
}

Name: Anonymous 2011-12-28 18:10

>>35
p & ~1
NOT YOU AGAIN

Name: Anonymous 2011-12-28 18:13

>>36


state &= ~CALM_BIT;
state |= TROLLED_BIT;

Name: Anonymous 2011-12-28 18:15

A method computing x^n without declaring variables or using Math libraries (or even integers):
λx.λn.nx
GEEZ THAT WAS SO DIFFICULT

Name: Anonymous 2011-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: Anonymous 2011-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;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List