clear
K = 34;

pe = 0.9;
B = [ 1 zeros(1,K-2)];
S0 = [ zeros(K-2,1); pe ]
S = zeros(K-1,K-1);

for i=1:K-1
    S(i,i) = 1-pe;
    if (i < K-1)        
        S(i,i+1) = pe;
    end
end
S
total = 0;
i = K;
go = 1;
while ((total < 0.999) && (i < 500) && (go == 1))
    calc = choose(i-1,K-1)*(1-pe)^(i-K)*pe^(K);
            
    if ((calc ~= NaN) && (calc ~= Inf))
        total = total + calc;
        go = 1;
        Dist(i-K+1) = calc;
        Num(i-K+1) = i;
        if ((i-K+1) == 1)
            Cumlative(i-K+1) = 0;
        else
            Cumlative(i-K+1) = Cumlative(i-K) + calc;
        end
    else
        go = 0;        
        i        
    end;
    i = i+1;
end

bar(Num,Dist)
%figure
%bar(Num,Cumlative)

