{(2,{1+[*] @_}...*)[$_]}
{(2,{1+.²-$_}...*)[$_]}
Explanation
# bare block with implicit parameter 「$_」 { ( # You can replace 2 with 1 here # so that it uses 1 based indexing # rather than 0 based 2, # bare block with implicit parameter 「@_」 { 1 + # reduce the input of this inner block with 「&infix:<*>」 # 「&infix:<*>」( the input is all of them generated when using a slurpy @ var ) [*] @_ # that is the same as: # 「@_.reduce: &infix:<*>」 } # keep calling that to generate more values until: ... # forever * # get the value as indexed by the input )[ $_ ] }
Usage:
my &code = {(2,{1+[*] @_}...*)[$_]} say code 0; # 2 say code 1; # 3 say code 2; # 7 say code 3; # 43 say code 4; # 1807 # you can even give it a range say code 4..7; # (1807 3263443 10650056950807 113423713055421844361000443) say code 8; # 12864938683278671740537145998360961546653259485195807 say code 9; # 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443 say code 10; # 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920807 my $start = now; # how many digits are there in the 20th value say chars code 20; # 213441 my $finish = now; # how long did it take to generate the values up to 20 say $finish - $start, ' seconds'; # 49.7069076 seconds