FlamingFox

0 Reputation

One Badge

8 years, 133 days

MaplePrimes Activity


These are replies submitted by FlamingFox

@vv Fixed. Copy Paste error. Not going to change the condition. It actually looks better in my mind that way.

JosephusImproved := proc (n, k)
local N:
if n = 1 then
return 0:
elif 1 < n < k then
return JosephusImproved(n - 1, k) + k mod n:
end if:
N := n - floor(n / k):
return floor(k * ((JosephusImproved(N,k)-(n mod k)) mod N)/(k - 1))
end proc:

@vv Ironically, this is the only function that can perform JosephusImproved(123456789101112, 13); without causing Error: Too many Recursive steps or too large a sequence. And incase you want it, the other two procedures I made and fixed:

JosephusCircle := proc (n, k)
local table, pos, count:
table := [seq(q, q = 0 .. n-1)]:
pos := 0:
count := 1:
while 1 < nops(table) do
pos := pos + k - 1 mod nops(table):
table := subsop(pos + 1 = NULL, table):
count := count + 1:
end do:
return table[1]:
end proc:
JosephusDynamic := proc (n, k)
if n = 1 then
return 0:
else
return JosephusDynamic(n - 1, k) + k mod n:
end if:
end proc:

Now I have all three procedures running fine :)

@vv Alright, so in the end I figured out that what I had initially was wrong. There were multiple interpretations of the problem, and I chose the wrong one. I've since fixed my code, and now all three procedures work as they should. Thanks @vv and @Kitonum.

@Kitonum Like I said, I already have the original recursive formula working. All I wanted was to apply the General Case shown here. The idea behind the improved Josephus algorithm is to, within a single execution cycle, kill off floor(n/k) people at once. For example if you had a circle of 42 people and your step was 13, after a single cycle, you should have accounted for floor(42/13) people = 3 people that have died in that first round the circle. That is what I am trying to achieve using recursion.

Page 1 of 1