Joe flips a coin 20 times, find the probability that he never gets 2 consecutive heads or 3 consecutive tails.
clearvars,clc
global series goodCt
series=''; goodCt=0;
addon;
goodCt
2^20
goodCt/2^20
2^20 / goodCt
function addon()
global series goodCt
for new='01'
saveseries=series;
if length(series)<(new=='0')+1
series=[series,new];
else
if new=='1' && series(end)=='1'
continue
end
if new=='0' && isequal(series(end-1:end),'00')
continue
end
series(end+1)=new;
end
if length(series)==20
goodCt=goodCt+1;
else
addon;
end
series=saveseries;
end
end
finds
goodCt =
465
ans =
1048576
ans =
0.000443458557128906
ans =
2255.00215053763
indicating that 465 out of the 2^20 = 1,048,576 possible sequences fit the criteria for a probability of 0.000443458557128906, or about 1 in 2255.
|
Posted by Charlie
on 2024-11-12 09:53:46 |