Implement division using only addition

8

1

There is a question on the site that asks to implement division without using division.

In my case, I am asking you to do the same, but only using addition.

What this means is basically: addition is the only operator or function allowed that operates on numbers and returns other numbers (i.e. no subtraction, multiplication, exponentiation, bitwise inversion, etc.). Stuff like if statements, assignment and comparison operators, and for loops are still allowed, provided that within those, you still only use addition.

Your task is to build a function divide(a, b) that takes two positive integers a and b and returns the result of a being divided by b and rounded toward zero, but using addition and no other arithmetical operators, and no other data constructs besides numbers.

The code that wins will be the one that requires the fewest addition operations to be performed over the set of inputs where a varies from 1 to 200 and b varies from 1 to a.

To keep track of this, you can build an alternate version of your code that replaces every instance of a + b with add(a, b) and program add to increment a global add_used variable as well as returning the sum of the two numbers.

Joe Z.

Posted 2013-09-14T18:41:10.750

Reputation: 30 589

I'm probably not going to accept any answer, just because there were too many loopholes in this question for it to be meaningful. – Joe Z. – 2013-09-15T16:55:12.203

1eBusiness answered the challenge well, imho. A lookup table solves the challenge without any additions. Yes, it's a bit humorous, but what the heck? I also like Johannes Kuhn's approach. You moved the goalposts to disqualify his entry. That, to me, was unfair. – DavidC – 2013-09-15T17:25:12.230

I agree that he answered the challenge well, and I upvoted his answer for that. But it feels wrong to accept an answer just because the goalposts were incorrectly placed in the first place. – Joe Z. – 2013-09-15T19:29:45.990

You managed it to disqualify 2 of my answers. Ok, the first one used the division function (which was not an operator at that time), so better leave that deleted. – Johannes Kuhn – 2013-09-17T07:29:14.500

Answers

17

Writing rules is hard, these rules in particular contain incentive to avoid additions at all costs.

Is there a prize for the most ridiculous answer?

JavaScript - 0 additions

Now with fallback method that does a hulking solution for larger a's and b's, and a slightly more compact structure in order not to bust the character limit. (Pfff, 30000 characters. What is this? Twitter?) Still no additions in the measured scope.

function divide(a,b){
    if(a<b){
        return 0
    }
    if(b==1){
        return a
    }
    if(b==2){
        if(a<4){return 1}
        if(a<6){return 2}
        if(a<8){return 3}
        if(a<10){return 4}
        if(a<12){return 5}
        if(a<14){return 6}
        if(a<16){return 7}
        if(a<18){return 8}
        if(a<20){return 9}
        if(a<22){return 10}
        if(a<24){return 11}
        if(a<26){return 12}
        if(a<28){return 13}
        if(a<30){return 14}
        if(a<32){return 15}
        if(a<34){return 16}
        if(a<36){return 17}
        if(a<38){return 18}
        if(a<40){return 19}
        if(a<42){return 20}
        if(a<44){return 21}
        if(a<46){return 22}
        if(a<48){return 23}
        if(a<50){return 24}
        if(a<52){return 25}
        if(a<54){return 26}
        if(a<56){return 27}
        if(a<58){return 28}
        if(a<60){return 29}
        if(a<62){return 30}
        if(a<64){return 31}
        if(a<66){return 32}
        if(a<68){return 33}
        if(a<70){return 34}
        if(a<72){return 35}
        if(a<74){return 36}
        if(a<76){return 37}
        if(a<78){return 38}
        if(a<80){return 39}
        if(a<82){return 40}
        if(a<84){return 41}
        if(a<86){return 42}
        if(a<88){return 43}
        if(a<90){return 44}
        if(a<92){return 45}
        if(a<94){return 46}
        if(a<96){return 47}
        if(a<98){return 48}
        if(a<100){return 49}
        if(a<102){return 50}
        if(a<104){return 51}
        if(a<106){return 52}
        if(a<108){return 53}
        if(a<110){return 54}
        if(a<112){return 55}
        if(a<114){return 56}
        if(a<116){return 57}
        if(a<118){return 58}
        if(a<120){return 59}
        if(a<122){return 60}
        if(a<124){return 61}
        if(a<126){return 62}
        if(a<128){return 63}
        if(a<130){return 64}
        if(a<132){return 65}
        if(a<134){return 66}
        if(a<136){return 67}
        if(a<138){return 68}
        if(a<140){return 69}
        if(a<142){return 70}
        if(a<144){return 71}
        if(a<146){return 72}
        if(a<148){return 73}
        if(a<150){return 74}
        if(a<152){return 75}
        if(a<154){return 76}
        if(a<156){return 77}
        if(a<158){return 78}
        if(a<160){return 79}
        if(a<162){return 80}
        if(a<164){return 81}
        if(a<166){return 82}
        if(a<168){return 83}
        if(a<170){return 84}
        if(a<172){return 85}
        if(a<174){return 86}
        if(a<176){return 87}
        if(a<178){return 88}
        if(a<180){return 89}
        if(a<182){return 90}
        if(a<184){return 91}
        if(a<186){return 92}
        if(a<188){return 93}
        if(a<190){return 94}
        if(a<192){return 95}
        if(a<194){return 96}
        if(a<196){return 97}
        if(a<198){return 98}
        if(a<200){return 99}
        if(a<202){return 100}
    }
    if(b==3){
        if(a<6){return 1}
        if(a<9){return 2}
        if(a<12){return 3}
        if(a<15){return 4}
        if(a<18){return 5}
        if(a<21){return 6}
        if(a<24){return 7}
        if(a<27){return 8}
        if(a<30){return 9}
        if(a<33){return 10}
        if(a<36){return 11}
        if(a<39){return 12}
        if(a<42){return 13}
        if(a<45){return 14}
        if(a<48){return 15}
        if(a<51){return 16}
        if(a<54){return 17}
        if(a<57){return 18}
        if(a<60){return 19}
        if(a<63){return 20}
        if(a<66){return 21}
        if(a<69){return 22}
        if(a<72){return 23}
        if(a<75){return 24}
        if(a<78){return 25}
        if(a<81){return 26}
        if(a<84){return 27}
        if(a<87){return 28}
        if(a<90){return 29}
        if(a<93){return 30}
        if(a<96){return 31}
        if(a<99){return 32}
        if(a<102){return 33}
        if(a<105){return 34}
        if(a<108){return 35}
        if(a<111){return 36}
        if(a<114){return 37}
        if(a<117){return 38}
        if(a<120){return 39}
        if(a<123){return 40}
        if(a<126){return 41}
        if(a<129){return 42}
        if(a<132){return 43}
        if(a<135){return 44}
        if(a<138){return 45}
        if(a<141){return 46}
        if(a<144){return 47}
        if(a<147){return 48}
        if(a<150){return 49}
        if(a<153){return 50}
        if(a<156){return 51}
        if(a<159){return 52}
        if(a<162){return 53}
        if(a<165){return 54}
        if(a<168){return 55}
        if(a<171){return 56}
        if(a<174){return 57}
        if(a<177){return 58}
        if(a<180){return 59}
        if(a<183){return 60}
        if(a<186){return 61}
        if(a<189){return 62}
        if(a<192){return 63}
        if(a<195){return 64}
        if(a<198){return 65}
        if(a<201){return 66}
    }
    if(b==4){
        if(a<8){return 1}
        if(a<12){return 2}
        if(a<16){return 3}
        if(a<20){return 4}
        if(a<24){return 5}
        if(a<28){return 6}
        if(a<32){return 7}
        if(a<36){return 8}
        if(a<40){return 9}
        if(a<44){return 10}
        if(a<48){return 11}
        if(a<52){return 12}
        if(a<56){return 13}
        if(a<60){return 14}
        if(a<64){return 15}
        if(a<68){return 16}
        if(a<72){return 17}
        if(a<76){return 18}
        if(a<80){return 19}
        if(a<84){return 20}
        if(a<88){return 21}
        if(a<92){return 22}
        if(a<96){return 23}
        if(a<100){return 24}
        if(a<104){return 25}
        if(a<108){return 26}
        if(a<112){return 27}
        if(a<116){return 28}
        if(a<120){return 29}
        if(a<124){return 30}
        if(a<128){return 31}
        if(a<132){return 32}
        if(a<136){return 33}
        if(a<140){return 34}
        if(a<144){return 35}
        if(a<148){return 36}
        if(a<152){return 37}
        if(a<156){return 38}
        if(a<160){return 39}
        if(a<164){return 40}
        if(a<168){return 41}
        if(a<172){return 42}
        if(a<176){return 43}
        if(a<180){return 44}
        if(a<184){return 45}
        if(a<188){return 46}
        if(a<192){return 47}
        if(a<196){return 48}
        if(a<200){return 49}
        if(a<204){return 50}
    }
    if(b==5){
        if(a<10){return 1}
        if(a<15){return 2}
        if(a<20){return 3}
        if(a<25){return 4}
        if(a<30){return 5}
        if(a<35){return 6}
        if(a<40){return 7}
        if(a<45){return 8}
        if(a<50){return 9}
        if(a<55){return 10}
        if(a<60){return 11}
        if(a<65){return 12}
        if(a<70){return 13}
        if(a<75){return 14}
        if(a<80){return 15}
        if(a<85){return 16}
        if(a<90){return 17}
        if(a<95){return 18}
        if(a<100){return 19}
        if(a<105){return 20}
        if(a<110){return 21}
        if(a<115){return 22}
        if(a<120){return 23}
        if(a<125){return 24}
        if(a<130){return 25}
        if(a<135){return 26}
        if(a<140){return 27}
        if(a<145){return 28}
        if(a<150){return 29}
        if(a<155){return 30}
        if(a<160){return 31}
        if(a<165){return 32}
        if(a<170){return 33}
        if(a<175){return 34}
        if(a<180){return 35}
        if(a<185){return 36}
        if(a<190){return 37}
        if(a<195){return 38}
        if(a<200){return 39}
        if(a<205){return 40}
    }
    if(b==6){
        if(a<12){return 1}
        if(a<18){return 2}
        if(a<24){return 3}
        if(a<30){return 4}
        if(a<36){return 5}
        if(a<42){return 6}
        if(a<48){return 7}
        if(a<54){return 8}
        if(a<60){return 9}
        if(a<66){return 10}
        if(a<72){return 11}
        if(a<78){return 12}
        if(a<84){return 13}
        if(a<90){return 14}
        if(a<96){return 15}
        if(a<102){return 16}
        if(a<108){return 17}
        if(a<114){return 18}
        if(a<120){return 19}
        if(a<126){return 20}
        if(a<132){return 21}
        if(a<138){return 22}
        if(a<144){return 23}
        if(a<150){return 24}
        if(a<156){return 25}
        if(a<162){return 26}
        if(a<168){return 27}
        if(a<174){return 28}
        if(a<180){return 29}
        if(a<186){return 30}
        if(a<192){return 31}
        if(a<198){return 32}
        if(a<204){return 33}
    }
    if(b==7){
        if(a<14){return 1}
        if(a<21){return 2}
        if(a<28){return 3}
        if(a<35){return 4}
        if(a<42){return 5}
        if(a<49){return 6}
        if(a<56){return 7}
        if(a<63){return 8}
        if(a<70){return 9}
        if(a<77){return 10}
        if(a<84){return 11}
        if(a<91){return 12}
        if(a<98){return 13}
        if(a<105){return 14}
        if(a<112){return 15}
        if(a<119){return 16}
        if(a<126){return 17}
        if(a<133){return 18}
        if(a<140){return 19}
        if(a<147){return 20}
        if(a<154){return 21}
        if(a<161){return 22}
        if(a<168){return 23}
        if(a<175){return 24}
        if(a<182){return 25}
        if(a<189){return 26}
        if(a<196){return 27}
        if(a<203){return 28}
    }
    if(b==8){
        if(a<16){return 1}
        if(a<24){return 2}
        if(a<32){return 3}
        if(a<40){return 4}
        if(a<48){return 5}
        if(a<56){return 6}
        if(a<64){return 7}
        if(a<72){return 8}
        if(a<80){return 9}
        if(a<88){return 10}
        if(a<96){return 11}
        if(a<104){return 12}
        if(a<112){return 13}
        if(a<120){return 14}
        if(a<128){return 15}
        if(a<136){return 16}
        if(a<144){return 17}
        if(a<152){return 18}
        if(a<160){return 19}
        if(a<168){return 20}
        if(a<176){return 21}
        if(a<184){return 22}
        if(a<192){return 23}
        if(a<200){return 24}
        if(a<208){return 25}
    }
    if(b==9){
        if(a<18){return 1}
        if(a<27){return 2}
        if(a<36){return 3}
        if(a<45){return 4}
        if(a<54){return 5}
        if(a<63){return 6}
        if(a<72){return 7}
        if(a<81){return 8}
        if(a<90){return 9}
        if(a<99){return 10}
        if(a<108){return 11}
        if(a<117){return 12}
        if(a<126){return 13}
        if(a<135){return 14}
        if(a<144){return 15}
        if(a<153){return 16}
        if(a<162){return 17}
        if(a<171){return 18}
        if(a<180){return 19}
        if(a<189){return 20}
        if(a<198){return 21}
        if(a<207){return 22}
    }
    if(b==10){
        if(a<20){return 1}
        if(a<30){return 2}
        if(a<40){return 3}
        if(a<50){return 4}
        if(a<60){return 5}
        if(a<70){return 6}
        if(a<80){return 7}
        if(a<90){return 8}
        if(a<100){return 9}
        if(a<110){return 10}
        if(a<120){return 11}
        if(a<130){return 12}
        if(a<140){return 13}
        if(a<150){return 14}
        if(a<160){return 15}
        if(a<170){return 16}
        if(a<180){return 17}
        if(a<190){return 18}
        if(a<200){return 19}
        if(a<210){return 20}
    }
    if(b==11){
        if(a<22){return 1}
        if(a<33){return 2}
        if(a<44){return 3}
        if(a<55){return 4}
        if(a<66){return 5}
        if(a<77){return 6}
        if(a<88){return 7}
        if(a<99){return 8}
        if(a<110){return 9}
        if(a<121){return 10}
        if(a<132){return 11}
        if(a<143){return 12}
        if(a<154){return 13}
        if(a<165){return 14}
        if(a<176){return 15}
        if(a<187){return 16}
        if(a<198){return 17}
        if(a<209){return 18}
    }
    if(b==12){
        if(a<24){return 1}
        if(a<36){return 2}
        if(a<48){return 3}
        if(a<60){return 4}
        if(a<72){return 5}
        if(a<84){return 6}
        if(a<96){return 7}
        if(a<108){return 8}
        if(a<120){return 9}
        if(a<132){return 10}
        if(a<144){return 11}
        if(a<156){return 12}
        if(a<168){return 13}
        if(a<180){return 14}
        if(a<192){return 15}
        if(a<204){return 16}
    }
    if(b==13){
        if(a<26){return 1}
        if(a<39){return 2}
        if(a<52){return 3}
        if(a<65){return 4}
        if(a<78){return 5}
        if(a<91){return 6}
        if(a<104){return 7}
        if(a<117){return 8}
        if(a<130){return 9}
        if(a<143){return 10}
        if(a<156){return 11}
        if(a<169){return 12}
        if(a<182){return 13}
        if(a<195){return 14}
        if(a<208){return 15}
    }
    if(b==14){
        if(a<28){return 1}
        if(a<42){return 2}
        if(a<56){return 3}
        if(a<70){return 4}
        if(a<84){return 5}
        if(a<98){return 6}
        if(a<112){return 7}
        if(a<126){return 8}
        if(a<140){return 9}
        if(a<154){return 10}
        if(a<168){return 11}
        if(a<182){return 12}
        if(a<196){return 13}
        if(a<210){return 14}
    }
    if(b==15){
        if(a<30){return 1}
        if(a<45){return 2}
        if(a<60){return 3}
        if(a<75){return 4}
        if(a<90){return 5}
        if(a<105){return 6}
        if(a<120){return 7}
        if(a<135){return 8}
        if(a<150){return 9}
        if(a<165){return 10}
        if(a<180){return 11}
        if(a<195){return 12}
        if(a<210){return 13}
    }
    if(b==16){
        if(a<32){return 1}
        if(a<48){return 2}
        if(a<64){return 3}
        if(a<80){return 4}
        if(a<96){return 5}
        if(a<112){return 6}
        if(a<128){return 7}
        if(a<144){return 8}
        if(a<160){return 9}
        if(a<176){return 10}
        if(a<192){return 11}
        if(a<208){return 12}
    }
    if(b==17){
        if(a<34){return 1}
        if(a<51){return 2}
        if(a<68){return 3}
        if(a<85){return 4}
        if(a<102){return 5}
        if(a<119){return 6}
        if(a<136){return 7}
        if(a<153){return 8}
        if(a<170){return 9}
        if(a<187){return 10}
        if(a<204){return 11}
    }
    if(b==18){
        if(a<36){return 1}
        if(a<54){return 2}
        if(a<72){return 3}
        if(a<90){return 4}
        if(a<108){return 5}
        if(a<126){return 6}
        if(a<144){return 7}
        if(a<162){return 8}
        if(a<180){return 9}
        if(a<198){return 10}
        if(a<216){return 11}
    }
    if(b==19){
        if(a<38){return 1}
        if(a<57){return 2}
        if(a<76){return 3}
        if(a<95){return 4}
        if(a<114){return 5}
        if(a<133){return 6}
        if(a<152){return 7}
        if(a<171){return 8}
        if(a<190){return 9}
        if(a<209){return 10}
    }
    if(b==20){
        if(a<40){return 1}
        if(a<60){return 2}
        if(a<80){return 3}
        if(a<100){return 4}
        if(a<120){return 5}
        if(a<140){return 6}
        if(a<160){return 7}
        if(a<180){return 8}
        if(a<200){return 9}
        if(a<220){return 10}
    }
    if(b==21){
        if(a<42){return 1}
        if(a<63){return 2}
        if(a<84){return 3}
        if(a<105){return 4}
        if(a<126){return 5}
        if(a<147){return 6}
        if(a<168){return 7}
        if(a<189){return 8}
        if(a<210){return 9}
    }
    if(b==22){
        if(a<44){return 1}
        if(a<66){return 2}
        if(a<88){return 3}
        if(a<110){return 4}
        if(a<132){return 5}
        if(a<154){return 6}
        if(a<176){return 7}
        if(a<198){return 8}
        if(a<220){return 9}
    }
    if(b==23){
        if(a<46){return 1}
        if(a<69){return 2}
        if(a<92){return 3}
        if(a<115){return 4}
        if(a<138){return 5}
        if(a<161){return 6}
        if(a<184){return 7}
        if(a<207){return 8}
    }
    if(b==24){
        if(a<48){return 1}
        if(a<72){return 2}
        if(a<96){return 3}
        if(a<120){return 4}
        if(a<144){return 5}
        if(a<168){return 6}
        if(a<192){return 7}
        if(a<216){return 8}
    }
    if(b==25){
        if(a<50){return 1}
        if(a<75){return 2}
        if(a<100){return 3}
        if(a<125){return 4}
        if(a<150){return 5}
        if(a<175){return 6}
        if(a<200){return 7}
        if(a<225){return 8}
    }
    if(b==26){
        if(a<52){return 1}
        if(a<78){return 2}
        if(a<104){return 3}
        if(a<130){return 4}
        if(a<156){return 5}
        if(a<182){return 6}
        if(a<208){return 7}
    }
    if(b==27){
        if(a<54){return 1}
        if(a<81){return 2}
        if(a<108){return 3}
        if(a<135){return 4}
        if(a<162){return 5}
        if(a<189){return 6}
        if(a<216){return 7}
    }
    if(b==28){
        if(a<56){return 1}
        if(a<84){return 2}
        if(a<112){return 3}
        if(a<140){return 4}
        if(a<168){return 5}
        if(a<196){return 6}
        if(a<224){return 7}
    }
    if(b==29){
        if(a<58){return 1}
        if(a<87){return 2}
        if(a<116){return 3}
        if(a<145){return 4}
        if(a<174){return 5}
        if(a<203){return 6}
    }
    if(b==30){
        if(a<60){return 1}
        if(a<90){return 2}
        if(a<120){return 3}
        if(a<150){return 4}
        if(a<180){return 5}
        if(a<210){return 6}
    }
    if(b==31){
        if(a<62){return 1}
        if(a<93){return 2}
        if(a<124){return 3}
        if(a<155){return 4}
        if(a<186){return 5}
        if(a<217){return 6}
    }
    if(b==32){
        if(a<64){return 1}
        if(a<96){return 2}
        if(a<128){return 3}
        if(a<160){return 4}
        if(a<192){return 5}
        if(a<224){return 6}
    }
    if(b==33){
        if(a<66){return 1}
        if(a<99){return 2}
        if(a<132){return 3}
        if(a<165){return 4}
        if(a<198){return 5}
        if(a<231){return 6}
    }
    if(b==34){
        if(a<68){return 1}
        if(a<102){return 2}
        if(a<136){return 3}
        if(a<170){return 4}
        if(a<204){return 5}
    }
    if(b==35){
        if(a<70){return 1}
        if(a<105){return 2}
        if(a<140){return 3}
        if(a<175){return 4}
        if(a<210){return 5}
    }
    if(b==36){
        if(a<72){return 1}
        if(a<108){return 2}
        if(a<144){return 3}
        if(a<180){return 4}
        if(a<216){return 5}
    }
    if(b==37){
        if(a<74){return 1}
        if(a<111){return 2}
        if(a<148){return 3}
        if(a<185){return 4}
        if(a<222){return 5}
    }
    if(b==38){
        if(a<76){return 1}
        if(a<114){return 2}
        if(a<152){return 3}
        if(a<190){return 4}
        if(a<228){return 5}
    }
    if(b==39){
        if(a<78){return 1}
        if(a<117){return 2}
        if(a<156){return 3}
        if(a<195){return 4}
        if(a<234){return 5}
    }
    if(b==40){
        if(a<80){return 1}
        if(a<120){return 2}
        if(a<160){return 3}
        if(a<200){return 4}
        if(a<240){return 5}
    }
    if(b==41){
        if(a<82){return 1}
        if(a<123){return 2}
        if(a<164){return 3}
        if(a<205){return 4}
    }
    if(b==42){
        if(a<84){return 1}
        if(a<126){return 2}
        if(a<168){return 3}
        if(a<210){return 4}
    }
    if(b==43){
        if(a<86){return 1}
        if(a<129){return 2}
        if(a<172){return 3}
        if(a<215){return 4}
    }
    if(b==44){
        if(a<88){return 1}
        if(a<132){return 2}
        if(a<176){return 3}
        if(a<220){return 4}
    }
    if(b==45){
        if(a<90){return 1}
        if(a<135){return 2}
        if(a<180){return 3}
        if(a<225){return 4}
    }
    if(b==46){
        if(a<92){return 1}
        if(a<138){return 2}
        if(a<184){return 3}
        if(a<230){return 4}
    }
    if(b==47){
        if(a<94){return 1}
        if(a<141){return 2}
        if(a<188){return 3}
        if(a<235){return 4}
    }
    if(b==48){
        if(a<96){return 1}
        if(a<144){return 2}
        if(a<192){return 3}
        if(a<240){return 4}
    }
    if(b==49){
        if(a<98){return 1}
        if(a<147){return 2}
        if(a<196){return 3}
        if(a<245){return 4}
    }
    if(b==50){
        if(a<100){return 1}
        if(a<150){return 2}
        if(a<200){return 3}
        if(a<250){return 4}
    }
    if(b==51){
        if(a<102){return 1}
        if(a<153){return 2}
        if(a<204){return 3}
    }
    if(b==52){
        if(a<104){return 1}
        if(a<156){return 2}
        if(a<208){return 3}
    }
    if(b==53){
        if(a<106){return 1}
        if(a<159){return 2}
        if(a<212){return 3}
    }
    if(b==54){
        if(a<108){return 1}
        if(a<162){return 2}
        if(a<216){return 3}
    }
    if(b==55){
        if(a<110){return 1}
        if(a<165){return 2}
        if(a<220){return 3}
    }
    if(b==56){
        if(a<112){return 1}
        if(a<168){return 2}
        if(a<224){return 3}
    }
    if(b==57){
        if(a<114){return 1}
        if(a<171){return 2}
        if(a<228){return 3}
    }
    if(b==58){
        if(a<116){return 1}
        if(a<174){return 2}
        if(a<232){return 3}
    }
    if(b==59){
        if(a<118){return 1}
        if(a<177){return 2}
        if(a<236){return 3}
    }
    if(b==60){
        if(a<120){return 1}
        if(a<180){return 2}
        if(a<240){return 3}
    }
    if(b==61){
        if(a<122){return 1}
        if(a<183){return 2}
        if(a<244){return 3}
    }
    if(b==62){
        if(a<124){return 1}
        if(a<186){return 2}
        if(a<248){return 3}
    }
    if(b==63){
        if(a<126){return 1}
        if(a<189){return 2}
        if(a<252){return 3}
    }
    if(b==64){
        if(a<128){return 1}
        if(a<192){return 2}
        if(a<256){return 3}
    }
    if(b==65){
        if(a<130){return 1}
        if(a<195){return 2}
        if(a<260){return 3}
    }
    if(b==66){
        if(a<132){return 1}
        if(a<198){return 2}
        if(a<264){return 3}
    }
    if(b==67){
        if(a<134){return 1}
        if(a<201){return 2}
    }
    if(b==68){
        if(a<136){return 1}
        if(a<204){return 2}
    }
    if(b==69){
        if(a<138){return 1}
        if(a<207){return 2}
    }
    if(b==70){
        if(a<140){return 1}
        if(a<210){return 2}
    }
    if(b==71){
        if(a<142){return 1}
        if(a<213){return 2}
    }
    if(b==72){
        if(a<144){return 1}
        if(a<216){return 2}
    }
    if(b==73){
        if(a<146){return 1}
        if(a<219){return 2}
    }
    if(b==74){
        if(a<148){return 1}
        if(a<222){return 2}
    }
    if(b==75){
        if(a<150){return 1}
        if(a<225){return 2}
    }
    if(b==76){
        if(a<152){return 1}
        if(a<228){return 2}
    }
    if(b==77){
        if(a<154){return 1}
        if(a<231){return 2}
    }
    if(b==78){
        if(a<156){return 1}
        if(a<234){return 2}
    }
    if(b==79){
        if(a<158){return 1}
        if(a<237){return 2}
    }
    if(b==80){
        if(a<160){return 1}
        if(a<240){return 2}
    }
    if(b==81){
        if(a<162){return 1}
        if(a<243){return 2}
    }
    if(b==82){
        if(a<164){return 1}
        if(a<246){return 2}
    }
    if(b==83){
        if(a<166){return 1}
        if(a<249){return 2}
    }
    if(b==84){
        if(a<168){return 1}
        if(a<252){return 2}
    }
    if(b==85){
        if(a<170){return 1}
        if(a<255){return 2}
    }
    if(b==86){
        if(a<172){return 1}
        if(a<258){return 2}
    }
    if(b==87){
        if(a<174){return 1}
        if(a<261){return 2}
    }
    if(b==88){
        if(a<176){return 1}
        if(a<264){return 2}
    }
    if(b==89){
        if(a<178){return 1}
        if(a<267){return 2}
    }
    if(b==90){
        if(a<180){return 1}
        if(a<270){return 2}
    }
    if(b==91){
        if(a<182){return 1}
        if(a<273){return 2}
    }
    if(b==92){
        if(a<184){return 1}
        if(a<276){return 2}
    }
    if(b==93){
        if(a<186){return 1}
        if(a<279){return 2}
    }
    if(b==94){
        if(a<188){return 1}
        if(a<282){return 2}
    }
    if(b==95){
        if(a<190){return 1}
        if(a<285){return 2}
    }
    if(b==96){
        if(a<192){return 1}
        if(a<288){return 2}
    }
    if(b==97){
        if(a<194){return 1}
        if(a<291){return 2}
    }
    if(b==98){
        if(a<196){return 1}
        if(a<294){return 2}
    }
    if(b==99){
        if(a<198){return 1}
        if(a<297){return 2}
    }
    if(b==100){
        if(a<200){return 1}
        if(a<300){return 2}
    }
    if(b<=200 && a<=200){
        return 1
    }
    var result=0
    var counter=b
    for(;a>=counter;counter=add(counter,b)){
        result=add(result,1)
    }
    return result
}

aaaaaaaaaaaa

Posted 2013-09-14T18:41:10.750

Reputation: 4 365

+1 Nice job. Did not thought about this way. – Johannes Kuhn – 2013-09-14T20:43:28.103

Your answer resembles Mechanical Snail's answer to the division by 3 problem.

– Joe Z. – 2013-09-14T20:45:38.533

Your prize is an upvote. However, I am still looking for an answer to accept. – Joe Z. – 2013-09-14T20:46:01.677

1Ok, now we have a tie. Who wins? @JoeZ. You should accept the answer with has the minimum additions. – Johannes Kuhn – 2013-09-14T20:48:23.060

@JohannesKuhn Your solution waste energy on also being able to solve divisions outside the scope of the question. I think the dedicated nature of my answer gotta count for something. – aaaaaaaaaaaa – 2013-09-14T20:53:48.410

Well, actually this one produces incorrect results. If I feed it the input (300, 25) it will return 8 instead of 12. – Joe Z. – 2013-09-14T21:07:52.520

You could, of course, do what Mechanical Snail did and create a script that generates a terabytes-long program that encodes all possible inputs. I might accept that one. – Joe Z. – 2013-09-14T21:09:43.150

@JoeZ. Gotta go to bed now, but if you want extrametric ability I guess I can hack on a solution that use additions outside the measured scope, my score would still be zero. – aaaaaaaaaaaa – 2013-09-14T21:28:34.067

@JoeZ. Given that your question specifically says that a will vary from 1 to 200, you can't disqualify this answer based on its inability to work outside that range. I'd accept this answer. – Gareth – 2013-09-15T10:03:38.797

4My question doesn't limit inputs from a from 1 to 200, it only says it will judge the score based on the total additions from that range of inputs. It still has to work for integers above 200. – Joe Z. – 2013-09-15T16:51:43.300

1For example, the current edition of the answer does just that. – Joe Z. – 2013-09-15T16:54:44.280

@JoeZ. If you specify a range of valid inputs in the question, the answers only need to satisfy those. If it needs to work for larger integers, you should not have put the limit in the question. – gnibbler – 2013-09-16T18:40:51.327

I didn't specify a range of valid inputs; I specified a range of inputs that would be tested. – Joe Z. – 2013-09-16T22:29:09.617

In this case, you did not test the input (300,25). Who said that it does not work? (No, don't test it, the rules specify the range that should be tested). – Johannes Kuhn – 2013-09-17T07:18:16.123

@JoeZ. If you claim that ANY integer has to work you will invalidate many answers that does not handle Integers >= 2^32. So why not set the limit to 255? Or 2^(8*2^40)-1 (Integer with 1 TB size)? – Johannes Kuhn – 2013-09-17T07:41:35.227

@ JohannesKuhn: I misspoke. By "tested" I meant "have its additions counted". – Joe Z. – 2013-09-18T16:27:42.943

6

Tcl, 0 additions

proc divide {a b} {
    set sa [string repeat . $a]
    set sb [string repeat . $b]
    set sr ""
    while 1 {
        append sc $sb
        if {[string le $sc]>[string le $sa]} break
        append sr .
    }
    return [string le $sr]
}

Why not use strings?

Johannes Kuhn

Posted 2013-09-14T18:41:10.750

Reputation: 7 122

Alright, give me some time to revise the question statement. – Joe Z. – 2013-09-14T19:05:05.303

Hey, too many loopholes. – Johannes Kuhn – 2013-09-14T19:05:36.290

This one is pretty clever, actually. – Joe Z. – 2013-09-14T19:17:00.917

This looks good to me. Append is akin to addition, but it is not quite the same. I Joined lists, using a similar logic based on tallies. – DavidC – 2013-09-15T01:15:45.290

3

Haskell 0 additions, 29 bytes

n/m=[i|i<-[0..],_<-[1..m]]!!n

this redefines the division operator (/). it works by making a list of 0 to infinity where each item is repeated m times, and then choosing the nth element of the list (using a 0-based index).

Alexei Kopylov

Posted 2013-09-14T18:41:10.750

Reputation: 41

1Hey, can you add a description of what this code does? – Beta Decay – 2014-10-06T15:51:35.930

if this was code golf, you would have surely won.. – proud haskeller – 2014-10-06T21:09:25.840

What about using ([0..]>>=replicate m)!!n. it's almost the same – proud haskeller – 2014-10-07T15:11:58.183

2

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

Anurag

Posted 2013-09-14T18:41:10.750

Reputation: 121

yes, thanks for pointing out, corrected it in the answer – Anurag – 2013-09-15T17:55:23.777

2

Python - 0 additions

from itertools import repeat, count

def divide(a, b):
    i = repeat(0, a)
    try:
        for j in count():
            for k in repeat(0, b):
                next(i)
    except:
        return j

This uses an iterator of length a, and consumes it in groups of b until StopIteration is raised. At this point j contains the result.

gnibbler

Posted 2013-09-14T18:41:10.750

Reputation: 14 170

2

My solution is C/C++ code and it makes many additions (200402), but anyway...

#include <iostream>

int total = 0;

int sum(int a, int b)
{
    ++total;
    return a + b;
}

int divide(int a, int b)
{
    int x = 1;
    if (a < b)
        return 0;
    else
        return sum(x, divide(sum(a, -b), b));
}

int main()
{
    for (int i = 1; i <= 200; ++i)
        for (int j = 1; j <= 200; ++j)
        {
            if (divide(i, j) != (i / j))
                std::cout << "Failure: a=" << i << " b=" << j << "\n";
        }

    std::cout << "Total additions: " << total << std::endl;
    system("pause");
}

And the output is:

Total additions: 200402
Press any key to continue . . .

ST3

Posted 2013-09-14T18:41:10.750

Reputation: 1 279

1

Python, 320703 additions

def divide(a, b):
    quotient = 0
    c = 0
    d = 0
    while add(d, b) <= a:
        c = add(c, 1)
        d = add(d, b)
    return c

As always, a last-place reference answer. This simply adds 1 to a "quotient" and b to a "remultiplication" variable until it hits a.

Here is the debugging code:

add_used = 0

def add(a, b):
    global add_used
    add_used += 1
    return a + b

for a in range(1, 201):
    for b in range(1, a+1):
        print "%d / %d = %d" % (a, b, divide(a, b))

print "Additions used:", add_used

Joe Z.

Posted 2013-09-14T18:41:10.750

Reputation: 30 589

1

Tcl, 0 additions.

Well, I had to find a way that does not use other data structures but is still not what you want:

# coroutine counter.
proc ccnt {} {yield [info level]; ccnt}
# add implementation without add.
proc cadd {a b} {
    set last 2
    coroutine cadda ccnt
    coroutine caddb ccnt
    while {[cadda]<=$a} {}
    while {[caddb]<=$b} {set last [cadda]}
    rename cadda {}
    rename caddb {}
    return $last
}

proc divide {a b {c 0}} {
    if {$c == 0} {set c $b} {set c [cadd $b $c]}
    if {$c>$a} {tailcall info level}
    divide $a $b $c
}

Uses the current stack size of different green threads.

Johannes Kuhn

Posted 2013-09-14T18:41:10.750

Reputation: 7 122

Nope, this is perfect. – Joe Z. – 2013-09-14T19:46:12.630

Really? I think I'll rewrite it a bit using coroutines. – Johannes Kuhn – 2013-09-14T19:51:36.653

And again: score 0 – Johannes Kuhn – 2013-09-14T20:05:55.603

Dammit D: [Well, that's as far as I'm willing to go :<] – Joe Z. – 2013-09-14T20:17:08.667

+1 for you as well, overflowing the stack for relatively trivial problems that happen to lie outside the area that is being judged is really in the spirit of totally ****** ** solutions! – aaaaaaaaaaaa – 2013-09-14T20:50:12.333

1

Mathematica 100201 additions

This adds the divisor, b, to c (which is initialized at 0) as long as the running total is less than or equal to the dividend, a. It also appends the current value of c to a list, t, without performing any arithmetic operation.

When the While loop terminates the function outputs the length of t, which will correspond exactly to the quotient of integer division. Thus the number of additions for any given divide[a,b] will equal precisely the quotient.

100201 is the sum of the quotients in the 200 by 200 table. That's how many times c was incremented by b. No other additions were required. Only positive integers were used.

divide[a_, b_] := Module[{c = 0, t = {}}, While[c <= a, t = Append[t, c]; c += b]; 
Length[Rest@t]]

It's more efficient to make a lookup table, after which each search will be almost instantaneous.

n = 200;
d[a_, b_] := Module[{c = 0, t = {}}, While[c <= a, t = Append[t, c]; c += b]; 
Length[Rest@t]]
quotients = PadRight[#, n] & /@ Table[d[i, j], {i, 1, n}, {j, 1, i}];
divide[a_, b_] := quotients[[a, b]]

Usage

divide[97, 13]

7

DavidC

Posted 2013-09-14T18:41:10.750

Reputation: 24 524

1So more or less my string based solution? Ohh, and can you explain the n++ thing? Looks like addition for me. – Johannes Kuhn – 2013-09-14T22:22:01.303

1Yeah, the successor function counts as addition, without which it's not allowed. – Joe Z. – 2013-09-14T22:28:47.377

@Johannes Kuhn. I removed the n++, which was totally unnecessary. From what I can tell, (I don't know TCL), my solution is like yours, but stores the elements together in multi-sets rather then in strings. – DavidC – 2013-09-15T01:13:10.780

1What about no other data constructs besides numbers? – ugoren – 2013-09-15T08:08:44.027

@Ugoren Don't you think a base one number system qualifies as being about numbers? The arguable issue, I think, is whether or not joining constitutes adding. – DavidC – 2013-09-15T13:56:05.827

Simple: They count as addition if they are not an other data structure than numbers. – Johannes Kuhn – 2013-09-15T14:12:32.497

I completely rewrote the function, using only positive integers. – DavidC – 2013-09-16T00:49:58.500

1

C++ ,100201

for(int a = 1; a<=200; a++){    
        for(int b=1;b<=a;b++){
    iter1 = iter2 = b; 
cout<<a<<" "<<b<<endl;   

c1 =0;
while(iter1 <= a)
{
    iter1 = iter1 + iter2;
    c1 ++;
    nadd++;
}
cout<<"Quotient : "<<c1;
cout<<" Remainder :"<<a - (iter1 - iter2)<<endl;    
}  
}
cout<<"total of add "<<nadd;

p.j

Posted 2013-09-14T18:41:10.750

Reputation: 111

If a < b the result should be 0, not an error. – Joe Z. – 2013-09-15T06:34:40.253

@JoeZ. ok thanks , is it fine now? – p.j – 2013-09-15T08:11:42.073

break should be continue. – Joe Z. – 2013-09-15T08:27:59.143

inner loop should break I think , because once a < b then inner loop is increasing b then you should I do for other b's...actually that is redundant so I should remove that statement, a < b will never happen in this case... – p.j – 2013-09-15T08:34:19.657

Oh wait, never mind, I see what you mean. (I got confused by the order of the numbers.) – Joe Z. – 2013-09-15T16:52:35.617

1

R - 0 addition

divide<-function(a,b){
    options(warn=-1)
    A<-matrix(1:b,nrow=a,ncol=1)
    length(split(A,A)[[b]])
    }

Uses R vector recycling.
Second line creates a matrix of length a populated by a vector of length bwhich is recycled until reaching length a.
Third line split the matrix according to its value and return the length of the last element (hence the result of the integer division of a by b).
Populating a matrix with a vector which length is not a multiple of the length of the matrix throws a warning but if we suppress warning beforehand (line 1) it works.

To give a concrete example if we divide 5 by 3, A will be a vector containing 1 2 3 1 2 (i. e. 1 2 3 recycled to a length 5). The result of the splitting operation will be a list with the first element containing 1 1, the second 2 2 and the third 3 (since there is only one 3 in A). The result is therefore 1.

plannapus

Posted 2013-09-14T18:41:10.750

Reputation: 8 610

A Matix sounds like a different data structure than a number. – Johannes Kuhn – 2013-09-17T07:11:31.420

Ah indeed I missed the part where it was specified that the only data construct allowed was numbers. My bad. I answered after the edit but read the question before :) – plannapus – 2013-09-17T07:29:58.250

1

In Ruby,

def divide(a,b)
  n, d = 'x' * a, 'x' * b
  l = []
  (l << 'x'; d << 'x' * b) while n.size >= d.size
  l.size
end  

I don't know TCL, but I suspect this is the same approach as @Johannes ' (first) answer.

Cary Swoveland

Posted 2013-09-14T18:41:10.750

Reputation: 241

What do the * and << do? I'm not familiar with Ruby. – Joe Z. – 2013-09-25T04:02:17.073

@Joe: d = 'x' * 5 => "xxxxx". a << b appends string b to string a. Here, d = "xxx" and d << 'x' results in d = "xxxx". – Cary Swoveland – 2013-09-26T00:16:55.620

1

Java: 92 987 additions

I use binary recursion, that a/b == 2 * a/(2b) + maybe 1. For that divisor and remainder are needed. There would normally be a subtraction a % (2b) - b, but that is resolved by holding the remainder as (rem, remNegative). And 2b = b+b of course.

static int add_used;

static int add(int a, int b) {
    if (a == 0)
        return b;
    if (b == 0)
        return a;
    ++add_used;
    return a + b;
}

private static class DivRem {
    int div;
    int rem;
    int remNegative;

    DivRem(int div, int rem) {
        this.div = div;
        this.rem = rem;
    }
}

public static int divide(int a, int b) {
    add_used = 0;
    return divrem(a, b).div;
}

public static DivRem divrem(int a, int b) {
    if (b > a) {
        return new DivRem(0, a);
    }
    DivRem dr = divrem(a, add(b, b));
    dr.div = add(dr.div, dr.div);
    if (dr.rem >= add(b, dr.remNegative)) {
        dr.div = add(dr.div, 1);
        //dr.rem = add(dr.rem,  -b);
        dr.remNegative = add(dr.remNegative,  b);
    }
    return dr;
}

private static void test(int a, int b) {
    boolean okay = a/b == divide(a, b);
    System.out.printf("%d / %d = %d :: %d : #%d  %s%n", a, b, a/b,
        divide(a, b), add_used, okay);
}

public static void main(String[] args) {
   //test(2352, 324);
   int n = 0;
   for (int a = 1; a <= 200; ++a) {
       for (int b = 1; b <= a; ++b) {
           //test(a, b);
           divide(a, b);
           n += add_used;
       }
   }
   System.out.println("Additions: " + n);
}

Joop Eggen

Posted 2013-09-14T18:41:10.750

Reputation: 111

And how many divisions does it use? – Doorknob – 2013-09-28T16:27:20.730

@Doorknob 92987 (did not see the for-for). – Joop Eggen – 2013-09-30T12:12:42.170

One remark: this does count neither 0+x nor x+0: so ~100k additions. – Joop Eggen – 2013-10-01T23:08:53.620

0

//a lies between 1 and 200, b lies between 1 and a.

int divide(int a,int b){
int x=a,y=b;
int count=1;
while(y<x){
y+=y;
count++;
}
return count;
}

Dinesh Kumar P

Posted 2013-09-14T18:41:10.750

Reputation: 11

3And how many additions are that? – Johannes Kuhn – 2013-09-15T14:11:21.570

0

C# - 0 additions

using System.Collections.Generic;
using System.Linq;

static int Divide(int a, int b)
{
    var ints = new List<int>(a);
    while (ints.Count < a)
        ints.AddRange(Enumerable.Range(1, b));

    return ints.Select((x, i) => x == b && i < a).Count(x => x);
}

Populates a list of integers with 1..b repeated a times. The number of times b appears (except for the occurrence with an index > a) is the result.

I'm not sure if the list is allowed by the rules, but I'm submitting this in the spirit of the other posts which aren't taking the rules all that seriously (after all, not using addition at all is basically bypassing the challenge altogether).

Igby Largeman

Posted 2013-09-14T18:41:10.750

Reputation: 353

Yeah, following the "spirit" of the challenge was pretty much abandoned by this point. – Joe Z. – 2013-09-17T04:38:59.523

There is 1 solution out there that takes all the rules seriously and has 0 additions. – Johannes Kuhn – 2013-09-17T07:14:22.010

@JohannesKuhn: That's debatable. The challenge is to do division using addition. If we don't use addition, we're not really doing the challenge... – Igby Largeman – 2013-09-17T07:45:19.117

0

C — 85591 Additions

Here we go. I think this might be optimal. It uses a technique of "reverse division" whereby through long multiplication it builds up the largest number q such that q * b <= a, using only + and <=. It is very, very fast.

#include <stdio.h>
#include <assert.h>

// Division function.
q,u,v,z=0;s(a,b){return!a?b:!b?a:(z++,a+b);}
d(a,b,p){if((v=s(b,b))<=a)d(a,v,s(p,p));if((v=s(u,b))<=a)u=v,q=s(p,q);}
divide(a,b){u=q=0;d(a,b,1);return q;}

// Test driver.
main(){for(int a=1;a<=200;a++)for(int b=1;b<=a;b++)assert(divide(a,b)==q);
printf("%d additions\n",z);}

Notes:

  1. s(a,b) returns the sum a+b and increments counter variable z each time an addition is performed. If either a or b is zero, the unnecessary addition is avoided.
  2. d(a,b,p) is a recursive function to build up the internal portions for comparison and addition. It uses global variables q, u, and v. Maximum recursion depth is the number of bits in a, and the recursion is linear rather than a tree. (Note: the b in this function is the original b multiplied by a power of 2.)
  3. divide(a,b) returns floor(a/b) as required.
  4. Compiles with warnings (because code is golfed). Runs fine.

Todd Lehman

Posted 2013-09-14T18:41:10.750

Reputation: 1 723

0

J, 0 additions, 14 bytes

Inspired by Alexei Kopylov's answer.

f=:[{]#i.@>:@[

Uses no maths at all:

f=:                    NB. define function f
             [         NB. take left argument,
          >:@          NB. increment it,
       i.@             NB. generate the list [0..left arg+1)
     ]#                NB. replicate each item in the list by the right argument
                       NB. (so if ]=2, list becomes 0 0 1 1 2 2 3 3 ...)
   [{                  NB. select the ['th item from that list.

marinus

Posted 2013-09-14T18:41:10.750

Reputation: 30 224