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

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