2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,8 +1 @@
function j(n,k)
p, i, seq=[0:n-1], 0, Int[]
while !isempty(p)
i=(i+k-1)%length(p)
push!(seq,splice!(p,i+1))
end
@sprintf("Prisoner killing order: %s.\nSurvivor: %i",replace(chomp(string(seq[1:end-1])),"\n",", "),seq[end])
end
josephus(n, k, m=1) = n == m ? collect(0:m-1) : mod(josephus(n-1, k, m) + k, n)

View file

@ -1,3 +1,4 @@
julia> print(j(41,3))
Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3, 34, 15.
Survivor: 30
julia> print(josephus(41,3))
[30]
julia> print(josephus(41,3,5))
[3,15,21,30,34]

View file

@ -1,9 +1,8 @@
function j2(n,k,m)
function j(n,k)
p, i, seq=[0:n-1], 0, Int[]
while length(p)>m
while !isempty(p)
i=(i+k-1)%length(p)
push!(seq,splice!(p,i+1))
end
prt_array(x)=replace(chomp(string(x)),"\n",", ")
@sprintf("Prisoner killing order: %s.\nSurvivors: %s",prt_array(seq),"["*prt_array(p)*"]")
@sprintf("Prisoner killing order: %s.\nSurvivor: %i",replace(chomp(string(seq[1:end-1])),"\n",", "),seq[end])
end

View file

@ -1,3 +1,3 @@
julia> print(j2(41,3,3))
Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3.
Survivors: [15, 30, 34]
julia> print(j(41,3))
Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3, 34, 15.
Survivor: 30

View file

@ -0,0 +1,9 @@
function j2(n,k,m)
p, i, seq=[0:n-1], 0, Int[]
while length(p)>m
i=(i+k-1)%length(p)
push!(seq,splice!(p,i+1))
end
prt_array(x)=replace(chomp(string(x)),"\n",", ")
@sprintf("Prisoner killing order: %s.\nSurvivors: %s",prt_array(seq),"["*prt_array(p)*"]")
end

View file

@ -0,0 +1,3 @@
julia> print(j2(41,3,3))
Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3.
Survivors: [15, 30, 34]