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

Pages: 1-4041-8081-120121-160161-

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

Name: Anonymous 2011-12-28 18:25

>>38

I don't get it. If x and n are both single argument lambda thingies, how do you define addition, multiplication and the like?

Name: Anonymous 2011-12-28 18:32

NEED URGENT ANUS

Name: Anonymous 2011-12-28 18:35

>>39
ENTERPRISE QUALITY JAVA


#ifdef __STDC__
        double pow(double x, double y)  /* wrapper pow */
#else
        double pow(x,y)                 /* wrapper pow */
        double x,y;
#endif
{
#ifdef _IEEE_LIBM
        return  __ieee754_pow(x,y);
#else
        double z;
        z=__ieee754_pow(x,y);
        if(_LIB_VERSION == _IEEE_|| isnan(y)) return z;
        if(isnan(x)) {
            if(y==0.0)
                return __kernel_standard(x,y,42); /* pow(NaN,0.0) */
            else
                return z;
        }
        if(x==0.0){
            if(y==0.0)
                return __kernel_standard(x,y,20); /* pow(0.0,0.0) */
            if(finite(y)&&y<0.0)
                return __kernel_standard(x,y,23); /* pow(0.0,negative) */
            return z;
        }
        if(!finite(z)) {
            if(finite(x)&&finite(y)) {
                if(isnan(z))
                    return __kernel_standard(x,y,24); /* pow neg**non-int */
                else
                    return __kernel_standard(x,y,21); /* pow overflow */
            }
        }
        if(z==0.0&&finite(x)&&finite(y))
            return __kernel_standard(x,y,22); /* pow underflow */
        return z;
#endif
}

Name: Anonymous 2011-12-28 18:38


#ifdef __STDC__
static const double
#else
static double
#endif
bp[] = {1.0, 1.5,},
dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */
dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */
zero    =  0.0,
one     =  1.0,
two     =  2.0,
two53   =  9007199254740992.0,  /* 0x43400000, 0x00000000 */
huge    =  1.0e300,
tiny    =  1.0e-300,
        /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
L1  =  5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
L2  =  4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
L3  =  3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
L4  =  2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
L5  =  2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
L6  =  2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
P1   =  1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
P2   = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
P3   =  6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
P4   = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
P5   =  4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
lg2  =  6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
lg2_h  =  6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
lg2_l  = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
ovt =  8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
cp    =  9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
cp_h  =  9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
cp_l  = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/
ivln2    =  1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
ivln2_h  =  1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/
ivln2_l  =  1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/

#ifdef __STDC__
        double __ieee754_pow(double x, double y)
#else
        double __ieee754_pow(x,y)
        double x, y;
#endif
{
        double z,ax,z_h,z_l,p_h,p_l;
        double y1,t1,t2,r,s,t,u,v,w;
        int i0,i1,i,j,k,yisint,n;
        int hx,hy,ix,iy;
        unsigned lx,ly;

        i0 = ((*(int*)&one)>>29)^1; i1=1-i0;
        hx = __HI(x); lx = __LO(x);
        hy = __HI(y); ly = __LO(y);
        ix = hx&0x7fffffff;  iy = hy&0x7fffffff;

    /* y==zero: x**0 = 1 */
        if((iy|ly)==0) return one;

    /* +-NaN return x+y */
        if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) ||
           iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0)))
                return x+y;

    /* determine if y is an odd int when x < 0
     * yisint = 0       ... y is not an integer
     * yisint = 1       ... y is an odd int
     * yisint = 2       ... y is an even int
     */
        yisint  = 0;
        if(hx<0) {
            if(iy>=0x43400000) yisint = 2; /* even integer y */
            else if(iy>=0x3ff00000) {
                k = (iy>>20)-0x3ff;        /* exponent */
                if(k>20) {
                    j = ly>>(52-k);
                    if((j<<(52-k))==ly) yisint = 2-(j&1);
                } else if(ly==0) {
                    j = iy>>(20-k);
                    if((j<<(20-k))==iy) yisint = 2-(j&1);
                }
            }
        }

    /* special value of y */
        if(ly==0) {
            if (iy==0x7ff00000) {       /* y is +-inf */
                if(((ix-0x3ff00000)|lx)==0)
                    return  y - y;      /* inf**+-1 is NaN */
                else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */
                    return (hy>=0)? y: zero;
                else                    /* (|x|<1)**-,+inf = inf,0 */
                    return (hy<0)?-y: zero;
            }
            if(iy==0x3ff00000) {        /* y is  +-1 */
                if(hy<0) return one/x; else return x;
            }
            if(hy==0x40000000) return x*x; /* y is  2 */
            if(hy==0x3fe00000) {        /* y is  0.5 */
                if(hx>=0)       /* x >= +0 */
                return sqrt(x);
            }
        }

        ax   = fabs(x);
    /* special value of x */
        if(lx==0) {
            if(ix==0x7ff00000||ix==0||ix==0x3ff00000){
                z = ax;                 /*x is +-0,+-inf,+-1*/
                if(hy<0) z = one/z;     /* z = (1/|x|) */
                if(hx<0) {
                    if(((ix-0x3ff00000)|yisint)==0) {
                        z = (z-z)/(z-z); /* (-1)**non-int is NaN */
                    } else if(yisint==1)
                        z = -1.0*z;             /* (x<0)**odd = -(|x|**odd) */
                }
                return z;
            }
        }

        n = (hx>>31)+1;

    /* (x<0)**(non-int) is NaN */
        if((n|yisint)==0) return (x-x)/(x-x);

        s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
        if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */

    /* |y| is huge */
        if(iy>0x41e00000) { /* if |y| > 2**31 */
            if(iy>0x43f00000){  /* if |y| > 2**64, must o/uflow */
                if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny;
                if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny;
            }
        /* over/underflow if x is not close to one */
            if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny;
            if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny;
        /* now |1-x| is tiny <= 2**-20, suffice to compute
           log(x) by x-x^2/2+x^3/3-x^4/4 */
            t = ax-one;         /* t has 20 trailing zeros */
            w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25));
            u = ivln2_h*t;      /* ivln2_h has 21 sig. bits */
            v = t*ivln2_l-w*ivln2;
            t1 = u+v;
            __LO(t1) = 0;
            t2 = v-(t1-u);
        } else {
            double ss,s2,s_h,s_l,t_h,t_l;
            n = 0;
        /* take care subnormal number */
            if(ix<0x00100000)
                {ax *= two53; n -= 53; ix = __HI(ax); }
            n  += ((ix)>>20)-0x3ff;
            j  = ix&0x000fffff;
        /* determine interval */
            ix = j|0x3ff00000;          /* normalize ix */
            if(j<=0x3988E) k=0;         /* |x|<sqrt(3/2) */
            else if(j<0xBB67A) k=1;     /* |x|<sqrt(3)   */
            else {k=0;n+=1;ix -= 0x00100000;}
            __HI(ax) = ix;

        /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
            u = ax-bp[k];               /* bp[0]=1.0, bp[1]=1.5 */
            v = one/(ax+bp[k]);
            ss = u*v;
            s_h = ss;
            __LO(s_h) = 0;
        /* t_h=ax+bp[k] High */
            t_h = zero;
            __HI(t_h)=((ix>>1)|0x20000000)+0x00080000+(k<<18);
            t_l = ax - (t_h-bp[k]);
            s_l = v*((u-s_h*t_h)-s_h*t_l);
        /* compute log(ax) */
            s2 = ss*ss;
            r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6)))));
            r += s_l*(s_h+ss);
            s2  = s_h*s_h;
            t_h = 3.0+s2+r;
            __LO(t_h) = 0;
            t_l = r-((t_h-3.0)-s2);
        /* u+v = ss*(1+...) */
            u = s_h*t_h;
            v = s_l*t_h+t_l*ss;
        /* 2/(3log2)*(ss+...) */
            p_h = u+v;
            __LO(p_h) = 0;
            p_l = v-(p_h-u);
            z_h = cp_h*p_h;             /* cp_h+cp_l = 2/(3*log2) */
            z_l = cp_l*p_h+p_l*cp+dp_l[k];
        /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
            t = (double)n;
            t1 = (((z_h+z_l)+dp_h[k])+t);
            __LO(t1) = 0;
            t2 = z_l-(((t1-t)-dp_h[k])-z_h);
        }

    /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
        y1  = y;
        __LO(y1) = 0;
        p_l = (y-y1)*t1+y*t2;
        p_h = y1*t1;
        z = p_l+p_h;
        j = __HI(z);
        i = __LO(z);
        if (j>=0x40900000) {                            /* z >= 1024 */
            if(((j-0x40900000)|i)!=0)                   /* if z > 1024 */
                return s*huge*huge;                     /* overflow */
            else {
                if(p_l+ovt>z-p_h) return s*huge*huge;   /* overflow */
            }
        } else if((j&0x7fffffff)>=0x4090cc00 ) {        /* z <= -1075 */
            if(((j-0xc090cc00)|i)!=0)           /* z < -1075 */
                return s*tiny*tiny;             /* underflow */
            else {
                if(p_l<=z-p_h) return s*tiny*tiny;      /* underflow */
            }
        }
    /*
     * compute 2**(p_h+p_l)
     */
        i = j&0x7fffffff;
        k = (i>>20)-0x3ff;
        n = 0;
        if(i>0x3fe00000) {              /* if |z| > 0.5, set n = [z+0.5] */
            n = j+(0x00100000>>(k+1));
            k = ((n&0x7fffffff)>>20)-0x3ff;     /* new k for n */
            t = zero;
            __HI(t) = (n&~(0x000fffff>>k));
            n = ((n&0x000fffff)|0x00100000)>>(20-k);
            if(j<0) n = -n;
            p_h -= t;
        }
        t = p_l+p_h;
        __LO(t) = 0;
        u = t*lg2_h;
        v = (p_l-(t-p_h))*lg2+t*lg2_l;
        z = u+v;
        w = v-(z-u);
        t  = z*z;
        t1  = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
        r  = (z*t1)/(t1-two)-(w+z*w);
        z  = one-(r-z);
        j  = __HI(z);
        j += (n<<20);
        if((j>>20)<=0) z = scalbn(z,n); /* subnormal output */
        else __HI(z) += (n<<20);
        return s*z;
}

Name: >>38 2011-12-28 18:39

the only one I've seen so far is a representation for numbers as a stack of dinner plates, and addition looks like:

|||||| + |||||||||
= ||||||| + ||||||||
= |||||||| + |||||||
= ||||||||| + ||||||
= |||||||||| + |||||
= ||||||||||| + ||||
= |||||||||||| + |||
= ||||||||||||| + ||
= |||||||||||||| + |
= ||||||||||||||| + nil
= |||||||||||||||

subtraction:

|||||||||| - |||||||||
= ||||||||| - ||||||||
= |||||||| - |||||||
= ||||||| - ||||||
= |||||| - |||||
= ||||| - ||||
= |||| - |||
= ||| - ||
= || - |
= | - nil
= |

Name: Anonymous 2011-12-28 18:44

>>44

__kernal_standard

            case 20:
                /* pow(0.0,0.0) */
                /* error only if _LIB_VERSION == _SVID_ */
                exc.type = DOMAIN;
                exc.name = "pow";
                exc.retval = zero;
                if (_LIB_VERSION != _SVID_) exc.retval = 1.0;
                else if (!matherr(&exc)) {
                        (void) WRITE2("pow(0,0): DOMAIN error\n", 23);
                        errno = EDOM;
                }
                break;
            case 21:
                /* pow(x,y) overflow */
                exc.type = OVERFLOW;
                exc.name = "pow";
                if (_LIB_VERSION == _SVID_) {
                  exc.retval = HUGE;
                  y *= 0.5;
                  if(x<zero&&rint(y)!=y) exc.retval = -HUGE;
                } else {
                  exc.retval = HUGE_VAL;
                  y *= 0.5;
                  if(x<zero&&rint(y)!=y) exc.retval = -HUGE_VAL;
                }
                if (_LIB_VERSION == _POSIX_)
                  errno = ERANGE;
                else if (!matherr(&exc)) {
                        errno = ERANGE;
                }
                break;
            case 22:
                /* pow(x,y) underflow */
                exc.type = UNDERFLOW;
                exc.name = "pow";
                exc.retval =  zero;
                if (_LIB_VERSION == _POSIX_)
                  errno = ERANGE;
                else if (!matherr(&exc)) {
                        errno = ERANGE;
                }
                break;
            case 23:
                /* 0**neg */
                exc.type = DOMAIN;
                exc.name = "pow";
                if (_LIB_VERSION == _SVID_)
                  exc.retval = zero;
                else
                  exc.retval = -HUGE_VAL;
                if (_LIB_VERSION == _POSIX_)
                  errno = EDOM;
                else if (!matherr(&exc)) {
                  if (_LIB_VERSION == _SVID_) {
                        (void) WRITE2("pow(0,neg): DOMAIN error\n", 25);
                      }
                  errno = EDOM;
                }
                break;
            case 24:
                /* neg**non-integral */
                exc.type = DOMAIN;
                exc.name = "pow";
                if (_LIB_VERSION == _SVID_)
                    exc.retval = zero;
                else
                    exc.retval = zero/zero;     /* X/Open allow NaN */
                if (_LIB_VERSION == _POSIX_)
                   errno = EDOM;
                else if (!matherr(&exc)) {
                  if (_LIB_VERSION == _SVID_) {
                        (void) WRITE2("neg**non-integral: DOMAIN error\n", 32);
                      }
                  errno = EDOM;
                }
                break;
            case 42:
                /* pow(NaN,0.0) */
                /* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
                exc.type = DOMAIN;
                exc.name = "pow";
                exc.retval = x;
                if (_LIB_VERSION == _IEEE_ ||
                    _LIB_VERSION == _POSIX_) exc.retval = 1.0;
                else if (!matherr(&exc)) {
                        errno = EDOM;
                }
                break;

Name: Anonymous 2011-12-28 18:44

>>41
Terrible!
If we define the natural number n to be the functional exponentiation of a procedure f, we get 0 = λf.λx.x = KI; 1 = λf.λx.fx = λf.f = I; 2 = λf.λx.f(fx); 3 = λf.λx.f(f(fx)); ....
succ n = n + 1 is defined as λn.λf.λx.f(nfx), because fn+1(x) = f(fn(x)).
n+m is just applying n times succ to m, n+m = succn(m) = λn.λm.n succ m.
n*m is either defined by applying n times m to a function f, n*m = (fm)n = λn.λm.λf.n (m f), or by applying n times +m to 0, n*m = λn.λm.m (+n) 0.
n^m is applying n times applying m times a function f, similar to multiplication: n^m = fnm = λn.λm.λf.(mn)f = λn.λm.mn

Name: >>41,45 2011-12-28 19:07

>>45's name should have been 41, my bad.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-28 20:00

I feel like I'm watching the special olypmics of computer programming.

Name: Anonymous 2011-12-28 20:10

>>49
kodak-kun! You're back! Hax my anus!

Name: Anonymous 2011-12-28 21:01

>>49

we all have to start somewhere kodak-kun.

Name: Anonymous 2011-12-29 5:14

kodak-kunt never contributes anything other than scorn for others.

Name: Anonymous 2011-12-29 6:38

>>52
What would he contribute anyway? He's not a competent programmer.

Name: Anonymous 2011-12-29 6:40

>>55
nice dubs bro

Name: Anonymous 2011-12-29 6:53

>>54
Thank you.

Name: Anonymous 2011-12-29 9:15

public class file {
    public static void main(String arg[]){
        System.out.println(powerup(Integer.parseInt(arg[0]),Integer.parseInt(arg[1])));
    }  
    private static int powerup(int x, int n){
        if (n == 1){
            return x;
        }else if (n == 0){
            return 1;
        }else{
            return x * powerup(x, n-1);
        }  
    }  
}  

yeah I'm new here so I don't know how to monospace this.

Name: Anonymous 2011-12-29 9:20



public class file {
    public static void main(String arg[]){
        System.out.println(powerup(Integer.parseInt(arg[0]),Integer.parseInt(arg[1])));
    }  
    private static int powerup(int x, int n){
        if (n == 1){
            return x;
        }else if (n == 0){
            return 1;
        }else{
            return x * powerup(x, n-1);
        }  
    }  
}


That should be better. Btw, are there any extensions similar to 4chan X for the BBSes?

Name: Anonymous 2011-12-29 10:03

power :: (Num a, Integral b) => a -> b -> a
power a b = getProduct . mconcat . map Product $ genericReplicate b a

-- (^) :: (Num a, Integral b) => a -> b -> a

Name: Anonymous 2011-12-29 10:12

>>58
getProduct . mconcat . map Product . (. flip genericReplicate)?

Name: Anonymous 2011-12-29 10:24

>>59
It would be better to apply the Product before the genericReplicate.

Name: IisMathwizard 2011-12-29 13:56

Not sure if OP is retarded or troll...

Name: IisMathwizard 2011-12-29 14:11

// @Author: IisMathwizard

public class someClass{
    static int count = 0; //OP Said not within the method so
    // global
    public static void main(){
        System.out.println(powerUp(10,12) + "");
        }
    public static int powerUp(int x, int n){
        count = 0;
        for(;count < n-1; count ++){
            x*=x;
            }
        return x;
        }
    }

Name: IisMathwizard 2011-12-29 14:18

**Quick edit to allow for entering of zero and negatives
// @Author: IisMathwizard

public class someClass{
    static int count = 0;//OP Said not within the method so
    static boolean neg = false; // global
    public static void main(){
        System.out.println(powerUp(10,12) + "");
        }
    public static double powerUp(int x, int n){
        count = 0;
        neg = false;
        if(n == 0){
            return 1;
        }
        if(n < 0){
            n = -1 *n;
            neg = true;
            }
        for(;count < n-1; count ++){
            x*=x;
            }
        if(neg){
            return (1/(double)x);
            }
        return x;
        }
    }

Name: Anonymous 2011-12-29 14:57

>>62,63
that + "" in your println is completely unnecessary. Also, making your variables global is purposely misinterpreting the question.
Yes, OP could have phrased it more clearly.
No, you're not smart for purposely misinterpreting it.
Take a look at one of the recursive examples above, that's the way it should be done

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 15:41

1. It is Necessary, it will not typecast to string for you so you must force it with a ' + "" '
2. I was reading what was giving...
3. true
4. Not trying to be asshole
5. What about x = 2 n = 560? STACK OVERLOAD I like the for loop better

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:07

>>65
I can't tell if you're trolling or just plain dumb.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:10

>>65
Also you uneducated toilet scrubber..

5. What about x = 2 n = 560? STACK OVERLOAD I like the for loop better

Masks one of the loop invariants. Man you're hella dumb. I see no real possible future for you as a computer better. The best you might be able to do is tech support at some hick firm. Geeze, do us all a favor and just go kill yourself.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:13

At least 1 holds up while using Netbeans IDE

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:15

I dare you to try x=2 and n=560 and show me a screenshot of both code and output and tell me that it's not going to have a stack error
I DARE YOU

Name: Anonymous 2011-12-29 16:15

>>1

POWER MY ANUS

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:19

>>67
Watch out we got a bad ass over here!

Name: Anonymous 2011-12-29 16:20

mailto:noko versus Kodak Gallery Autist

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:22

Sorry about that.. It's my default cause im usually on /g/ or /a/ or /c/ or... well i could go on and on

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:22

>>68
Using something like netbeans is a lame substitute for an editor like emacs and a proper makefile. But of your monkey ass wouldn't know something like this. Also, that is a for loop. Not a while loop. While they might look they same the surface, they really aren't (in Java).

Again, you're dumb. And again, I see no real future for you as a computer programming.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:24

>>71
No, but at least I know enough to be able to do this kind of crap for a living.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:25

>>71
And now, by all means, tell us all what you do for a living. Tell us about the software you helped write that is used by over 40 million people.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:26

So would you point me in the direction of the while loop because i remember posting about is the for loop... And netbeans is decent IDE for when you need things done quickly... I'd normally type it in notepad but i have Netbeans open for a project at the time of reading...

Name: Anonymous 2011-12-29 16:26

>>75
You actually convinced somebody to pay you for writing broken pow implementations in Java? AMAZING.

Name: Anonymous 2011-12-29 16:27

>>74-76
Fuck off and die you cock sucking faggot.

Name: Anonymous 2011-12-29 16:29

>>74
Kodak-san uses Emacs, my nigga right here.

Name: Anonymous 2011-12-29 16:29

>>67
Man you're hella dumb.

you just made me miss norcal so bad right now...

Name: Anonymous 2011-12-29 16:30

>>81
He hella mad brah.

Name: Anonymous 2011-12-29 16:30

>>78
No, but someone did pay me, and a lot I might add, to help with the programming part of SMART FIT technology.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:30

Do i even have to argue anymore? it seems it's being done for me....

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:33

>>84
You have no ground to stand on. You're technical responses are weak at the very best and you still haven't told any of us what you do for a living. Are you ashamed of the fact that you have a general labor job?

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:36

I don't do anything for a living... im a student and proud of it... i do have one question to ask you
>>85
When you worked on that kodak thingy-muh-bobbers did you actually "code " it or did you just pressed the button that said compile...

Name: Anonymous 2011-12-29 16:37

>>83
So you're rich and can have as many KODAK MOMENTS (tm) as you want, yet here you are, browsing /prog/.

Name: Anonymous 2011-12-29 16:38

>>73
/g/ /a/ /c/
please go back to the imageboards you faggot

this is for real programmers

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:39

. . . .. . . . . . . . . . . ,.-‘”. . . . . . . . . .``~.,
. . . . . . . .. . . . . .,.-”. . . . . . . . . . . . . . . . . .“-.,
. . . . .. . . . . . ..,/. . . . . . . . . . . . . . . . . . . . . . . ”:,
. . . . . . . .. .,?. . . . . . . . . . . . . . . . . . . . . . . . . . .\,
. . . . . . . . . /. . . . . . . . . . . . . . . . . . . . . . . . . . . . ,}
. . . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . . ,:`^`.}
. . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . ,:”. . . ./
. . . . . . .?. . . __. . . . . . . . . . . . . . . . . . . . :`. . . ./
. . . . . . . /__.(. . .“~-,_. . . . . . . . . . . . . . ,:`. . . .. ./
. . . . . . /(_. . ”~,_. . . ..“~,_. . . . . . . . . .,:`. . . . _/
. . . .. .{.._$;_. . .”=,_. . . .“-,_. . . ,.-~-,}, .~”; /. .. .}
. . .. . .((. . .*~_. . . .”=-._. . .“;,,./`. . /” . . . ./. .. ../
. . . .. . .\`~,. . ..“~.,. . . . . . . . . ..`. . .}. . . . . . ../
. . . . . .(. ..`=-,,. . . .`. . . . . . . . . . . ..(. . . ;_,,-”
. . . . . ../.`~,. . ..`-.. . . . . . . . . . . . . . ..\. . /\
. . . . . . \`~.*-,. . . . . . . . . . . . . . . . . ..|,./.....\,__
,,_. . . . . }.>-._\. . . . . . . . . . . . . . . . . .|. . . . . . ..`=~-,
. .. `=~-,_\_. . . `\,. . . . . . . . . . . . . . . . .\
. . . . . . . . . .`=~-,,.\,. . . . . . . . . . . . . . . .\
. . . . . . . . . . . . . . . . `:,, . . . . . . . . . . . . . `\. . . . . . ..__
. . . . . . . . . . . . . . . . . . .`=-,. . . . . . . . . .,%`>--==``
. . . . . . . . . . . . . . . . . . . . _\. . . . . ._,-%. . . ..`

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:40

>>86

>did you just pressed the button that said compile...

You're also naive to the real world. Has anyone ever shown you have code gets built out in the real world? Maybe you should see if you can take a tour of IBM or Microsoft you halfwit.

Also, I wrote some code, because that is what I'm getting paid to do. Now do society a favor and not become a programmer. For whatever reason, this doesn't seem to be your think. Maybe you should take up something a bit easier. Say like being a business major.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:41

I do believe that i am a real programmer. I only browse /a/ /c/ /g/ because i do appreciate a good fan art of the manga variety and the technology board isn't that bad.

Name: Anonymous 2011-12-29 16:43

>>91
You aren't a real programmer until you do the stuff for a living you idiot. I hate to break it to you, but there is a vast difference between writing simple toys programs found in your typical computer programming books and writing real world programs.

Name: Anonymous 2011-12-29 16:43

Hey kodak, how many times did the SMART FIT bug out and fill your albums with creepy stock photos of black children playing while adults watch in the background smiling? Before you got it right, that is.

Name: isMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:44

>>90
Just asking an honest question... Wasn't sure given your knowledge of the subject.I wouldn't want to go to microsoft but IBM sounds nice... Unfortunately i don't live close to either so ill have to take you up on your offer later. Though i will be a programmer for Google upon graduation. If you would like in a couple of years you can stop by Googleplex and i give you a free tour of Google.

Name: Anonymous 2011-12-29 16:44

>>92
Do you seriously still have to type your entire name and tripcode every time you post? What browser do you use Kodak-san?

Name: Anonymous 2011-12-29 16:45

>>93
Hush you.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:46

>>91
Well then i'd like a correction.. I'm am an aspiring programmer... Does that fit better?

Name: Anonymous 2011-12-29 16:47

>>96
Because it happened to me once, is there a place I can file bug reports?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:48

>>94
I doubt it. You still can't figure out how to solve the OP's problem. By the way, it can be done without recursion, and yes, you don't need to introduce any new variables! Geeze, if you can't figure out a somewhat easy puzzle, how the hell do you expect to be able to answer the really challenging questions during the Google interview?

Name: Anonymous 2011-12-29 16:48

>>93
Stock photos always creep me out like that, it's like the adults are there smiling at this random kid who they don't even know.

Name: Anonymous 2011-12-29 16:48

>>95
I use IE.

Name: --- 2011-12-29 16:48

Check em' binary literal for a prime number.

Name: Anonymous 2011-12-29 16:49

>>101
So IE doesn't offer you auto-fill on /prog/, or did you turn it off?

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:50

i know it can be done without recursion. Thank you for your concern in my future affairs with google. It is nice to know that people here are helpful enough to provide suggestions and go over this ordeal with me. I am an aspiring programmer i know this to be true and i don't understand how someone who has worked long and hard on such successful software as SmartFit (TM) can't understand a simple statement as such...

Name: Anonymous 2011-12-29 16:50

>>103
I turned it off.

Name: Anonymous 2011-12-29 16:51

>>104
Then why can't you write it? The solution you posted isn't correct.

Name: Anonymous 2011-12-29 16:52

>>104
Well, again, at least I know enought about programming to be able to do it for a living. And what the hell? You still use notepad? Are you too stupid to learn a proper editor like vi or emacs?

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:52

By what do you mean?? Ohh... yes i noticed there was an error in their later on... i rewrote it but i was in such a heated discussion i hadn't reposted it...

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 16:53

>>108
YOUR GOD DAMN PROGRAM ISN'T THE CORRECT SOLUTION. WHICH PART OF THIS DON'T YOU UNDERSTAND YOU STUPID FUCKER?

Name: Anonymous 2011-12-29 16:54

>>69

the recursive ones that don't use tail recursion that also make p recursive calls will likely stack overflow for large values of p. But if you use a solution that only makes log(p) recursive calls, you are likely safe. Anyways, I think that one can be made tail recursive. You just have to go from the most significant bits down to the least significant bits:


unsigned long long square(unsigned long long x) {
  return x*x;
}

unsigned long long power_(unsigned long long x, unsigned long long p, unsigned long long bit_mask, unsigned long long accumulation_product) {
  if(bit_mask == 0) {
    return accumulation_product;
  } else if(p & (1<<bit_index)) {
    return power_(x, p & ~bit_mask, bit_mask, accumulation_product*x);
  } else {
    return power_(x, p, bit_mask>>1, square(accumulation_product));
  }
}


I should test this though. Also there is a chance to optimize the odd case.

(x)^(2n+1) = (x^(2n))*x = ((x^(n))^2)*x = x*square(power(x,n))

so it would go to the next bit, regardless if it was even or odd, rather than making the odd power even and relying on the next recursive call to go to the next bit.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:54

>>107
I use notepad because i don't want to be distracted by the features that modern day IDE's envelope one with. For example.. the problem with netbeans that you talked about earlier... System.out.println(someMethodReturningAnInt()); comes out as an error but when compiling command line and writing in notepad one does not have to deal with these falsifications

Name: Anonymous 2011-12-29 16:55

>>108
As an apology to Kodak-san I think you should make him a small Kodak Gallery Photo Book using Smart Fit technology, it's really affordable right now just $8.99 I think he would really like it.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:56

>>112
I will get the duck tape...

Name: Anonymous 2011-12-29 16:56

>>111
Uh.... please tell me that you're really not this god damn stupid.

Name: Anonymous 2011-12-29 16:56

>>110

wupsy, forgot the actual power function:


unsigned long long power(unsigned long long x, unsigned long long p) {
  return power_(x, p, ((unsigned long long)1)<<((sizeof(unsigned long long)*8)-1), 1);
}

Name: Anonymous 2011-12-29 16:56

>>112
Don't fill it with creepy stock photos of black children smiling I don't think he likes that.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 16:57

>>116
wouldn't dream of it
>>114
what do you mean?

Name: Anonymous 2011-12-29 17:00

>>117
You're stupid. You're justifications for using notepad reflect the fact that you really are ignorant to what goes on. Why don't you just fucking leave here and actually learn about editors.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:02

I have learned about the editors and i do, as mentioned a while back, have netbeans and eclipse. I use notepad because i am a fan of writing everything from scratch. i don't understand why not appreciating "extra features" is a crime???

Name: Anonymous 2011-12-29 17:02

>>63
You know the best thing about you trying to act like you know shit is that your code didn't event work properly, i even copied and pasted it just to make sure i didn't type it wrong:



[ Thu Dec 29 04:58:03 ]
[ @ ~/host/prog ] $ cat pow.java
public class pow
{
    public static void main(String args[])
    {
        int
            x = Integer.parseInt(args[0]),
            n = Integer.parseInt(args[1]);
        System.out.println("FOR:"+powi(x,n));
        System.out.println("REC:"+powr(x,n));
    }

    public static double powr(int x,int n)
    {
        if(x == 1 || n == 0) return 1.0;
        return n < 0 ? (1.0/(powr(x,-n-1)*x)) : powr(x,n-1)*x;
    }

    public static double powi(int x,int n)
    {
        int count = 0;
        boolean neg = false;
        if(n == 0){
            return 1;
        }
        if(n < 0){
            n = -1 *n;
            neg = true;
        }
        for(;count < n-1; count ++){
            x*=x; //<-- this is your poor mistake, so much for being good at Math
        }
        if(neg){
            return (1/(double)x);
        }
        return x;
    }
}
[ Thu Dec 29 04:59:38 ]
[ @ ~/host/prog ] $ java pow 2 2
FOR:4.0
REC:4.0
[ Thu Dec 29 04:59:42 ]
[ @ ~/host/prog ] $ java pow 2 4
FOR:256.0    <--- WRONG
REC:16.0
[ @ ~/host/prog ] $ java pow 10 12
FOR:0.0      <--- Your own example didn't even work
REC:1.0E12
[ Thu Dec 29 05:00:02 ]
[ @ ~/host/prog ] $ java pow 2 560
FOR:0.0
REC:3.7739624248215414E168    <-- So much for that stack overflow....

Name: Anonymous 2011-12-29 17:04

Kodak-san would you like to receive a Photo Book created with Smart Fit technology as a gift?

Name: Anonymous 2011-12-29 17:05

>>120

[ Thu Dec 29 05:04:42 ]
[ @ ~/host/prog ] $ java pow 2 10000
FOR:0.0
REC:Infinity



I have a feeling you don't know what causes stack overflows to begin with to even make you think pow(2,560) would overflow

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:05

>>120
As i have mentioned here please note for further engagement
>>108

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:07

I don't think that pow(2,560) would overflow
I think that a recursive function that was described during the conversation with x = 2 and n = 560 would overflow

Name: Anonymous 2011-12-29 17:08

>>124
It wouldn't, you're wrong, go back to your imageboards faggot and act like you know shit that you don't.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:10

>>125
... you realllllllllllly don't think that a RECURSIVE function looping on itself 560 times wouldn't overflow?

Name: Anonymous 2011-12-29 17:11

>>102
This epic failure has been archived.

Name: Anonymous 2011-12-29 17:11

>>126
LOL!s

Name: Anonymous 2011-12-29 17:12

>>126
Java uses a fucking VM where a stack frame takes like 2 bytes YOU STUPID FAGGOT!!

Name: Anonymous 2011-12-29 17:12

>>126
see >>120

public static double powr(int x,int n)
    {
        if(x == 1 || n == 0) return 1.0;
        return n < 0 ? (1.0/(powr(x,-n-1)*x)) : powr(x,n-1)*x;
    }

[ Thu Dec 29 05:04:42 ]
[ @ ~/host/prog ] $ java pow 2 10000
FOR:0.0
REC:Infinity
[ Thu Dec 29 05:00:02 ]
[ @ ~/host/prog ] $ java pow 2 560
FOR:0.0
REC:3.7739624248215414E168


you're retarded

Name: Anonymous 2011-12-29 17:14

>>119
Something like Netbeans isn't an editor alone. It's an editor, a compiler, a debugger, and a few other things. The fact that you keep saying it's an editor reflects that fact you are in fact stupid.

Also, there are other editors besides notepad. So again, you're stupid. And again, you have no real future a computer programmer.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:14

>>130
Because you can't read ill type REAL SLOW...
That code is OBVIOUSLY WRONG... I discovered this when i went to get cookies... I fixed but DIDN'T repost it... would you like the fixed version???

Name: Anonymous 2011-12-29 17:15

>>132
Something like Netbeans isn't an editor alone. It's an editor, a compiler, a debugger, and a few other things. The fact that you keep saying it's an editor reflects that fact you are in fact stupid.

Also, there are other editors besides notepad. So again, you're stupid. And again, you have no real future a computer programmer.

Name: Anonymous 2011-12-29 17:16

>>132
No sir, you're retarded. >>130 had nothing to do with your shitty mistake. It was showing you that you're horribly wrong to think it would stack overflow with the recursive method.

Stop trying to go off topic back to your shitty mistake and just face the facts.

By the time the recursive function even does overflow you've gone before infinity anyway thus making it worthless to even complain about

Name: Anonymous 2011-12-29 17:17

>>133
You gonna be hella mad when you get an invite to check out Google in a couple of years.

Name: Anonymous 2011-12-29 17:18

>>134
before infinity
beyond*

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:18

>>131
I believe that i specified it as an IDE. i understand what netbeans is and know of its features especially because i have it open as i type...

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 17:18

>>134
I didn't that post the code you idiot. I was responding to the other idiot who thinks Netbeans is an editor (alone).

Name: Anonymous 2011-12-29 17:19

>>137
Yes, it's an IDE. What that means is that not only is it an editor, but also a compiler, and a debugger. What part of this don't you understand you fucking moron?

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:19

>>135
glad to see someone still has a sense of humour

Name: Anonymous 2011-12-29 17:20

In this thread, the users of prog expose how insecure they are by attempting to belittle a student programmer. Don't you know that this behavior is unscientific and ultimately unproductive?

Name: Anonymous 2011-12-29 17:21

>>137,138,140,143-1000
Get a room already you fucking faggots.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:21

>>138
i do apologize.. i have way too many people attacking me right now.. I didn't realize that you were having a side conversation

Name: Anonymous 2011-12-29 17:22

>>142
Let's just hope google doesn't ask him what an editor is -(.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2011-12-29 17:23

>>143
I was just pointing out one of your many vast misconceptions. Again, computer programming doesn't seem to be your thing.

Name: Anonymous 2011-12-29 17:23

>>141
It's only Kodak-san and some other random retard, Kodak belittles everyone since he's like 5 feet tall or something.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:23

>>144
ouch... So quick poll... Who doesn't like me and for what reason(s)
so that i may solve this arguement clusterfuck

Name: Anonymous 2011-12-29 17:25

>>134

using stack space when it isn't necessary is always a waste of space and time, and should always be considered sub optimal, even when you have enough resources to afford it without crashing.

Name: Anonymous 2011-12-29 17:25

>>147
Just stop talking to them.

Name: Anonymous 2011-12-29 17:27

>>138
why did you reply that to >>134 when 134 has no relation to you, it as pointed at IisMathwizard


Are you trolling?

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:27

>>149
kthen... so does this mean that the arguements have been resolved
?

Name: Anonymous 2011-12-29 17:29

>>151
Yes you are new to programming, everyone has been at some point. Everybody makes mistakes.

Name: Anonymous 2011-12-29 17:31

>>147

you've found a situation that is very common on the internet, and in the business world, and in academia, where people get off to acting like they know more than others about some kind of distinguished subject. Kudos on not playing into it yourself there friend. In this test, you have stayed committed to science!

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:31

>>152
alright then... One last call... anyone else want to say something  about my programming skill/ mother/ Google dream's/ n00b status/ my unemployment (ima student if you hadn't read the comment about it before)

Name: Anonymous 2011-12-29 17:31

>>148
argues stack overflow
proven wrong
now argues waste of space and time
you just can't seem to admit you're wrong can you?
the power recursive is just as fast as the iterative as well you can go time it yourself the difference is less than a few hundred milliseconds for powers up to 10000.

The fact that you're arguing over space is just funny. elegance solutions > shit code any day.

If the JVM supported tail recursive like other languages it wouldn't even be a problem since it would be OPTIMIZED

Name: Anonymous 2011-12-29 17:32

The jews are after me.

Name: Anonymous 2011-12-29 17:33

>>154
could you change your name to fit something that actually makes sense. You're clearly not good at math nor a wizard by any means.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:35

>>157
no... I am most doubtful of your demoting comment. I am quite good at mathematics thank you.
>>155
I think we went over this already so im not going to reiterate it for you.

Because this came up a few times...:

public class someClass{
    static int count = 0;
    static boolean neg = false;
    static int v = 1;
    public static void main(String[] args){
        System.out.println(powerUp((int) Integer.parseInt(args[0]),(int)Integer.parseInt(args[1])) + "");
        }
    public static double powerUp(int x, int n){
        count = 0;
        neg = false;
        if(n < 0){
            n = -1 *n;
            neg = true;
            }
        for(;count < n; count++){
            v = v * x;
            }
        if(neg){
            return (1/(double)v);
            }
        return v;
        }
    }

Name: Anonymous 2011-12-29 17:37

>>158
still trying to strawman off topic back to your mistake?
Please stop posting about it, no one has cared about it nor did we ask for you to show a correct solution.

Name: IisMathwizard !!b/mIEkVC6+3Cvk1 2011-12-29 17:43

bye then. Happy coding...

Name: Anonymous 2011-12-29 17:44

>>155

pushing around a stack pointer a couple thousand times when you don't need to do will take more time than necessary. The difference may only be a constant factor, but you'll feel it if your routine is called heavily.

Name: Anonymous 2011-12-29 17:46

>>160
thank you, go back to your imageboards and never comes back.

>>161
If the JVM supported tail recursive like other languages it wouldn't even be a problem since it would be OPTIMIZED

yes recursion in java is shit at the moment when used for anything heavy and big but a simple problem like this which is most likely for simple academia work isn't going to matter

Name: Anonymous 2011-12-29 17:49

>>162
But it's best practice not to use recursion either way.

Name: Anonymous 2011-12-29 17:49

>>155

and almost none of the solutions posted in here made use of tail recursion. Tail recursive calls need to look like:


int f(int x) {
  return g(x - 1);
}


this can't be tail call optimized


int f(int x) {
  return x*g(x - 1);
}


It needs to call g, get the return value, and then multiply by x. That multiply by x prevents a tail call, which would, in one way or another, jump to g with x - 1 as a parameter and its return value would go to the caller of f.

Name: Anonymous 2011-12-29 17:51

>>164
Java doesn't support tail recursion, that's why no one made a solution that looked like that.

Name: Anonymous 2011-12-29 17:51

>>163

tail calls and nice, and they generalize of iteration with loops, but not too many programmers are familiar with it. When writing enterprise code, I'll write it out tail call style and then do a translation to loops sometimes. It helps get things correct the first time you write it.

Name: Anonymous 2011-12-29 17:52

>>165

It can't support tail calls to other functions, but any language that supports loops can support tail recursion.

Name: Anonymous 2011-12-29 17:53

IisMathwizard, congratulations. You won the prize of the most retarded and annoying /prog/ spammer up to date.
1. IisMathwizard.
2. kodak-kun.
3. ``in Lisp'' guy.
4. FrozenVoid.

Name: Anonymous 2011-12-29 17:53

>>167
Java can't do tail recursion.

Go ahead try it for yourself, make something tail recursive in java and make you'll see it overflow like normal recursion.

Name: Anonymous 2011-12-29 17:55

>>169

I said it can support tail recursion, not that it does.

Name: Anonymous 2011-12-29 17:56

>>170
will you suck on my big meaty balls now?

Name: Anonymous 2011-12-29 17:57

how did IisMathwizard Win?

Name: Anonymous 2011-12-29 18:08

>>171
why would this internet discussion battle affect my decision?
>>172
win/loss point of view on discussion considered harmful.

Name: Anonymous 2011-12-29 21:11

Use a global ^^

optimize with exponent-by-squaring

all done

Name: Anonymous 2011-12-29 21:36

Something like



int pTop, CurrentSum, EndSum=1, endCount=0;

function doPower(X, n){

while (endCount!=n){
  CurrentSum = X;
  pTop=1;

  while(pTop*2< n-endCount){
    CurrentSum *= CurrentSum;
    pTop += pTop;
    }

  EndSum *= CurrentSum;
  endCount += pTop;
  }
}

Name: Anonymous 2011-12-29 21:47

lol @ tail recursion... n^256 still does 256 multiplications, and you call it an optimization..?

Name: Anonymous 2011-12-29 21:48

http://rosettacode.org/wiki/Exponentiation_operator#Java

PROPER ENTERPRISE JAVA WAY


public class Exp{
   public static void main(String[] args){
      System.out.println(pow(2,30));
      System.out.println(pow(2.0,30)); //tests
      System.out.println(pow(2.0,-2));
   }
 
   public static double pow(double base, int exp){
      if(exp < 0) return 1 / pow(base, -exp);
      double ans = 1.0;
      for(;exp > 0;--exp) ans *= base;
      return ans;
   }
}

Name: Anonymous 2011-12-29 21:48

>>176
that's not what tail recursion optimization means

Name: Anonymous 2011-12-29 22:06

>>178

But it does just optimize the 'junk' calls to itself?

Name: Anonymous 2011-12-29 22:15

When IisMathwizard said he was a student, he meant high-school.  He is 16 years old and makes the claim of 5 years programming experience.  He is notably immature; but so is Kodak-san.

Name: Anonymous 2011-12-29 22:33

>>179
it turns a tail recursion function into an iterative loop.


Other optimization optimize the method at which you tried to do stuff.

Name: Anonymous 2011-12-29 22:33

Technically, the C code might already be tail-recursion optimized because it has no tail...?

btw has anyone got a legit example use for tail recursion... I've not seen many, but it always seems to be getting used when it's not really necessary in the first place..?

Eg. fib sequence doing every last node when you only need to do n + (n-1) for n steps...?

Name: Anonymous 2011-12-29 23:48

public int power(int x, int n)
{
  if (n == 1)
    return x;

  x = x*power(x, n-1);
}

public static void main(String[] args)
{
  System.println(power(2, 5));
}

lol

Name: Anonymous 2011-12-29 23:52

>>182
do you even know what tail recursion optimization is?

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

--->>

Name: Anonymous 2011-12-30 0:12

>>182
Its useful in languages without (or awkward) iterative looping.
Other than that its useful in some places like state automata-like code without resorting to mutation.
(define (bears z)
  (let chomp ([y z] [a 0] [b 0] [c 0])
    (if (empty? y)
        (list a b c)
        (case (car y)
          ((a) (chomp (cdr y) (add1 a) b c))
          ((b) (chomp (cdr y) a (add1 b) c))
          ((c) (chomp (cdr y) a b (add1 c))))
        )))

 > (bears '(a b b c c c))
'(1 2 3)
 > (bears (make-list 10000000 'a))
'(10000000 0 0)

Name: Anonymous 2011-12-30 0:30

>>182

[ Fri Dec 30 12:28:54 ]
[ @ ~/host/prog/fib ] $ cat fib.c
#include <stdlib.h>
#include <stdio.h>

/* Recursive */
unsigned long long rfib(const int n)
{
    return (n < 2) ? n : rfib(n - 1) + rfib(n - 2);
}

/* Tail Call */

unsigned long long tcfib(const unsigned long long a,const unsigned long long b,const int n)
{
    return n < 1 ? a : n == 1 ? b : tcfib(b,a+b,n - 1);
}


unsigned long long tfib(const int n)
{
    return tcfib(0,1,n);
}


int main(int argc,char **argv)
{
    int n = atoi(argv[2]);
    switch(atoi(argv[1])){
    case 0: printf("%d\n",rfib(n)); break;
     case 1: printf("%d\n",tfib(n)); break;
    }
}
[ @ ~/host/prog/fib ] $ time ./fib 0 30
832040

real    0m0.100s
user    0m0.080s
sys    0m0.008s
[ Fri Dec 30 12:26:49 ]
[ @ ~/host/prog/fib ] $ time ./fib 0 40
102334155

real    0m4.745s
user    0m4.732s
sys    0m0.008s
[ Fri Dec 30 12:29:41 ]
[ @ ~/host/prog/fib ] $ time ./fib 1 40
102334155

real    0m0.007s
user    0m0.000s
sys    0m0.004s
[ Fri Dec 30 12:29:47 ]
[ @ ~/host/prog/fib ] $ time ./fib 1 10000
1242044891

real    0m0.007s
user    0m0.000s
sys    0m0.000s



Why would i choose to do ugly for loops when i can make a nice tail call that gets optimized into one under the hood.

Name: Anonymous 2011-12-30 0:36

>>185 ty // 186 also, though i have no idea what it does =)

looks like a no brainer when given a choice between spaghetti code and just about any alternative =)

small challenge for the readers... see if you can optimize for n^(2^x-1) instead of n^(2^x) ;)

Name: Anonymous 2011-12-30 0:42

>>187

Where's the 'normal' implementation to compare with?

Eg >> 1+1 1+2 2+3 3+5...

Name: Anonymous 2011-12-30 0:51

>>189

[ Fri Dec 30 12:45:42 ]
[ @ ~/host/prog/fib ] $ cat fib.c
#include <stdlib.h>
#include <stdio.h>


/* Iterative */
unsigned long long ifib(const int a)
{
    unsigned long long l = 0,ret = 1,n,i;
    if(a < 2) return a;
    for(i=1;i<a;++i){
        n = l + ret;
        l = ret;
        ret = n;
    }
    return ret;
}


/* Recursive */
unsigned long long rfib(const int n)
{
    return (n < 2) ? n : rfib(n - 1) + rfib(n - 2);
}

/* Tail Call */

unsigned long long tcfib(const unsigned long long a,const unsigned long long b,const int n)
{
    return n < 1 ? a : n == 1 ? b : tcfib(b,a+b,n - 1);
}


unsigned long long tfib(const int n)
{
    return tcfib(0,1,n);
}


int main(int argc,char **argv)
{
    int n = atoi(argv[2]);
    switch(atoi(argv[1])){
    case 0: printf("%d\n",rfib(n)); break;
     case 1: printf("%d\n",tfib(n)); break;
    case 2: printf("%d\n",ifib(n)); break;
    }
}
[ @ ~/host/prog/fib ] $ time ./fib 1 1000000
1884755131

real    0m0.013s
user    0m0.008s
sys    0m0.004s
[ Fri Dec 30 12:50:28 ]
[ @ ~/host/prog/fib ] $ time ./fib 1 1000000
1884755131

real    0m0.011s
user    0m0.012s
sys    0m0.000s
[ Fri Dec 30 12:50:30 ]
[ @ ~/host/prog/fib ] $ time ./fib 2 1000000
1884755131

real    0m0.009s
user    0m0.004s
sys    0m0.000s
[ Fri Dec 30 12:50:32 ]
[ @ ~/host/prog/fib ] $ time ./fib 2 1000000
1884755131

real    0m0.017s
user    0m0.000s
sys    0m0.012s


that's with -O3, -O0 the tail call will seg fault @ fib 1000000

Name: Anonymous 2011-12-30 1:04

...does a bit of variable switching i guess

hows this?


a=0;
b=1;
for(i=0;i<n;i+=2){
    a+=b;
    b+=a;
    }
ret = b;
if (n%2==1) ret = a;

Name: Anonymous 2011-12-30 1:06

>>188
Its something like:
  void chomp (list y, int a, int b, int c) {
    if (empty_questionmark (y)) {
        return LISP(a, b, c);
    } else {
      switch (car (y)) {
      case SYMBOL_A: chomp (cdr (y), ++a, b, c)); break;
      case SYMBOL_B: chomp (cdr (y), a, ++b, c)); break;
      case SYMBOL_C: chomp (cdr (y), a, b, ++c)); break;
      }
    }
  }
void bears(list z) {
  chomp (z, 0, 0, 0);
}

Name: Anonymous 2011-12-30 1:06

>>191

return n%2 ? b : a;

Name: Anonymous 2011-12-30 1:08

>>188

n^(2^x-1) = (n^(2^x))/n I guess that would be good enough. One could do:

n^(sum_i_to_m(2^i)) = n^(2^(m+1) - 1) = n^(2^(m+1))/n

that could allow you to skip lots of consecutive ones. But you have to do a squaring every time you shift a bit anyways, so I don't think it would save much.

Name: Anonymous 2013-09-01 23:11


Zermelo began to axiomatize set theory in 1905; in 1908, he published his results despite his failure to prove the consistency of his axiomatic system.

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