Ludic numbers: Difference between revisions

m (→‎{{header|Pascal}}: Fix comment: Perl 6 --> Raku)
Line 3,168:
 
Output is the same as for the fast version.
 
===Python: lazy streaming generator===
The following streaming version of function ludic also returns ludic numbers until reaching system limits.
Instead of creating a bounded table and deleting elements, it uses the insight that after each iteration the <b>remaining</b> numbers are shuffled left, modifying their indices in a regular way. Reversing this process tracks the k'th ludic number in the final list back to its position in the initial list of integers, and hence determines its value without any need to build the table. The only storage requirement is for the list of numbers found so far.
<br>Based on the similar algorithm for lucky numbers at https://oeis.org/A000959/a000959.txt.
<br>Function triplets wraps ludic and uses a similar stream-filtering approach to find triplets.
<lang python>def ludic():
yield 1
ludics = []
while True:
k = 0
for j in reversed(ludics):
k = (k*j)//(j-1) + 1
ludics.append(k+2)
yield k+2
def triplets():
a, b, c, d = 0, 0, 0, 0
for k in ludic():
if k-4 in (b, c, d) and k-6 in (a, b, c):
yield k-6, k-4, k
a, b, c, d = b, c, d, k
 
first_25 = [k for i, k in zip(range(25), gen_ludic())]
print(f'First 25 ludic numbers: {first_25}')
count = 0
for k in gen_ludic():
if k > 1000:
break
count += 1
print(f'Number of ludic numbers <= 1000: {count}')
it = iter(gen_ludic())
for i in range(1999):
next(it)
ludic2000 = [next(it) for i in range(6)]
print(f'Ludic numbers 2000..2005: {ludic2000}')
print('Ludic triplets < 250:')
for a, b, c in triplets():
if c >= 250:
break
print(f'[{a}, {b}, {c}]')
</lang>
{{out}}
<pre>First 25 ludic numbers: [1, 2, 3, 5, 7, 11, 13, 17, 23, 25, 29, 37, 41, 43, 47, 53, 61, 67, 71, 77, 83, 89, 91, 97, 107]
Number of ludic numbers <= 1000: 142
Ludic numbers 2000..2005: [21475, 21481, 21487, 21493, 21503, 21511]
Ludic triplets < 250:
[1, 3, 7]
[5, 7, 11]
[11, 13, 17]
[23, 25, 29]
[41, 43, 47]
[173, 175, 179]
[221, 223, 227]
[233, 235, 239]
</pre>
 
=={{header|Racket}}==
2

edits