15
1
Consider three number sequences, A
, B
and C
:
A
: A sequence based on recurrence relations,f(n) = f(n-1)+f(n-2)
, starting withf(1) = 3, f(2) = 4
. So, the sequence begins like this:3 4 7 11 18 29 47 76 ...
B
: The composite numbers, that is all integers that aren't primes (or 1):4 6 8 9 10 12 14 15 16 ...
C
: The digits of Pi:3 1 4 1 5 9 2 6 5 ...
Given a positive integer N < 50
, either as function argument or STDIN, return the decimal value of the fraction A(N)/B(N)
with C(N)
digits after the decimal point. Normal rules for rounding applies (round up if the N+1'th digit is 5 or higher). If the Nth digit of pi
is zero, an integer should be printed. scientific notation / Standard form is accepted for numbers higher than 1000.
This is code golf, so the shortest answer in bytes wins.
Some examples:
N = 1: 0.750
N = 2: 0.7
N = 3: 0.8750
N = 4: 1.2
N = 6: 2.416666667
N = 10: 11.056
N = 20: 764.8750
Of course, standard code golf rules applies.
The function must terminate in less than two minutes on any modern laptop.
When you say
C(n)
digits, do we have to include trailing 0's? – Maltysen – 2015-09-03T19:19:45.603To which input does the time limit apply? – Dennis – 2015-09-03T19:33:56.640
@Dennis, do you mean to which
N
? If so, up to N = 49. Or something else? – Stewie Griffin – 2015-09-03T19:36:32.520JavaScript has a limited floating point accuracy of 16. Past that you'll start getting inaccurate results. Is this okay? – Downgoat – 2015-09-03T22:08:30.487
1@vihan My solution (unpublished ATM) stores the first 49 digits of pi in a string. And you don't need any more than 9 digits of accuracy in the result, if you're worried about that. – ETHproductions – 2015-09-03T23:26:41.197
@vihan, I think ETHproductions answered it well. You only need to output 9 digits after the decimal points. Those 9 should be exact. There are algorithms for calculating only the N'th digit of pi (I'll admit: I don't know how big accuracy you need to calculate the 49th). You can also follow ETHprocuctions tips. – Stewie Griffin – 2015-09-04T06:19:24.653