09.12.2012 Views

Concrete mathematics : a foundation for computer science

Concrete mathematics : a foundation for computer science

Concrete mathematics : a foundation for computer science

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

80 INTEGER FUNCTIONS<br />

We’ve got more tools to work with than we had in Chapter 1, so let’s<br />

consider the more authentic Josephus problem in which every third person is<br />

eliminated, instead of every second. If we apply the methods that worked in<br />

Chapter 1 to this more difficult problem, we wind up with a recurrence like<br />

J3(n) = [iJ3(Ljnl) + a,] modn+ 1,<br />

where ‘mod’ is a function that we will be studying shortly, and where we have<br />

a,, = -2, +1 , or -i according as n mod 3 = 0, 1, or 2. But this recurrence<br />

is too horrible to pursue.<br />

There’s another approach to the Josephus problem that gives a much<br />

better setup. Whenever a person is passed over, we can assign a new number.<br />

Thus, 1 and 2 become n + 1 and n + 2, then 3 is executed; 4 and 5 become<br />

n + 3 and n + 4, then 6 is executed; . . . ; 3kSl and 3k+2 become n+2k+ 1<br />

and n + 2k + 2, then 3k + 3 is executed; . . . then 3n is executed (or left to<br />

survive). For example, when n = 10 the numbers are<br />

1 2 3 4 5 6 7 8 9 10<br />

11 12 13 14 15 16 17<br />

18 19 20 21 22<br />

23 24 25<br />

26 27<br />

28<br />

29<br />

30<br />

The kth person eliminated ends up with number 3k. So we can figure out who<br />

the survivor is if we can figure out the original number of person number 3n.<br />

If N > n, person number N must have had a previous number, and we<br />

can find it as follows: We have N = n + 2k + 1 or N = n + 2k + 2, hence<br />

k = [(N - n - 1)/2J ; the previous number was 3k + 1 or 3k + 2, respectively.<br />

That is, it was 3k + (N - n - 2k) = k + N - n. Hence we can calculate the<br />

survivor’s number J3 (n) as follows:<br />

N := 3n;<br />

while N>n do N:= [“-r-‘] +N-n;<br />

J3(n) := N.<br />

This is not a closed <strong>for</strong>m <strong>for</strong> Jj(n); it’s not even a recurrence. But at least it<br />

“Not too slow,<br />

not too fast,”<br />

tells us how to calculate the answer reasonably fast, if n is large. -L. Amstrong

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!