Pattern visualizer
Josephus Problem
Simulating the circle directly is the most literal reading of the problem: keep an array of who is still standing, repeatedly count k people from the current position and remove the k-th, and whoever is left is the survivor. Removing an element shrinks the array in place, so the next count simply resumes at the same index — the person who used to be next in line is now sitting right there. This mirrors the recursive idea (the survivor of a smaller circle maps onto the bigger one by shifting forward by k) but makes every intermediate state visible instead of unwinding a call stack. Animated on: n=5 people in a circle, eliminate every k=2-th person. Answer: position 3..
Shrinking the circle one elimination at a time
5 people stand in a circle, positions 1..5. Counting k=2 people at a time and removing the k-th, over and over, until one remains. The circle is the live state — no separate "removed" list needed, since a shrinking array already models who is still standing.
1FUNCTION josephus(n, k)2 circle <- [1, 2, ..., n]3 idx <- 04 WHILE LENGTH(circle) > 15 idx <- (idx + k - 1) MOD LENGTH(circle)6 REMOVE circle[idx] FROM circle7 idx <- idx MOD LENGTH(circle)8 RETURN circle[0]
← / → step · space play · Home restart