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

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