All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

Given positive, integer N, what does this algorithm produce?
```let S and D = N
repeat
let D = ⌊D/2⌋
subtract D from S
until D=0
produce S as the result```
Note: ⌊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?

 See The Solution Submitted by e.g. Rating: 3.7143 (7 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
 An implementation (with results--a Hint--so look only if you want the hint) | Comment 1 of 9

Here's an implementation.  It actually does a little more, as it loops through and shows the results for several values of N.

CLS
FOR n = 1 TO 40
s = n: d = n
DO
d = INT(d / 2)
s = s - d
LOOP UNTIL d = 0
PRINT USING "##  ## "; n; s
NEXT

Resulting in:

` `
` `
` 1   1 2   1 3   2 4   1 5   2 6   2 7   3 8   1 9   210   211   312   213   314   315   416   117   218   219   320   221   322   323   424   225   326   327   428   329   430   431   532   133   234   235   336   237   338   339   440   2`

 Posted by Charlie on 2005-01-14 15:16:49

 Search: Search body:
Forums (0)