Given an alphabet of 3 letters i.e. a,b,c evaluate
the number of n-letter words containing even number
of “a”s.
(In reply to
solution by Charlie)
Of course, reading Larry's post reminded me that zero is an even number, so:
clearvars, clc
for n=1:25
tot=0;
for i=0:2:n
tot=tot+nchoosek(n,i)*2^(n-i);
end
fprintf('%2d %15d\n',n,tot);
end
1 2
2 5
3 14
4 41
5 122
6 365
7 1094
8 3281
9 9842
10 29525
11 88574
12 265721
13 797162
14 2391485
15 7174454
16 21523361
17 64570082
18 193710245
19 581130734
20 1743392201
21 5230176602
22 15690529805
23 47071589414
24 141214768241
25 423644304722
|
Posted by Charlie
on 2022-04-27 08:50:11 |