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!
>>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);
}
}
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.
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;
}
}
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:
Anonymous2011-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;
}
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;
}
}
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:
Anonymous2011-12-28 17:50
>>10 is the best answer, simple with recursion. >>31 creates new variable >>29 creates new variable
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));
}
}
A method computing x^n without declaring variables or using Math libraries (or even integers): λx.λn.nx GEEZ THAT WAS SO DIFFICULT
Name:
Anonymous2011-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:
Anonymous2011-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;
}
/* 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;
}
}
/* (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;
>>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
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.
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:
Anonymous2011-12-29 10:03
power :: (Num a, Integral b) => a -> b -> a
power a b = getProduct . mconcat . map Product $ genericReplicate b a
>>59
It would be better to apply the Product before the genericReplicate.
Name:
IisMathwizard2011-12-29 13:56
Not sure if OP is retarded or troll...
Name:
IisMathwizard2011-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:
IisMathwizard2011-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;
}
}
>>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
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
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.
>>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.
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:
Anonymous2011-12-29 16:26
>>75
You actually convinced somebody to pay you for writing broken pow implementations in Java? AMAZING.
>>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?
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:
Anonymous2011-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:
Anonymous2011-12-29 16:38
>>73 /g/ /a/ /c/
please go back to the imageboards you faggot
>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.
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:
Anonymous2011-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:
Anonymous2011-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.
>>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:
Anonymous2011-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?
>>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?
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...
>>104
Then why can't you write it? The solution you posted isn't correct.
Name:
Anonymous2011-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?
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...
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.
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.
>>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:
Anonymous2011-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.
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);
}
>>116
wouldn't dream of it >>114
what do you mean?
Name:
Anonymous2011-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.
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:
Anonymous2011-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:
Anonymous2011-12-29 17:04
Kodak-san would you like to receive a Photo Book created with Smart Fit technology as a gift?
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
>>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.
>>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:
Anonymous2011-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:
Anonymous2011-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:
Anonymous2011-12-29 17:17
>>133
You gonna be hella mad when you get an invite to check out Google in a couple of years.
>>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:
Anonymous2011-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?
>>135
glad to see someone still has a sense of humour
Name:
Anonymous2011-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?
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.
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!
>>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:
Anonymous2011-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
>>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;
}
}
>>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.
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.
>>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:
Anonymous2011-12-29 17:49
>>162
But it's best practice not to use recursion either way.
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:
Anonymous2011-12-29 17:51
>>164
Java doesn't support tail recursion, that's why no one made a solution that looked like that.
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.
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.
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;
}
}
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.
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:
Anonymous2011-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));
}
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;
}
}
--->>
>>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)
/* 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.
/* 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
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:
Anonymous2013-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.