Klarner-Rado sequence
sub Klarner-Rado ($n) {
my @klarner-rado = 1;
my @next = x2, x3;
loop {
@klarner-rado.push: my $min = @next.min;
@next[0] = x2 if @next[0] == $min;
@next[1] = x3 if @next[1] == $min;
last if +@klarner-rado > $n.max;
}
sub x2 { state $i = 0; @klarner-rado[$i++] × 2 + 1 }
sub x3 { state $i = 0; @klarner-rado[$i++] × 3 + 1 }
@klarner-rado[|$n]
}
use Lingua::EN::Numbers;
put "First 100 elements of Klarner-Rado sequence:";
put Klarner-Rado(^100).batch(10)».fmt("%3g").join: "\n";
put '';
put (($_».Int».&ordinal».tc »~» ' element: ').List Z~ |(List.new: (Klarner-Rado ($_ »-» 1))».&comma)
given $(1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6)).join: "\n";
Output:
First 100 elements of Klarner-Rado sequence:
1 3 4 7 9 10 13 15 19 21
22 27 28 31 39 40 43 45 46 55
57 58 63 64 67 79 81 82 85 87
91 93 94 111 115 117 118 121 127 129
130 135 136 139 159 163 165 166 171 172
175 183 187 189 190 193 202 223 231 235
237 238 243 244 247 255 256 259 261 262
271 273 274 279 280 283 319 327 331 333
334 343 345 346 351 352 355 364 367 375
379 381 382 387 388 391 405 406 409 418
First element: 1
Tenth element: 21
One hundredth element: 418
One thousandth element: 8,487
Ten thousandth element: 157,653
One hundred thousandth element: 2,911,581
One millionth element: 54,381,285
Last updated