How to resolve the algorithm Josephus problem step by step in the Julia programming language
How to resolve the algorithm Josephus problem step by step in the Julia programming language
Table of Contents
Problem Statement
Josephus problem is a math puzzle with a grim description:
n
{\displaystyle n}
prisoners are standing on a circle, sequentially numbered from
0
{\displaystyle 0}
to
n − 1
{\displaystyle n-1}
.
An executioner walks along the circle, starting from prisoner
0
{\displaystyle 0}
, removing every
k
{\displaystyle k}
-th prisoner and killing him.
As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed. >
For example, if there are
n
5
{\displaystyle n=5}
prisoners and
k
2
{\displaystyle k=2}
, the order the prisoners are killed in (let's call it the "killing sequence") will be 1, 3, 0, and 4, and the survivor will be #2.
Given any
n , k
0
{\displaystyle n,k>0}
, find out which prisoner will be the final survivor.
In one such incident, there were 41 prisoners and every 3rd prisoner was being killed (
k
3
{\displaystyle k=3}
).
Among them was a clever chap name Josephus who worked out the problem, stood at the surviving position, and lived on to tell the tale.
Which number was he?
The captors may be especially kind and let
m
{\displaystyle m}
survivors free, and Josephus might just have
m − 1
{\displaystyle m-1}
friends to save.
Provide a way to calculate which prisoner is at any given position on the killing sequence.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Josephus problem step by step in the Julia programming language
The provided Julia code implements the Josephus problem, which is a mathematical puzzle that simulates the elimination process in a circular firing squad. Here's a detailed explanation of the code:
-
Memoization: The
@memoize
macro is used to memoize thejosephus
function. Memoization is a technique that stores the results of function calls for a given set of input parameters. For thejosephus
function, the input parameters aren
,k
, andm
, and the result is the sequence of eliminated prisoners. By memoizing the function, subsequent calls with the same input parameters will return the cached result instead of recomputing it, which can significantly improve performance. -
Recursive Definition of
josephus
: The recursive definition of thejosephus
function is as follows:- If there is only one prisoner left (i.e.,
n
is equal tom
), the function returns a collection of all prisoners' indices from 0 tom-1
. - Otherwise, the function recursively calls itself with
n-1
prisoners, the samek
, andm
unchanged. It then uses the modulo operator to determine the next prisoner to be eliminated based on the number of prisonersn
and the killing stepk
. This process continues until there is only one prisoner left.
- If there is only one prisoner left (i.e.,
-
Recursive Implementation of
josephus
: This part of the code provides an alternative implementation of the Josephus problem using recursion. It iteratively eliminates prisoners in a circular fashion, keeping track of the remaining prisoners in thep
vector and the eliminated prisoners in theseq
vector. The process continues until onlym
prisoners remain. -
Examples: The code includes two examples of using the
josephus
function:- The first example calls
josephus(41, 3)
and prints the sequence of eliminated prisoners and the survivor. In this case, there are 41 prisoners, the killing step is 3, and the prison at index 30 survives. - The second example calls
josephus(41, 3, 5)
and prints the sequence of eliminated prisoners and the survivor. In this case, the starting prison is set to index 5, and the prison at index 35 survives.
- The first example calls
-
Additional Functions: The code defines an additional function called
josephus
with a different signature. This function takes the same input parameters as the recursive implementation but returns two values: a vector of eliminated prisoners (seq
) and a vector of surviving prisoners (surv
). -
Examples: The code includes two examples of using this alternative
josephus
function:- The first example calls
josephus(41, 3)
and prints the sequence of eliminated prisoners and the survivor. - The second example calls
josephus(41, 3, 3)
and prints the sequence of eliminated prisoners and the survivor.
- The first example calls
Source code in the julia programming language
using Memoize
@memoize josephus(n::Integer, k::Integer, m::Integer=1) = n == m ? collect(0:m .- 1) : mod.(josephus(n - 1, k, m) + k, n)
@show josephus(41, 3)
@show josephus(41, 3, 5)
function josephus(n::Integer, k::Integer, m::Integer=1)
p, i, seq = collect(0:n-1), 0, Vector{typeof(n)}(0)
while length(p) > m
i = (i + k - 1) % length(p)
push!(seq, splice!(p, i + 1))
end
return seq, p
end
seq, surv = josephus(41, 3)
println("Prisoner killing in order: $seq\nSurvivor: $surv")
seq, surv = josephus(41, 3, 3)
println("Prisoner killing in order: $seq\nSurvivor: $surv")
You may also check:How to resolve the algorithm Pi step by step in the C# programming language
You may also check:How to resolve the algorithm Leonardo numbers step by step in the VBA programming language
You may also check:How to resolve the algorithm Ramer-Douglas-Peucker line simplification step by step in the RPL programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the Haskell programming language