12 Temmuz 2008 Cumartesi

loto0.m

%    #######################  loto0.m DRAFT  #######################
%
% Author : Senol Korkmaz <senol.korkmaz@gmail.com>
% Filename : loto0.m
% Description : A MATLAB script to calculate n/m\k
% Usage : Run script and follow instructions.
% Version : 0 (Draft)
%
% License :
GNU/GPL v3
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.

%% Clear all variables and screen
clear;
clc;

%% Get user inputs n,m,k and validate
n = input(' n : ');
if ~(n>1)
error(' n must be greater than 1 (n=%d)',n);
end;

m = input(' m : ');
if ~(m>n)
error(' m must be greater than n (m=%d , n=%d)',m,n);
end;

k = input(' k : ');
if (k>n)
error(' k cannot be greater than n (k=%d , n=%d)',k,n)
end;

%% Generate variables

tmp = 0; % Temporary variable
flg = false; % Flag variable
covered = false; % Is selections covered target_pool in scope k ?
sub_combinations = 0; %

i = 0; % index to be used within loops
j = 0; % index to be used within loops
l = 0; % index to be used within loops
z = 0; % index to be used within loops

target_pool = combnk(1:m,n); % One of these combinations will be selected in loto
source_pool = target_pool; % Min of these combinations should be selected to cover target_pool at scope k
selections = zeros(1,n); % Current selections to test
selections_scope = [];
show_info = 0;

%% cycle throught combinations

while ~(covered) % Main loop
i = i + 1; % It is not enought, increase i
sub_combinations = combnk(1:size(source_pool,1),i); % Generate subcombinations
j=0;
z=size(sub_combinations,1);
show_info_period = z/100;

while ( (j < z) && ~(covered)) % #######
j = j+1;
selections = source_pool(sub_combinations(j,:),:);
show_info = show_info+1;

if (show_info > show_info_period)
disp(sprintf('is %d enought ? sub-Combination %d/%d , Completed = %d %%',i,j,z,fix(j/show_info_period)));
show_info = 0;
end;

selections_scope = selections(1,1:k); % ###################

for
l=1:i % Prepare k scope to test target_pool ########
selections_scope = union(selections_scope,combnk(selections(l,:),k),'rows');
end;

for l=1:size(target_pool,1)
if (size(intersect(combnk(target_pool(l,:),k),selections_scope,'rows'),1)==0)
break;
end;
if (l==size(target_pool,1))
covered = true;
end;
end;
end;
end;

%% Show results

disp(sprintf('Answer = %d',i));

Hiç yorum yok:

Konular

Matematik (5) Kod (4) Gündem (2) Bilgisayar (1) İnternet (1)