This project implements a model of the Josephus Circle problem
The Josephus Circle problem is a famous theoretical problem in mathematics and computer science. The problem is named after Flavius Josephus, a Jewish historian who lived in the 1st century. According to Josephus's own account, during the Jewish-Roman War, he and his 40 soldiers were trapped in a cave, surrounded by Roman soldiers. They chose suicide over capture and decided to form a circle and, proceeding around it, to kill every third person remaining until no one was left. Josephus, preferring capture to death, figured out where he needed to sit to be the last survivor. Using this strategy, he was able to survive and later surrendered to the Romans.
The Josephus problem is a classic example of a survival problem and has various applications in computer science, particularly in the fields of algorithms and data structures. It is often used to illustrate algorithmic thinking and problem-solving techniques. There are recursive algorithms to solve the Josephus problem for any given number of people n
and step size m
.
Here is the recursive function
Just click run to run this model.
We were trying to model the Josephus Circle Problem described above. We hoped to use Forge to calculate who survives to the end.
We didn't create a custom visualization. Because the number of parameters is so large, we recommended to observe by time projecting to each STATE. By constantly clicking the next state, we can observe the whole process.
We ran traces
on different values for Circle.personSkipped
, which is the number of people that are skipped before someone is removed. Run State0.person
in the Sterling Evaluator to see the starting person. We stepped through each State
to confirm that the model ran correctly. To boost our confidence even more, we tested each predicate in test expect
blocks, with some tests in the traces
test suite that mirrors our run
statements for different inputs for this problem.
Circle
is a trace of the entire model trace, similar to the trace in the ring election lab.Person
refers to each person, they are connected to form a ring at the beginning and the end.State
refers to the state of each second, assuming that every second is counted. Unlike in reality, dead people also count in his moment.wellformed
,init
, andendState
are helper predicates to constrain certain states in our model.validTransitions
containsdoNothing
,incrementCount
, andremovePerson
. These are the valid transitions between states in our model.traces
is the predicate that outputs consecutive states of our model.- The remaining predicates are helpers for the predicates listed above.
For the model itself (traces
), we wrote some tests that mirrored our run
statements (i.e. examples of the Josephus Circle Problem).
For the other predicates, we wrote tests that asserted theorems that each predicate should require.
See code for comments.