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

Weird useless algorithm

Name: Anonymous 2015-01-08 3:56

How to subtract the smallest of two integers from the greater one, in a system that supports only addition, equality comparisons and conditional jumps:

The numbers are A and B.
If A=B, return 0 as the answer.
Define three numeric variables a, b and c of the same type as A and B.
Let a=A, b=B and c=0.
Now let c=c+1, a=a+1 and b=b+1.
If either a=B or b=A, go to the next line. Otherwise, go to the previous line.
Return c as the answer.

The algorithm is not mine, by the way. It's anonymous.

Name: Anonymous 2015-01-08 15:37

>>5
Fuck, right, no subtraction.
unsigned int sub(unsigned int a, unsigned int b)
{
if(a <= b) return 0;
if(b == 0) return a;
return sub(a + (~((int)0)), b + (~((int)0)));
}

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