Josephus problem: Difference between revisions

Content added Content deleted
m (→‎{{header|R}}: Syntax highlighting.)
m (→‎Iterative solution: Improved syntax.)
Line 3,527: Line 3,527:
*The idiomatic way to roll an array in R (e.g. as [[Josephus_problem#Ruby|the Ruby solution]] has) is to exploit the head and tail functions, but those break if we are rolling by more than the length of the array (see https://stackoverflow.com/q/18791212 for a few tricks for this).
*The idiomatic way to roll an array in R (e.g. as [[Josephus_problem#Ruby|the Ruby solution]] has) is to exploit the head and tail functions, but those break if we are rolling by more than the length of the array (see https://stackoverflow.com/q/18791212 for a few tricks for this).
Regardless, it is still solvable. The following adapts a great deal of [[Josephus_problem#Lua|the Lua solution]]. The arguments n, k, and m are as in the task description.
Regardless, it is still solvable. The following adapts a great deal of [[Josephus_problem#Lua|the Lua solution]]. The arguments n, k, and m are as in the task description.
<lang rsplus>josephusProblem<-function(n,k,m)
<lang rsplus>josephusProblem <- function(n, k, m)
{
{
prisoners<-0:(n-1)
prisoners <- 0:(n - 1)
exPos<-countToK<-1
exPos <- countToK <- 1
dead<-integer(0)
dead <- integer(0)
while(length(prisoners)>m)
while(length(prisoners) > m)
{
{
if(countToK==k)
if(countToK == k)
{
{
dead<-c(dead,prisoners[exPos])
dead <- c(dead, prisoners[exPos])
prisoners<-prisoners[-exPos]
prisoners <- prisoners[-exPos]
exPos<-exPos-1
exPos <- exPos - 1
}
}
exPos<-exPos+1
exPos <- exPos + 1
countToK<-countToK+1
countToK <- countToK + 1
if(exPos>length(prisoners)){exPos<-1}
if(exPos > length(prisoners)) exPos <- 1
if(countToK>k){countToK<-1}
if(countToK > k) countToK <- 1
}
}
print(paste0("Execution order: ",paste0(dead,collapse =", "),"."))
print(paste0("Execution order: ", paste0(dead, collapse = ", "), "."))
paste0("Survivors: ",paste0(prisoners,collapse =", "),".")
paste0("Survivors: ", paste0(prisoners, collapse = ", "), ".")
}</lang>
}</lang>
{{out}}
{{out}}
<pre>> josephusProblem(5,2,1)
<pre>> josephusProblem(5, 2, 1)
[1] "Execution order: 1, 3, 0, 4."
[1] "Execution order: 1, 3, 0, 4."
[1] "Survivors: 2."
[1] "Survivors: 2."
> josephusProblem(41,3,1)
> josephusProblem(41, 3, 1)
[1] "Execution 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."
[1] "Execution 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."
[1] "Survivors: 30."
[1] "Survivors: 30."
> josephusProblem(41,3,3)
> josephusProblem(41, 3, 3)
[1] "Execution 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."
[1] "Execution 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."
[1] "Survivors: 15, 30, 34."</pre>
[1] "Survivors: 15, 30, 34."</pre>