from fractions import Fraction #importing module
def main():
mm=input() #taking input
a,b=mm.split() #splitting string
a=int(a) #making charactera as integer
b=int(b)
pie=3.1415926535897932384626433832795028841971693993751
mn=[] #list to store numbers closest to pi value
c=0.0
d=0.0
for i in range(a,b+1,1): #iterates each number from
min to max
for j in range(i,1000000):
#iterates from denominator to 1000000
if
((j/i)<=pie): #if value of numerator/denominator is less than or
equal to pie
c=j/i
else: #if
not
d=j/i
break #breaks the loop
if (abs(pie-c) < d-pie): #checks
which value is closest to pie
mn.append(c)
#adds to list
else:
mn.append(d)
mx=0.0
for i in mn: #finds the closest number
if
(abs(pie-i)<abs(pie-mx)):
mx=i
print(Fraction(mx).limit_denominator()) #converts
decimal to fraction
main()
You may need to edit the code as per test cases.
If you have any doubts comment me.
Hello, on the Hackerrank coding challenges there was one called Minimal Distance to Pi. it is classified as hard. I was...