Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Josephus-problem/Julia/josephus-problem-1.julia
Normal file
5
Task/Josephus-problem/Julia/josephus-problem-1.julia
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
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)
|
||||
14
Task/Josephus-problem/Julia/josephus-problem-2.julia
Normal file
14
Task/Josephus-problem/Julia/josephus-problem-2.julia
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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")
|
||||
Loading…
Add table
Add a link
Reference in a new issue