Klarner-Rado sequence: Difference between revisions
Content added Content deleted
m (Removed extraneous character) |
(Add C# implementation) |
||
Line 522: | Line 522: | ||
54381285 |
54381285 |
||
1031926801</pre> |
1031926801</pre> |
||
=={{header|C#}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="C#"> |
|||
using System; |
|||
class KlarnerRadoSequence { |
|||
static void Main(string[] args) { |
|||
const int limit = 1_000_000; |
|||
int[] klarnerRado = InitialiseKlarnerRadoSequence(limit); |
|||
Console.WriteLine("The first 100 elements of the Klarner-Rado sequence:"); |
|||
for (int i = 1; i <= 100; i++) { |
|||
Console.Write($"{klarnerRado[i],3}{(i % 10 == 0 ? "\n" : " ")}"); |
|||
} |
|||
Console.WriteLine(); |
|||
int index = 1_000; |
|||
while (index <= limit) { |
|||
Console.WriteLine($"The {index}th element of Klarner-Rado sequence is {klarnerRado[index]}"); |
|||
index *= 10; |
|||
} |
|||
} |
|||
private static int[] InitialiseKlarnerRadoSequence(int limit) { |
|||
int[] result = new int[limit + 1]; |
|||
int i2 = 1, i3 = 1; |
|||
int m2 = 1, m3 = 1; |
|||
for (int i = 1; i <= limit; i++) { |
|||
int minimum = Math.Min(m2, m3); |
|||
result[i] = minimum; |
|||
if (m2 == minimum) { |
|||
m2 = result[i2] * 2 + 1; |
|||
i2 += 1; |
|||
} |
|||
if (m3 == minimum) { |
|||
m3 = result[i3] * 3 + 1; |
|||
i3 += 1; |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 100 elements of the 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 |
|||
The 1000th element of Klarner-Rado sequence is 8487 |
|||
The 10000th element of Klarner-Rado sequence is 157653 |
|||
The 100000th element of Klarner-Rado sequence is 2911581 |
|||
The 1000000th element of Klarner-Rado sequence is 54381285 |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |