Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/Josephus-problem/MATLAB/josephus-problem.m
Normal file
38
Task/Josephus-problem/MATLAB/josephus-problem.m
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue