Beta

Pi approximation fractions

Description:

Description

Have you heard about Archimedean approximation? It's a nice way to approximate π number by a simple fraction 22/7. You can see that 22/7 covers 2 decimal points of π:

π:    3.14_15926535897932
22/7: 3.14_2857142857143

In ancient China common π approximation was 355/113, that covers 6 decimal points:

π:       3.141592_6535897932
355/113: 3.141592_9203539825

Task

Your goal is to write a function get_fraction that accepts eps as it's argument. The function returns tuple of fraction's numerator and denominator (n, d), so that |n/d - π| < eps. Basically, you need to write a function that finds two smallest numbers, which fraction is closest to π with given precision.

Examples

get_fraction(1)      #=> (3, 1)
get_fraction(0.1)    #=> (22, 7)
get_fraction(0.001)  #=> (333, 106)
get_fraction(1e-5)   #=> (355, 113)

Limitations

eps won't be bigger that 1 and smaller than 10 ^ -110:

1e-110 <= eps <= 1

Notes

  • For Python users: module fractions is forbidden, while decimal precision is changed to 150 points
  • Tests precision may exceed your language's standart π limit, so you have preloaded Pi (for Python) constant that is 110 digit pi decimal
  • First 110 digits of π is: 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651
  • Hint: If you're stuck, Continued fraction may help
Mathematics
Algorithms

Similar Kata:

Stats:

CreatedAug 21, 2018
PublishedAug 21, 2018
Warriors Trained150
Total Skips45
Total Code Submissions148
Total Times Completed16
Python Completions16
Total Stars8
% of votes with a positive feedback rating93% of 7
Total "Very Satisfied" Votes6
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments6
Average Assessed Rank
5 kyu
Highest Assessed Rank
2 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • polluxCast0r Avatar
Ad