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-29 23:52

>>182

It can be useful for shifting back and forth between a bunch of different functions. IE, try doing this in a loop:


int f(int x, int y) {
  if(x & 1) {
    return f(y, x);
  } else {
    return g(x - y, x + y, x);
  }
}

int g(int x, int y, int z) {
  switch((x + y)%(z + 1)) {
    case 16: return f(1, z);
    case 1: return 6;
    case 135: return g(1, 1, z);
    default: return h(x + z);
  }
}

int h(int x) {
  if(x % 9 == 3) {
    h(x % 7);
  } else {
    f(x % 7, x % 11);
  }
}


But also imagine that these functions make sense. This might be hard to follow, but it is perfectly manageable to deal with these definitions mathematically. If you were to try to implement this using a single loop (and you can but it requires inlining it all into a single function), it becomes impossible to follow. It is still complicated of course, but the logic transfers so to speak have names, f g and h. Here is the loop, assuming the first call comes from f.


int f(int x, int y) {
  if(x & 1) {
    return f(y, x);
  } else {
    return g(x - y, x + y, x);
  }
}
int g(int x, int y, int z) {
  switch((x + y)%(z + 1)) {
    case 16: return f(1, z);
    case 1: return 6;
    case 135: return g(1, 1, z);
    default: return h(x + z);
  }
}
int h(int x) {
  if(x % 9 == 3) {
    return h(x % 7);
  } else {
    return f(x % 7, x % 11);
  }
}

--->>

int f(int fx, int fy) {
  int gx, gy, gz, hx;
f_label:
  if(x & 1) {
    //return f(y, x);
    int temp1 = y;
    int temp2 = x;
    x = temp1;
    y = temp2;
    goto f_label;
  } else {
    //return g(x - y, x + y, x);
    gx = x - y;
    gy = x + y;
    gz = x;
    goto g_label;
  }
g_label:
  switch((gx + gy)%(gz + 1)) {
    case 16: //return f(1, z);
             x = 1;
             z = gz;
             goto f_label;
    case 1: return 6;
    case 135: //return g(1, 1, z);
              gx = 1;
              gy = 1;
              //gz is fixed.
              goto g_label;
    default: //return h(x + z);
             hx = gx + gz;
             goto h_label;
  }
h_label:
  if(hx % 9 == 3) {
    //return h(x % 7);
    hx = hx % 7;
    goto h_label;
  } else {
    //return f(x % 7, x % 11);
    fx = hx % 7;
    fy = hx % 11;
    goto f_label;
  }
}

--->>

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