June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,7 @@
10 N=41
20 K=3
30 M=0
40 FOR I=M+1 TO N
50 M=INT(I*((M+K)/I-INT((M+K)/I))+0.5)
60 NEXT I
70 PRINT "Survivor is number";M

View file

@ -0,0 +1,5 @@
10 DEF FN MOD(X) = X - INT (X / A) * A
20 LM = 0: INPUT "GIVE N AND K (N,K): ";N,K
30 IF N < 1 or K < 1 THEN GOTO 20
40 FOR A = 1 TO N: LM = FN MOD(LM + K): NEXT A
50 PRINT "N = ";N;", K = ";K;", SURVIVOR: ";LM

View file

@ -0,0 +1,10 @@
REM >josephus
PRINT "Survivor is number "; FNjosephus(41, 3, 0)
END
:
DEF FNjosephus(n%, k%, m%)
LOCAL i%
FOR i% = m% + 1 TO n%
m% = (m% + k%) MOD i%
NEXT
= m%

View file

@ -1 +1,5 @@
josephus(n, k, m=1) = n == m ? collect(0:m-1) : mod(josephus(n-1, k, m) + k, n)
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)

View file

@ -1,4 +1,14 @@
julia> print(josephus(41,3))
[30]
julia> print(josephus(41,3,5))
[3,15,21,30,34]
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")

View file

@ -0,0 +1,38 @@
function [indAlive] = josephus(numPeople,count)
% Josephus: Given a circle of numPeople individuals, with a count of count,
% find the index (starting at 1) of the survivor [see Josephus Problem]
%% Definitions:
% 0 = dead position
% 1 = alive position
% index = # of person
%% Setting up
arrPeople = ones(1, numPeople);
currInd = 0;
%% Counting
while (length(arrPeople(arrPeople == 1)) > 1) % While more than 1 person is alive
counter = 0;
while counter ~= count % Counting until we hit the count
currInd = currInd + 1; % Move to the next person
if currInd > numPeople % If overflow, wraparound
currInd = currInd - numPeople;
end
if arrPeople(currInd) % If the current person is alive
counter = counter + 1; % Add 1 person to the count
%fprintf("Index: %d \t| Counter: %d\n", currInd, counter) % Uncomment to display index and counter location
end
end
arrPeople(currInd) = 0; % Kill the person we reached
%fprintf("Killed person %d \n", currInd) % Uncomment to display order of killing
%disp(arrPeople) % Uncomment to display current status of people
end
indAlive = find(arrPeople);
end

View file

@ -0,0 +1,26 @@
MODULE Josephus;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
PROCEDURE Josephus(n,k : INTEGER) : INTEGER;
VAR a,m : INTEGER;
BEGIN
m := 0;
FOR a:=1 TO n DO
m := (m + k) MOD a;
END;
RETURN m
END Josephus;
VAR
buf : ARRAY[0..63] OF CHAR;
n,k,i : INTEGER;
nl,kl,il : LONGCARD;
BEGIN
n := 41;
k := 3;
FormatString("n = %i, k = %i, final survivor: %i\n", buf, n, k, Josephus(n, k));
WriteString(buf);
ReadChar
END Josephus.

View file

@ -0,0 +1,16 @@
sub Execute(@prisoner, $k) {
until @prisoner == 1 {
@prisoner.=rotate($k - 1);
@prisoner.shift;
}
}
my @prisoner = ^41;
Execute @prisoner, 3;
say "Prisoner {@prisoner} survived.";
# We don't have to use numbers. Any list will do:
my @dalton = <Joe Jack William Averell Rantanplan>;
Execute @dalton, 2;
say "{@dalton} survived.";

View file

@ -0,0 +1,23 @@
#general solution
(de jo (N K)
(if (=1 N)
1
(inc
(%
(+ (dec K) (jo (dec N) K))
N ) ) ) )
#special case when K is 2; much faster than general version.
(de jo2(N)
(let P 1
(while (<= P N)
(setq P (* 2 P))
(+ (- (* 2 N) P) 1) ) ) )
# find the survivor using an optimal solution
(de survivor (N K)
(if (=0 (% N 2))
(jo2 N)
(jo N K) ) )
(print (survivor 5 2))
(print (survivor 41 3))

View file

@ -0,0 +1,33 @@
from itertools import compress, cycle
def josephus(prisoner, kill, surviver):
p = range(prisoner)
k = [0] * kill
k[kill-1] = 1
s = [1] * kill
s[kill -1] = 0
queue = p
queue = compress(queue, cycle(s))
try:
while True:
p.append(queue.next())
except StopIteration:
pass
kil=[]
killed = compress(p, cycle(k))
try:
while True:
kil.append(killed.next())
except StopIteration:
pass
print 'The surviver is: ', kil[-surviver:]
print 'The kill sequence is ', kil[:prisoner-surviver]
josephus(41,3,2)
The surviver is: [15, 30]
The kill sequence is [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]
josephus(5,2,1)
The surviver is: [2]
The kill sequence is [1, 3, 0, 4]