let S and D = N repeat let D = ⌊D/2⌋ subtract D from S until D=0 produce S as the resultNote: ⌊x⌋ represents the integer part of x.
For non-programmers: start with a positive, integer number (say, 19). Divide it by 2, discarding remainders, until you get to 0. (In this case, you'd get 9, 4, 2, and 1.) Sum all the quotients. (9+4+2+1=16.) Subtract the sum from the original number. (19-16=3.) What's the result?