Quantcast
Channel: Implement division using only addition - Code Golf Stack Exchange
Viewing all articles
Browse latest Browse all 18

Answer by Anurag for Implement division using only addition

$
0
0

Use this implementation in java, 199206 additions

public int divide(int a, int b){    int counter = 0;    int c = 0;    if(b==1){        return a;    }    if(a==b){        return 1;    }    else{        boolean done = false;        while(!done){            c = add(c, b);            if(a<c){                done = true;            }            counter = add(counter,1);        }        return counter;    }}

Following are the helper functions

public static void main(String[] args) {    Main main = new Main();    for(int a = 1; a<=200; a++){            for(int b=1;b<=a;b++){            main.divide(a, b);        }    }    System.out.println("Number of additions: "+numberOfAdds);}public int add(int a, int b){    numberOfAdds++;    return (a+b);}

Viewing all articles
Browse latest Browse all 18

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>