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!
/* 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.