Question
Hello, on the Hackerrank coding challenges there was one called Minimal Distance to Pi. it is classified as hard. I was wondering how I can code it in Python? thank you

9:14イ Done 1 of 30 Minimal Distance to Pi ☆ Points: 1262.07 Rank: 605 Problem Submissions Leaderboard Ed Given two long integ
9:14イ Done 1 of 30 Input Format Two space-separated long integers describing the respective values of min and maz Constraints
9:14イ 1 of 30 Done Explanation 0 We must check all fractions with denominators from min = 1 to maz = 10; Ford = 1:클 π 을, the
9:14イ Done 1 of 30 Minimal Distance to Pi ☆ Points: 1262.07 Rank: 605 Problem Submissions Leaderboard Ed Given two long integers, min and maz. find and print a common fraction,. such that min ^ d S maz and T is minimal (recall that 3. 1415926535 8979323846 2643383279 5028841971 693993751 ). If there are several fractions having minimal distance to π, choose the one with the smallest denominator Input Format Two space-separated long integers describing the respective values of min and maz. oO Constraints 1minS maz 3 105 Output Format
9:14イ Done 1 of 30 Input Format Two space-separated long integers describing the respective values of min and maz Constraints .1 min
0 0
Add a comment Improve this question Transcribed image text
Answer #1

from fractions import Fraction def main): #importing module MM-input a,b mn.split() a-int(a) b-int(b) pie=3.14159265358979323

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()

jack@jack-TravelMate-P243-M:~$ python3 mpie.py 1 10 22/7 jack@jack-TravelMate-P243-M:~$ python3 mpie.py 10 100 311/99 |jack@j

You may need to edit the code as per test cases.

If you have any doubts comment me.

Add a comment
Answer #2

where is main?


Add a comment
Know the answer?
Add Answer to:
Hello, on the Hackerrank coding challenges there was one called Minimal Distance to Pi. it is classified as hard. I was...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT