Pages

Thursday, June 26, 2014

Python code for implementing Golomb sequence or Silverman's sequence

Today, I have come across this article in wikipedia. It describes about certain sequence which is surprising to know.
The article is as follows :

"In mathematics, the Golomb sequence, named after Solomon W. Golomb (but also called Silverman's sequence), is a non-decreasing integer sequence where an is the number of times that n occurs in the sequence, starting with a1 = 1, and with the property that for n > 1 each an is the unique integer which makes it possible to satisfy the condition. For example, a1 = 1 says that 1 only occurs once in the sequence, so a2 cannot be 1 too, but it can be, and therefore must be, 2. The first few values are
1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12 (sequence A001462 in OEIS).
a1 = 1
Therefore 1 occurs exactly one time in this sequence.
a2 > 1
a2 = 2
2 occurs exactly 2 times in this sequence.
a3 = 2
3 occurs exactly 2 times in this sequence.
a4 = a5 = 3
4 occurs exactly 3 times in this sequence.
5 occurs exactly 3 times in this sequence.
a6 = a7 = a8 = 4
a9 = a10 = a11 = 5
etc."

Let us see a python code to implement the same.

The code is given below :

a=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(1,45):
    a[1]=1
    a[i+1]=1+a[i+1-a[a[i]]]
    print(a[i])

And here is the answer :
>>>
1
2
2
3
3
4
4
4
5
5
5
6
6
6
6
7
7
7
7
8
8
8
8
9
9
9
9
9
10
10
10
10
10
11
11
11
11
11
12
12
12
12
12
12
>>> 
Here, I have given n=45, one can change this in the range() and run the code for any other number of iterations.

Tuesday, June 24, 2014

Python code for finding the probability of ropes problem


Q. No. 6 :

Three identical skipping ropes are dropped in a heap. The six free ends of the ropes are picked up
by three people—each person picks up one free end with his right hand and another free end with his left. Then they all step away from each other.
(a) What is the probability that every person finds he is holding a single skipping rope?
(b) What is the probability that the three people are joined in a single closed chain?

Answer :

(a) What is the probability that every person finds he is holding a single skipping rope?
There are 3 ropes.
Each rope has two ends.
Therefore, there are a total of 6 ends.
Let us call the rope ends as follows :


Rope                               Ends
  1                                   1 – 2
  2                                   3 – 4
  3                                   5 - 6

Let us assume that the first person catches rope 1- end 1.
Now he has got 5 more ends left.
The probability of getting end 2 is therefore P(A) = 1/5

Let us now assume that the second person catches rope 2- end 3.
Now he has got 3 more ends left.
The probability of getting end 4 is therefore P(B) = 1/3

Let us assume that the third person catches rope 3- end 5.
Now he has got only 1 more end left.
The probability of getting end 6 is therefore P(C) = 1/1= 1

Therefore the total probability is P = P(A)*P(B)*P(C)= 1/5*1/3*1=1/15

The python code for the same is given below :


tie1=0
attempt1=0
a=0
for rop1 in range(1,7):
    if rop1==1:
        continue
    for rop2 in range(1,6):
        if rop2==1:
            tie1+=1
        attempt1+=1
a+=float(tie1)/attempt1

tie2=0

attempt2=0
b=0
for rop3 in range(1,5):
    if rop3==1:
        continue
    for rop4 in range(1,4):
        if rop4==1:
            tie2+=1
        attempt2+=1
b+=float(tie2)/attempt2

tie3=0

attempt3=0
c=0
for rop5 in range(1,3):
    if rop5==1:
        continue
    for rop6 in range(1,2):
        if rop6==1:
            tie3+=1
        attempt3+=1
c+=float(tie3)/attempt3

print('The probability = ',float(a*b*c))

Answer :

(b) What is the probability that the three people are joined in a single closed chain?

Let us assume that the first person catches rope 1- end 1.
Now he has got 4 out of 5 ends left as he should not choose end 2.
Therefore the probability is P(A) = 4/5

Let us now assume that the second person catches rope 2- end 3.
Now he has got 2 out of 3 ends left as he should not choose end 4.
Therefore the probability is P(B) = 2/3

Now the third person has got only 1 more option left.
Therefore the probability is P(C) = 1/1= 1

Therefore the total probability is P = P(A)*P(B)*P(C)= 4/5*2/3*1=8/15

The python code is given below :

s1=0
tries1=0
a=0
for first1 in range(1,7):
    if first1==1:
        continue
    for first2 in range(2,7):
        if first2==2:
            s1+=1
        tries1+=1
a+=1-float(s1)/tries1

s2=0
tries2=0
b=0
for second1 in range(3,7):
    if second1==3:
        continue
    for second2 in range(4,7):
        if second2==4:
            s2+=1
        tries2+=1
b+=1-float(s2)/tries2

s3=0
tries3=0
c=0
for third1 in range(5,7):
    if third1==1:
        continue
    for third2 in range(6,7):
        if third2==2:
            s3+=1
        tries3+=1
c+=1-s3/tries3
print('The probability = ',float(a*b*c))

Python code to find the probability of drawing a red ball from three boxes

Q. No. 5 :

Box u = 1 contains 1 red ball and 3 black balls. Box u = 2 contains 1 red ball, 1 white ball, and 1 black ball. Box u = 3 contains 1 red ball and 1 black ball.
(a) A box is chosen at random and one ball is drawn. What is the probability that the ball is red?
(b) Given that the ball is red, what are the probabilities that the chosen box was box u = 1, 2, 3?

Answer : 

(a) A box is chosen at random and one ball is drawn. What is the probability that the ball is red?

There are 3 boxes.
The probability of choosing any one box is 1/3.

There are 4 balls in box u=1.
Among these 4 balls 1 ball is red.
The probability of drawing a red ball from box u=1 is 1/4.
Among all the 3 boxes, the probability of drawing a red ball from this box is :
P(A) =1/4*1/3 = 1/12.

There are 3 balls in box u=2.
Among these 3 balls 1 ball is red.
The probability of drawing a red ball from box u=1 is 1/3.
Among all the 3 boxes, the probability of drawing a red ball from this box is :
P(B) =1/3*1/3 = 1/9.

There are 2 balls in box u=3.
Among these 2 balls 1 ball is red.
The probability of drawing a red ball from box u=1 is 1/2.
Among all the 3 boxes, the probability of drawing a red ball from this box is :
P(C) =1/2*1/3 = 1/6.

Now the total probability P = P(A)+P(B)+P(C)
i. e. P = 1/12+1/9+1/6 = 13/36
The python code for the solution of the above problem is given below :

s1=0
tries1=0
s2=0
tries2=0
s3=0
tries3=0
for box in range(1,4):
    for ball in range(1,5):
        if box==1:
            if ball==1:
                s1+=1
        tries1+=1
    for ball in range(1,4):
        if box==2:
            if ball==1:
                s2+=1
        tries2+=1
    for ball in range(1,3):
        if box ==3:
            if ball==1:
                s3+=1
        tries3+=1
print((float(s1)/tries1)+(float(s2)/tries2)+(float(s3)/tries3))
#0.36111111111111105
#>>> 13/36
#0.3611111111111111

Answer :

(b) Given that the ball is red, what are the probabilities that the chosen box was box u = 1, 2, 3?

We know that box u=1 contains 4 balls among which 1 is a red ball.
The probability of drawing a red ball here is P(A)=1/4.

Similarly box u=2 contains 3 balls among which 1 is a red ball.
The probability of drawing a red ball here is P(B)=1/3.

And box u=3 contains 2 balls among which 1 is a red ball.
The probability of drawing a red ball here is P(C)=1/2.


The red ball might have been drawn from box u=1 OR u=2 OR u=3.
OR indicates addition.
Hence total probability is P=P(A)+P(B)+P(C)=1/4+1/3+1/2 = 13/12

Now the probability of drawing a red ball from box u=1 is P(A)/P = 3/13.
The probability of drawing a red ball from box u=2 is P(B)/P = 4/13.
The probability of drawing a red ball from box u=3 is P(C)/P = 6/13
The python code is given below :
box1=0
box2=0
box3=0
tries1=0
tries2=0
tries3=0
for first in range(1,5):
    if first==1:
        box1+=1
    tries1+=1
a=float(box1)/tries1
for second in range(1,4):
    if second==1:
        box2+=1
    tries2+=1
b=float(box2)/tries2
for third in range(1,3):
    if third ==1:
        box3+=1
    tries3+=1
c=float(box3)/tries3
print('The probability of drawing the red ball from box u=1 is = ',float(a)/float(a+b+c))
print('The probability of drawing the red ball from box u=2 is = ',float(b)/float(a+b+c))
print('The probability of drawing the red ball from box u=3 is = ',float(c)/float(a+b+c))


CLICK HERE FOR ANSWER TO Q. No.6

Monday, June 23, 2014

Python code to find the probability of drawing an ace or a heart from a pack of 52 playing cards

Q. No. 4 :

What is the probability of drawing an ace or a heart from a pack of 52 playing cards?

Answer :

We know that the total number of playing cards are 52.
Among these, there are four types.
  1. Spades
  2. Diamonds
  3. Hearts
  4. Clubs
Hence, the number of heart cards are 13 (52/4).
The number of aces (A's) are four...Among which one is of hearts.
Hence, there are three aces which are not hearts.
Our problem is to draw either a heart card or an ace. Hence, we have to draw any one of these 16 (13+3) cards from total 52 cards.
Therefore the probability is P=16/52.
The python code solution for this problem is given below :
s=0
tries=0
for prob in range(1,53):
    if prob < 17:
        s+=1
    tries+=1
print (float(s)/tries)

##Ans:
##>>>
##0.3076923076923077
##>>> s
##16
##>>> tries
##52
##>>>

CLICK HERE FOR ANSWER TO Q. No. 5

Python code to Find the probability of obtaining at least one 6 when 5 dice are thrown

Q. No. 3

Find the probability of obtaining at least one 6 when 5 dice are thrown.

Answer :

The python code is as follows :
s=0
tries=0
for first in range(1,7):
    for second in range(1,7):
        for third in range(1,7):
            for fourth in range(1,7):
                for fifth in range(1,7):
                    if first==6:
                        s+=1
                    elif second==6:
                        s+=1
                    elif third==6:
                        s+=1
                    elif fourth==6:
                        s+=1
                    elif fifth==6:
                        s+=1
                    tries+=1
print(float(s)/tries)

#Ans : 0.5981224279835391
#>>> tries
#7776
#>>> s
#4651 

CLICK HERE FOR ANSWER TO Q. No. 4

Python code for finding the probability of sum of numbers on two dies rolled

Q. No. 2 :

Two dice are rolled. We define s as the sum of the values on the two faces. 
(a) What is the mean (expected value) of s? 
(b) What is the probability that s is even? 

Answer :

(a) 
Mean is the average value. 
Each die can have a value 1 to 6. 
Hence the sum can be 2 to 12.
11
12
13
14
15
16
21
22
23
24
25
.... 
....
....
64
65
66
Hence there are 36 possible combinations.
The total sum can be 252.
Hence the mean is 252/36 = 7

The python code is given below :

n = 0
sum=0
for first in range(1,7):
    for second in range(1,7):
        sum+=int(first)+int(second)
        n+=1
print (int(sum)/n) 

(b)
The answer to this problem is rather simple. Among the total combinations possible, half are odd and half are even (As the numbers are sequentially increasing). Hence, the answer is 1/2 = 0.5
sum=0
n=0
tries=0
for first in range(1,7):
    for second in range(1,7):
        sum=int(first)+int(second)
        if sum%2 ==0 :
            n+=1
        tries+=1
print(int(n)/tries)

CLICK HERE TO FIND THE ANSWER FOR Q. No.3

Implementation of probability and solving problems in Python scripting language

Recently, I came across a probability related worksheet here expected to be solved by students of African Institute for Mathematical Sciences. The were expected to solve the problems using Random function of Python.
Here I would like to show you an attempt of solving those problems by a beginner.

Q. No. 1:

1. Suppose you have a fair six-sided die. Now suppose that you throw the die three times. What is the
probability that you will get:
(a) a 1 on the first throw, a 2 on the second throw, and a 3 on the third throw?
(b) a 1, a 2, and a 3 in any order?
(c) three sixes?

Answer :

(a)
Generally, the probability of obtaining 1 on first through is 1/6 as there are 6 possibilities.
Similarly, the possibility of obtaining 2 on second through and 3 on third through is also 1/3 each.
If P(A) =P(B) = P(C) = 1/6
Then, the total probability P =  P(A) * P(B) * P(C) = 1/6 ³
The python code is shown below :
success = 0
tries = 0
for first in range (0,6) :
    for second in range (0,6) :
        for third in range (0,6):
            if first == 1 and second == 2 and third == 3:
                success+=1
            tries+=1
print (float(success)/tries)


Answer = 0.004629629629629629 i.e. 1/6^3


(b)
a 1, a 2, and a 3 in any order?
Answer :
The number of possibilities of getting 1, 2 and 3 in any order are :
123
132
213
231
312
321
Hence, there are 6 possibilities.
But the total possibilities are 111 to 666. i. e. 6^3
Therefore, the total probability is 6/6^3 = 1/6^2
The python code for this problem is shown below :
success = 0
tries = 0
for first in range(0,6):
    for second in range(0,6):
        for third in range(0,6):
            if first == 1 :
                if second == 2 :
                    if third == 3:
                        success+=1
                elif second == 3:
                    if third == 2 :
                        success+=1
            elif first == 2 :
                if second == 1 :
                    if third == 3:
                        success+=1
                elif second == 3:
                    if third == 1 :
                        success+=1
            elif first == 3 :
                if second == 1 :
                    if third == 2:
                        success+=1
                elif second == 2:
                    if third == 1 :
                        success+=1                               
            tries+=1
print (float(success)/tries)

#Ans : 0.027777777777777776 i.e. 6/6^3 = 1/6^2

(c)
The probability of getting a six on first or second or third die is 1/6
The total probability is the sum of all these probabilities.
If P(A) =P(B) = P(C) = 1/6
Then, the total probability P = P(A) * P(B) * P(C) = 1/6 ³
The python code is shown below :
success = 0
tries = 0
for first in range(1,7):
    for second in range(1,7):
        for third in range(1,7):
            if first == 6 and second == 6 :
                if third == 6 :
                    success+=1
            tries+=1
print ('Success=',success)
print ('tries=',tries)
print ('Probability=',float(success)/tries)

#Ans = 0.004629629629629629 i.e. 1/6^3

CLICK HERE FOR THE ANSWER TO SECOND QUESTION

Receive all updates via Facebook. Just Click the Like Button Below...