Express yourself

Computer Science Level pending

Suppose you were to flip an unbiased coin 20 times. Consider the probability of seeing at least one occurrence of the pattern consisting of one head immediately followed by one tail immediately followed by one head immediately followed by either a head or a tail immediately followed by zero or more heads. If this probability is expressed as a fraction in lowest terms then what is the sum of the fraction's numerator and denominator.


The answer is 993171.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Bill Bell
Oct 17, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from itertools import product,repeat
from re import compile
from fractions import Fraction

N=20
patt=compile('HTH[HT]H*')

coin=['H','T']

count=0
total=0
for toss in product(*repeat(coin,N)):
    total+=1
    result=''.join(toss)
    m=patt.search(result)
    if m:
        count+=1
print N
print count, total, Fraction(count,total)
print Fraction(count,total).numerator+Fraction(count,total).denominator

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...