Divaddition

Due to some circumstances I was sitting around with some time on my hands, so I wrote this little program based on a challenge posted in the code golf section of stack

http://pastebin.com/cTQsvaHk

#include <stdio.h>

static int addCounter;

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

int divide(int dividend, int divisor)
{
int quotient = 0, accumulator = 0, quantity = divisor, counter = 1;

while(42) {
if (add(accumulator, quantity) > dividend) {
if (counter == 1)
break;

counter = 1;
quantity = divisor;
}
else if (add(accumulator, quantity) == dividend) {
quotient = add(quotient, counter);
break;
}
else {
quotient = add(quotient, counter);
accumulator = add(accumulator, quantity);
quantity = add(quantity, quantity);
counter = add(counter, counter);
}
}

return quotient;
}

void main(char **stuff)
{
int a = 5;
int b = 10;
int i = 0;
int q;

for (i = 0; i < 150; ++i, a+=27) {
addCounter = 0;
q = divide(a, b);
printf(“%d / %d = %d | Additions : (%d)\n”, a, b, q, addCounter);
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s