September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,14 @@
100 PROGRAM "Josephus.bas"
110 INPUT PROMPT "Number of prisoners: ":NP
120 INPUT PROMPT "Execution step: ":EX
130 INPUT PROMPT "How many survivors: ":SU
140 PRINT "Survivors:";
150 FOR S=0 TO SU-1
160 PRINT JOSEPHUS(NP,EX,S);
170 NEXT
180 DEF JOSEPHUS(N,K,M)
190 FOR I=M+1 TO N
200 LET M=MOD((M+K),I)
210 NEXT
220 LET JOSEPHUS=M
230 END DEF

View file

@ -0,0 +1,6 @@
n = ARGV.fetch(0, 41).to_i # n default is 41 or ARGV[0]
k = ARGV.fetch(1, 3).to_i # k default is 3 or ARGV[1]
prisoners = (0...n).to_a
while prisoners.size > 1; prisoners.rotate!(k-1).shift end
puts "From #{n} prisoners, eliminating each prisoner #{k} leaves prisoner #{prisoners.first}."

View file

@ -0,0 +1,12 @@
Function Josephus (n As Integer, k As Integer, m As Integer) As Integer
Dim As Integer lm = m
For i As Integer = m + 1 To n
lm = (lm + k) Mod i
Next i
Josephus = lm
End Function
Dim As Integer n = 41 'prisioneros
Dim As Integer k = 3 'orden de ejecución
Print "n ="; n, "k ="; k, "superviviente = "; Josephus(n, k, 0)

View file

@ -1,4 +1,3 @@
<lang PowerShell>
function Get-JosephusPrisoners ( [int]$N, [int]$K )
{
# Just for convenience

View file

@ -0,0 +1,36 @@
void setup() {
println("Survivor: " + execute(41, 3));
println("Survivors: " + executeAllButM(41, 3, 3));
}
int execute(int n, int k) {
int killIdx = 0;
IntList prisoners = new IntList(n);
for (int i = 0; i < n; i++) {
prisoners.append(i);
}
println("Prisoners executed in order:");
while (prisoners.size() > 1) {
killIdx = (killIdx + k - 1) % prisoners.size();
print(prisoners.get(killIdx) + " ");
prisoners.remove(killIdx);
}
println();
return prisoners.get(0);
}
IntList executeAllButM(int n, int k, int m) {
int killIdx = 0;
IntList prisoners = new IntList(n);
for (int i = 0; i < n; i++) {
prisoners.append(i);
}
println("Prisoners executed in order:");
while (prisoners.size() > m) {
killIdx = (killIdx + k - 1) % prisoners.size();
print(prisoners.get(killIdx) + " ");
prisoners.remove(killIdx);
}
println();
return prisoners;
}

View file

@ -0,0 +1,26 @@
const N: usize = 41;
const K: usize = 3;
const M: usize = 3;
const POSITION: usize = 5;
fn main() {
let mut prisoners: Vec<usize> = Vec::new();
let mut executed: Vec<usize> = Vec::new();
for pos in 0..N {
prisoners.push(pos);
}
let mut to_kill: usize = 0;
let mut len: usize = prisoners.len();
while len > M {
to_kill = (to_kill + K - 1) % len;
executed.push(prisoners.remove(to_kill));
len -= 1;
}
println!("JOSEPHUS n={}, k={}, m={}", N, K, M);
println!("Executed: {:?}", executed);
println!("Executed position number {}: {}", POSITION, executed[POSITION - 1]);
println!("Survivors: {:?}", prisoners);
}

View file

@ -0,0 +1,25 @@
Module Module1
'Determines the killing order numbering prisoners 1 to n
Sub Josephus(n As Integer, k As Integer, m As Integer)
Dim p = Enumerable.Range(1, n).ToList()
Dim i = 0
Console.Write("Prisoner killing order:")
While p.Count > 1
i = (i + k - 1) Mod p.Count
Console.Write(" {0}", p(i))
p.RemoveAt(i)
End While
Console.WriteLine()
Console.WriteLine("Survivor: {0}", p(0))
End Sub
Sub Main()
Josephus(5, 2, 1)
Console.WriteLine()
Josephus(41, 3, 1)
End Sub
End Module

View file

@ -0,0 +1,12 @@
10 LET n=41: LET k=3: LET m=0
20 GO SUB 100
30 PRINT "n= ";n;TAB (7);"k= ";k;TAB (13);"final survivor= ";lm
40 STOP
100 REM Josephus
110 REM Return m-th on the reversed kill list; m=0 is final survivor.
120 LET lm=m: REM Local copy of m
130 FOR a=m+1 TO n
140 LET lm=FN m(lm+k,a)
150 NEXT a
160 RETURN
200 DEF FN m(x,y)=x-INT (x/y)*y: REM MOD function