Pi is an irrational number, which means that its decimal representation never terminates or repeats itself.
Pi truncated to 41 decimal digits (40 places) is 3.1415926535897932384626433832795028841971
.
If we ignore the decimal point and list the digits as a sequence of positive integers, avoiding duplicates, we get 3 1 4 15 9 2 6 5 35 8 97 93 23 84 62 64 33 83 27 950 28 841 971
(OEIS A064809).
(Notice that 15
appears in the sequence instead of 1 5
because 1
had already occurred.
Also note that 0
does not occur because it is not positive; 950
contains the first zero.)
To construct the first pirrational number we use this sequence to index into the digits of Pi (the first digit being 3, the second 1, etc.).
So the first digit of the first pirrational number is the 3rd digit of Pi,
the second digit is the 1st digit of Pi,
the third digit is the 4th digit of Pi,
the fourth is the 15th digit of Pi,
and so on.
A decimal point is added after the first digit to mimic Pi.
Thus the first pirrational number to 41 digits is 4.3195195867462520687356193644029372991880
.
(Note that for the 30th digit I had to go all the way to the 974th digit of Pi.)
To construct the second pirrational number the process is repeated using the first pirrational number instead of Pi. (Pi itself may be called the zeroth pirrational number.) So the new sequence is 4 3 1 9 5 19 58 ...
and the first piirational number is indexed to produce the second, which starts 9.14858...
.
Further pirrational numbers are created in the same way, each being generated from the one before.
Challenge
Your task is to write the shortest program possible that takes in two integers, N
and D
, and outputs the N
th pirrational number truncated to D
decimal digits.
D
is always positive but N
is non-negative, and D
digits of Pi should be output when N
is 0.
When D
is 1 it does not matter if the decimal point is present or not.
The input should come from stdin or the command line and output should go to stdout (or your language's closest alternatives).
Your program should work for all input values of N
and D
below 216, but it needn't be timely or efficient.
The shortest code in bytes wins.
(Note that pirrational numbers exits in other bases but everything in this challenge is done in base 10.)
N=1
,D=13393
for example, you'd need the 31 millionth digit of PI \$\endgroup\$