sed -r
, 13193 131 136 bytes
s/1+/&&&&&&&&&&/
s/$/;,/
:d
s/,.+/,/
:
s/^(1+)(1*;\1;1*;.*1*;\1;1*,)1{10}?/\21/
t
s/^1*1*/&&&&&&&&&&/
ta
:a
s/,1{10}/1,/
ta
s/^(1*;1*;)1/\1,/
td
s/.+,//
Takes input in unary and outputs in unary, and the program is 01-indexed (0 is shown in unary by an empty string). Thankfully, this challenge has already prepared me for this challengeone.
The concept is similar, both implement long division. Here, I perform long division x
times, where x
is the digit after the decimal place that I have to find. After each iteration, I discard the previous decimal places because they are no longer needed.
While doing division, the program is in the format dividend;divisor;x;dividend;divisor;x,result
where .
s/$/,/
adds this comma, the comma is required to separate the "decimal place".result from everything else
###Explanation (more coming soon)Then follows the main program loop
Multiply the dividend by 10. Now the program can start at the decimal point so that it's easier to calculate the xth number after the decimal place. Then append the "decimal point", which is in reality a comma instead to save bytes.:d
label d
s/1+/&&&&&&&&&&/
s/$/;,/
s/,.+/,/
remove everything after the comma:
empty label-
s/(1+)(1*;\1;1*,)1{10}?/\21/
perform division, adding 1 to the result each iteration, whilst simultaneously removing blocks of 10 continuous 1s in the result
t
branch to the empty label, in other words, loop until the dividend has been exhausteds/1*/&&&&&&&&&&/
multiply the dividend by 10 to prepare for the next iterationta
branch to label a:a
label a, this line and the line above are required to maketd
works/1,/,/
subtract 1 from x
Thentd
conditional branch to d, this is triggered if there has been a successful substitution since the last conditional branch, since :ds/1*/&&&&&&&&&&/
denotes the step whereis always successful, td
will always get triggered, but by introducing branch a, we dofix that so that it only depends on the division.previous substitution
We want to delete the digit afters/.+,//
finally, remove everything but the decimal place after each iteration.result
s/,.+/,/