Skip to end of metadata
Go to start of metadata

a)

pool = 1:1:34;

b)

function ticket = getTicket(pool, N)
for i = 1:N
    k = randi(length(pool));
    ticket(i) = pool(k);
    pool(k) = [];
end
end

c)

function equals = compareLists(list1, list2)
equals = 0;
for i = 1:length(list1)
    if ismember(list1(i), list2)
        equals = equals + 1;
    end
end
end

d)

function prize = prizeSize(correctNumbers, correctAdditionalNumbers)
if correctNumbers==7
    prize = 2749455;
elseif correctNumbers == 6 && correctAdditionalNumbers >=1
    prize = 102110;
elseif correctNumbers == 6
    prize = 3385;
elseif correctNumbers == 5
    prize = 95;
elseif correctNumbers == 4 && correctAdditionalNumbers >=1
    prize = 45;
else
    prize = 0;
end
end

e)

function lottoSimulator()
pool = 1:34;
myTicket = zeros(10,7);
fprintf('Dine tall:\n')
for i = 1:10
    fprintf('\nRekke %d: ', i);
    myTicket(i,:) = sort(getTicket(pool, 7));
    fprintf('%3d ', myTicket(i,:));
end
pause(3);
drawnNumbers = getTicket(pool, 10);
fprintf('\nTrukne tall:\n');
for j = 1:7
    pause(0.5);
    fprintf('%5d ',drawnNumbers(j));
end
fprintf('\nTilleggstall:\n');
for k = 1:3
    pause(0.5);
    fprintf('%5d ',drawnNumbers(7 + k));
end
prize = 0;
for h = 1:10
    correctNumbers = compareLists(myTicket(h,:), drawnNumbers(1:7));
    correctAdditionalNumbers = compareLists(myTicket(h,:), drawnNumbers(8:10));
    prize = prize + priseSize(correctNumbers, correctAdditionalNumbers);
end
 fprintf('\nDu har vunnet: %d kroner\n', prize);
end

f)

function bool = smartInvestment()
totalWinnings = 0;
numberOfCoupons = 7400/5;
pool = [1:34];
drawnNumbers = getTicket(pool, 10);
for i = 1:numberOfCoupons
    myCoupon = getTicket(pool, 7);
    correctNumbers = compareLists(myCoupon, drawnNumbers(1:7));
    correctAdditionalNumbers = compareLists(myCoupon, drawnNumbers(8:10));
    totalWinnings = totalWinnings + prizeSize(correctNumbers, correctAdditionalNumbers);
end
fprintf('Total gevinst: %d', totalWinnings);
bool = totalWinnings > 7400;
end

Etter å ha kjørt koden 20 ganger, har det ennå ikke lønnet seg å spille lotto. 

 

Sidetype

Ferdig

Undersider, revidert

Undersider, til revisjon

løsning

70

0

0

  • No labels