Klarner-Rado sequence
Klarner-Rado sequences are a class of similar sequences that were studied by the mathematicians David Klarner and Richard Rado.
The most well known is defined as the thinnest strictly ascending sequence K which starts 1, then, for each element n, it will also contain somewhere in the sequence, 2 × n + 1 and 3 × n + 1.
So, the sequence K starts with 1.
Set n equal to the first element 1; the sequence will also contain 2 × n + 1 and 3 × n + 1, or 3 and 4.
Set n equal to the next element: 3, somewhere in the sequence it will contain 2 × n + 1 and 3 × n + 1, or 7 and 10.
Continue setting n equal to each element in turn to add to the sequence.
- Task
- Find and display the first one hundred elements of the sequence.
- Find and display the one thousandth and ten thousandth elements of the sequence.
Preferably without needing to find an over abundance and sorting.
- Stretch
- Find and display the one hundred thousandth and one millionth elements of the sequence.
- See also
Raku
<lang perl6>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 $(1e3, 1e4, 1e5, 1e6)).join: "\n";</lang>
- 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 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