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, whiledecimal
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:
Created | Aug 21, 2018 |
Published | Aug 21, 2018 |
Warriors Trained | 150 |
Total Skips | 45 |
Total Code Submissions | 148 |
Total Times Completed | 16 |
Python Completions | 16 |
Total Stars | 8 |
% of votes with a positive feedback rating | 93% of 7 |
Total "Very Satisfied" Votes | 6 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 6 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 2 kyu |
Lowest Assessed Rank | 6 kyu |