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

NEED URGENT HELP

Name: edo-chan 2011-12-28 11:11

sup /prog/

Ive asked this everywer already but peopl seem too lame to make even a simple program like so i hope the mighty anon programers of 4chan are more skilled

what im trying to do is to Write a method taking two parameters x and n that computes the nth power of x without using ^ or declaring a new variable inside the method or using Math lib.
and it has to be coded in JAVA (i know it sux but dont ask why i use dat shit)
hep plox!

Name: Anonymous 2011-12-29 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....

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