Exponentials as fractions
Description:
The aim is to calculate exponential(x)
(written exp(x)
in most math libraries) as an irreducible fraction, the numerator of this fraction having a given number of digits.
We call this function expand
, it takes two parameters, x
of which we want to evaluate the exponential, digits
which is the required number of digits for the numerator.
The function expand
will return an array of the form [numerator, denominator]
; we stop the loop in the Taylor expansion (see references below) when the numerator has a number of digits >=
the required number of digits
Examples:
expand(1, 2) --> 65/24 (we will write this [65, 24] or (65, 24) in Haskell;
65/24 ~ 2.708...)
expand(2, 5) --> [20947, 2835]
expand(3, 10) --> [7205850259, 358758400]
expand(1.5, 10) --> [36185315027,8074035200]
Note
expand(1,5) = [109601, 40320]
is the same as expand(1, 6)
Method:
As said above the way here is to use Taylor expansion
of the exponential function though it is not always the best approximation by a rational.
References:
https://en.wikipedia.org/wiki/Exponential_function#Formal_definition
Similar Kata:
Stats:
Created | Mar 3, 2015 |
Published | Mar 3, 2015 |
Warriors Trained | 3740 |
Total Skips | 1619 |
Total Code Submissions | 10923 |
Total Times Completed | 528 |
Ruby Completions | 51 |
Python Completions | 347 |
Haskell Completions | 63 |
Clojure Completions | 42 |
R Completions | 45 |
Total Stars | 90 |
% of votes with a positive feedback rating | 71% of 121 |
Total "Very Satisfied" Votes | 70 |
Total "Somewhat Satisfied" Votes | 32 |
Total "Not Satisfied" Votes | 19 |
Total Rank Assessments | 9 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |