Create a function randpi that will continuously generate random digits until pro
ID: 3838485 • Letter: C
Question
Create a function randpi that will continuously generate random digits until producing a sequence which reproduces up to n significant figures, where n is the input argument to the function. The function should print the total number of random digits that were generated in the process.
For example, if the call to the function is
>>randpi(3)
digits are to be randomly generated until the sequence 3 1 4 is produced, which corresponds to with 3 significant figures, i.e., 3.14.
For sake of illustration, shown is a stream of randomly generated digits terminating at 3 1 4:
62942241613494624558375764618056730468499330976020373824054965418834695314
In this particular case, 74 digits were generated prior to reaching the desired sequence. The number 74 would then be output on the terminal.
Explanation / Answer
#package
import random
#phi value
phi="314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201"
#randpi function
def randpi(no):
#variables
index=0
result=0
count=0
result_pi=phi[:no]
#while loop
while True:
#generating random degit
degit=random.randint(0,9)
print degit
#if degit eqals to proper degit in phi
if(str(degit)==result_pi[index]):
count+=1
index+=1
#no genereated
if(count==no):
#returning result
return result
#mismatch occur
else:
count=0
index=0
result+=1
result=randpi(3)
print "Total no of degits generated:",
print result
4
4
3
3
0
3
5
1
2
2
7
4
2
8
7
6
9
1
1
3
9
4
8
5
0
7
8
1
4
2
9
5
4
1
5
6
8
3
8
8
6
1
2
5
6
2
8
0
1
4
1
0
9
1
5
0
9
4
4
1
9
2
9
8
9
7
4
8
4
9
2
5
4
4
4
4
3
4
4
7
1
9
9
3
8
2
3
6
8
7
6
0
3
2
6
9
4
1
9
5
1
6
8
0
0
6
0
4
6
7
8
8
1
4
5
3
6
7
4
0
0
9
4
8
8
0
0
5
6
4
5
6
8
4
6
5
6
3
7
3
9
0
0
3
5
7
6
6
8
9
3
0
9
4
4
9
6
9
9
6
0
9
8
0
3
9
9
9
7
0
0
1
7
2
9
3
9
6
8
5
0
3
8
0
6
1
9
9
8
3
3
0
7
3
1
9
4
8
8
3
1
4
Total no of degits generated:201
#package
import random
#phi value
phi="314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201"
#randpi function
def randpi(no):
#variables
index=0
result=0
count=0
result_pi=phi[:no]
#while loop
while True:
#generating random degit
degit=random.randint(0,9)
print degit
#if degit eqals to proper degit in phi
if(str(degit)==result_pi[index]):
count+=1
index+=1
#no genereated
if(count==no):
#returning result
return result
#mismatch occur
else:
count=0
index=0
result+=1
result=randpi(3)
print "Total no of degits generated:",
print result
4
4
3
3
0
3
5
1
2
2
7
4
2
8
7
6
9
1
1
3
9
4
8
5
0
7
8
1
4
2
9
5
4
1
5
6
8
3
8
8
6
1
2
5
6
2
8
0
1
4
1
0
9
1
5
0
9
4
4
1
9
2
9
8
9
7
4
8
4
9
2
5
4
4
4
4
3
4
4
7
1
9
9
3
8
2
3
6
8
7
6
0
3
2
6
9
4
1
9
5
1
6
8
0
0
6
0
4
6
7
8
8
1
4
5
3
6
7
4
0
0
9
4
8
8
0
0
5
6
4
5
6
8
4
6
5
6
3
7
3
9
0
0
3
5
7
6
6
8
9
3
0
9
4
4
9
6
9
9
6
0
9
8
0
3
9
9
9
7
0
0
1
7
2
9
3
9
6
8
5
0
3
8
0
6
1
9
9
8
3
3
0
7
3
1
9
4
8
8
3
1
4
Total no of degits generated:201
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.