# Implement division using only addition

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.

• I'm probably not going to accept any answer, just because there were too many loopholes in this question for it to be meaningful. Commented Sep 15, 2013 at 16:55
• eBusiness 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. Commented Sep 15, 2013 at 17:25
• 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. Commented Sep 15, 2013 at 19:29
• 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. Commented Sep 17, 2013 at 7:29

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?

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
}
return result
}

• Your answer resembles Mechanical Snail's answer to the division by 3 problem. Commented Sep 14, 2013 at 20:45
• Ok, now we have a tie. Who wins? @JoeZ. You should accept the answer with has the minimum additions. Commented Sep 14, 2013 at 20:48
• My 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. Commented Sep 15, 2013 at 16:51
• For example, the current edition of the answer does just that. Commented Sep 15, 2013 at 16:54

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?

• Alright, give me some time to revise the question statement. Commented Sep 14, 2013 at 19:05
• Hey, too many loopholes. Commented Sep 14, 2013 at 19:05
• This one is pretty clever, actually. Commented Sep 14, 2013 at 19:17
• 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. Commented Sep 15, 2013 at 1:15

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).

• Hey, can you add a description of what this code does? Commented Oct 6, 2014 at 15:51
• if this was code golf, you would have surely won.. Commented Oct 6, 2014 at 21:09
• What about using ([0..]>>=replicate m)!!n. it's almost the same Commented Oct 7, 2014 at 15:11

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){
if(a<c){
done = true;
}
}
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);
}
}

}

public int add(int a, int b){
return (a+b);
}

• yes, thanks for pointing out, corrected it in the answer Commented Sep 15, 2013 at 17:55

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.

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 . . .


def divide(a, b):
quotient = 0
c = 0
d = 0
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

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



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}
set last 2
while {[cadda]<=$a} {} while {[caddb]<=$b} {set last [cadda]}
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.

• Nope, this is perfect. Commented Sep 14, 2013 at 19:46
• Really? I think I'll rewrite it a bit using coroutines. Commented Sep 14, 2013 at 19:51
• And again: score 0 Commented Sep 14, 2013 at 20:05
• Dammit D: [Well, that's as far as I'm willing to go :<] Commented Sep 14, 2013 at 20:17
• +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! Commented Sep 14, 2013 at 20:50

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 ++;
}
cout<<"Quotient : "<<c1;
cout<<" Remainder :"<<a - (iter1 - iter2)<<endl;
}
}

• If a < b the result should be 0, not an error. Commented Sep 15, 2013 at 6:34
• @JoeZ. ok thanks , is it fine now? Commented Sep 15, 2013 at 8:11
• break should be continue. Commented Sep 15, 2013 at 8:27
• 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... Commented Sep 15, 2013 at 8:34
• Oh wait, never mind, I see what you mean. (I got confused by the order of the numbers.) Commented Sep 15, 2013 at 16:52

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

• So more or less my string based solution? Ohh, and can you explain the n++ thing? Looks like addition for me. Commented Sep 14, 2013 at 22:22
• Yeah, the successor function counts as addition, without which it's not allowed. Commented Sep 14, 2013 at 22:28
• @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. Commented Sep 15, 2013 at 1:13
• What about no other data constructs besides numbers? Commented Sep 15, 2013 at 8:08
• @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. Commented Sep 15, 2013 at 13:56

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.

• A Matix sounds like a different data structure than a number. Commented Sep 17, 2013 at 7:11
• 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 :) Commented Sep 17, 2013 at 7:29

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.

• What do the * and << do? I'm not familiar with Ruby. Commented Sep 25, 2013 at 4:02
• @Joe: d = 'x' * 5 => "xxxxx". a << b appends string b to string a. Here, d = "xxx" and d << 'x' results in d = "xxxx". Commented Sep 26, 2013 at 0:16

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;
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) {
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));
if (dr.rem >= add(b, dr.remNegative)) {
}
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,
}

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

• And how many divisions does it use? Commented Sep 28, 2013 at 16:27
• @Doorknob 92987 (did not see the for-for). Commented Sep 30, 2013 at 12:12
• One remark: this does count neither 0+x nor x+0: so ~100k additions. Commented Oct 1, 2013 at 23:08
//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;
}

• And how many additions are that? Commented Sep 15, 2013 at 14:11

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

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

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).

• Yeah, following the "spirit" of the challenge was pretty much abandoned by this point. Commented Sep 17, 2013 at 4:38
• There is 1 solution out there that takes all the rules seriously and has 0 additions. Commented Sep 17, 2013 at 7:14
• @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... Commented Sep 17, 2013 at 7:45

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


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.

## J, 0 additions, 14 bytes

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.