Ludic numbers: Difference between revisions

91,973 bytes added ,  1 month ago
Added Easylang
(Added Elixir)
(Added Easylang)
 
(89 intermediate revisions by 47 users not shown)
Line 1:
{{task|Sieves}}
[[Category:Prime Numbers]]
[https://oeis.org/wiki/Ludic_numbers Ludic numbers] are related to prime numbers as they are generated by a sieve quite like the [[Sieve of Eratosthenes]] is used to generate prime numbers.
 
[https://oeis.org/wiki/Ludic_numbers Ludic numbers]   are related to prime numbers as they are generated by a sieve quite like the [[Sieve of Eratosthenes]] is used to generate prime numbers.
The first ludic number is <span style="color:blue;font-weight:bold">1</span>.
 
<br>To generate succeeding ludic numbers create an array of increasing integers starting from 2
The first ludic number is &nbsp; <span style="color:blue;font-weight:bold">1</span>.
 
To generate succeeding ludic numbers create an array of increasing integers starting from &nbsp; <span style="color:blue;font-weight:bold">2</span>.
:<code><span style="color:blue;font-weight:bold">2</span> 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...</code>
(Loop)
* Take the first member of the resultant array as the next Ludicludic number &nbsp; <span style="color:blue;font-weight:bold">2</span>.
* Remove every &nbsp; '''2'<sup>nd</sup>''' &nbsp; indexed item from the array (including the first).
::<code><span style="color:blue;font-weight:bold"><s>2</s></span> 3 <s>4</s> 5 <s>6</s> 7 <s>8</s> 9 <s>10</s> 11 <s>12</s> 13 <s>14</s> 15 <s>16</s> 17 <s>18</s> 19 <s>20</s> 21 <s>22</s> 23 <s>24</s> 25 <s>26</s> ...</code>
* (Unrolling a few loops...)
* Take the first member of the resultant array as the next Ludicludic number &nbsp; <span style="color:blue;font-weight:bold">3</span>.
* Remove every &nbsp; '''3'<sup>rd</sup>''' &nbsp; indexed item from the array (including the first).
::<code><span style="color:blue;font-weight:bold"><s>3</s></span> 5 7 <s>9</s> 11 13 <s>15</s> 17 19 <s>21</s> 23 25 <s>27</s> 29 31 <s>33</s> 35 37 <s>39</s> 41 43 <s>45</s> 47 49 <s>51</s> ...</code>
* Take the first member of the resultant array as the next Ludicludic number &nbsp; <span style="color:blue;font-weight:bold">5</span>.
* Remove every &nbsp; '''5'<sup>th</sup>''' &nbsp; indexed item from the array (including the first).
::<code><span style="color:blue;font-weight:bold"><s>5</s></span> 7 11 13 17 <s>19</s> 23 25 29 31 <s>35</s> 37 41 43 47 <s>49</s> 53 55 59 61 <s>65</s> 67 71 73 77 ...</code>
* Take the first member of the resultant array as the next Ludicludic number &nbsp; <span style="color:blue;font-weight:bold">7</span>.
* Remove every &nbsp; '''7'<sup>th</sup>''' &nbsp; indexed item from the array (including the first).
::<code><span style="color:blue;font-weight:bold"><s>7</s></span> 11 13 17 23 25 29 <s>31</s> 37 41 43 47 53 55 <s>59</s> 61 67 71 73 77 83 <s>85</s> 89 91 97 ...</code>
* <big><b> ... </b></big>
* ...
* Take the first member of the current array as the next Ludicludic number &nbsp; <span style="color:blue;font-weight:bold">L</span>.
* Remove every &nbsp; '''L'<sup>th</sup>''' &nbsp; indexed item from the array (including the first).
* <big><b> ... </b></big>
* ...
 
 
 
;Task:
* Generate and show here the first 25 ludic numbers.
* How many ludic numbers are there less than or equal to 1000?
* Show the 2000..2005'<sup>th</sup> ludic numbers.
 
* A triplet is any three numbers <math>x,</math> <math>x+2,</math> <math>x+6</math> where all three numbers are also ludic numbers. Show all triplets of ludic numbers < 250 (Stretch goal)
 
<br>
;Stretch goal:
Show all triplets of ludic numbers < 250.
* A triplet is any three numbers &nbsp; &nbsp; <big><math>x,</math> &nbsp; <math>x+2,</math> &nbsp; <math>x+6</math> </big> &nbsp; &nbsp; where all three numbers are also ludic numbers.
 
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F ludic(nmax = 100000)
V r = [1]
V lst = Array(2..nmax)
L !lst.empty
r.append(lst[0])
[Int] newlst
V step = lst[0]
L(i) 0 .< lst.len
I i % step != 0
newlst.append(lst[i])
lst = newlst
R r
 
V ludics = ludic()
print(‘First 25 ludic primes:’)
print(ludics[0.<25])
print("\nThere are #. ludic numbers <= 1000".format(sum(ludics.filter(l -> l <= 1000).map(l -> 1))))
print("\n2000'th..2005'th ludic primes:")
print(ludics[2000 - 1 .. 2004])
V n = 250
V triplets = ludics.filter(x -> x + 6 < :n &
x + 2 C :ludics &
x + 6 C :ludics).map(x -> (x, x + 2, x + 6))
print("\nThere are #. triplets less than #.:\n #.".format(triplets.len, n, triplets))</syntaxhighlight>
 
{{out}}
<pre>
First 25 ludic primes:
[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]
 
There are 142 ludic numbers <= 1000
 
2000'th..2005'th ludic primes:
[21475, 21481, 21487, 21493, 21503, 21511]
 
There are 8 triplets less than 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|360 Assembly}}==
{{trans|Fortran}}
<syntaxhighlight lang="360asm">* Ludic numbers 23/04/2016
LUDICN CSECT
USING LUDICN,R15 set base register
LH R9,NMAX r9=nmax
SRA R9,1 r9=nmax/2
LA R6,2 i=2
LOOPI1 CR R6,R9 do i=2 to nmax/2
BH ELOOPI1
LA R1,LUDIC-1(R6) @ludic(i)
CLI 0(R1),X'01' if ludic(i)
BNE ELOOPJ1
SR R8,R8 n=0
LA R7,1(R6) j=i+1
LOOPJ1 CH R7,NMAX do j=i+1 to nmax
BH ELOOPJ1
LA R1,LUDIC-1(R7) @ludic(j)
CLI 0(R1),X'01' if ludic(j)
BNE NOTJ1
LA R8,1(R8) n=n+1
NOTJ1 CR R8,R6 if n=i
BNE NDIFI
LA R1,LUDIC-1(R7) @ludic(j)
MVI 0(R1),X'00' ludic(j)=false
SR R8,R8 n=0
NDIFI LA R7,1(R7) j=j+1
B LOOPJ1
ELOOPJ1 LA R6,1(R6) i=i+1
B LOOPI1
ELOOPI1 XPRNT =C'First 25 ludic numbers:',23
LA R10,BUF @buf=0
SR R8,R8 n=0
LA R6,1 i=1
LOOPI2 CH R6,NMAX do i=1 to nmax
BH ELOOPI2
LA R1,LUDIC-1(R6) @ludic(i)
CLI 0(R1),X'01' if ludic(i)
BNE NOTI2
XDECO R6,XDEC i
MVC 0(4,R10),XDEC+8 output i
LA R10,4(R10) @buf=@buf+4
LA R8,1(R8) n=n+1
LR R2,R8 n
SRDA R2,32
D R2,=F'5' r2=mod(n,5)
LTR R2,R2 if mod(n,5)=0
BNZ NOTI2
XPRNT BUF,20
LA R10,BUF @buf=0
NOTI2 EQU *
CH R8,=H'25' if n=25
BE ELOOPI2
LA R6,1(R6) i=i+1
B LOOPI2
ELOOPI2 MVC BUF(25),=C'Ludic numbers below 1000:'
SR R8,R8 n=0
LA R6,1 i=1
LOOPI3 CH R6,=H'999' do i=1 to 999
BH ELOOPI3
LA R1,LUDIC-1(R6) @ludic(i)
CLI 0(R1),X'01' if ludic(i)
BNE NOTI3
LA R8,1(R8) n=n+1
NOTI3 LA R6,1(R6) i=i+1
B LOOPI3
ELOOPI3 XDECO R8,XDEC edit n
MVC BUF+25(6),XDEC+6 output n
XPRNT BUF,31 print buffer
MVC BUF(80),=CL80'Ludic numbers 2000 to 2005:'
LA R10,BUF+28 @buf=28
SR R8,R8 n=0
LA R6,1 i=1
LOOPI4 CH R6,NMAX do i=1 to nmax
BH ELOOPI4
LA R1,LUDIC-1(R6) @ludic(i)
CLI 0(R1),X'01' if ludic(i)
BNE NOTI4
LA R8,1(R8) n=n+1
CH R8,=H'2000' if n>=2000
BL NOTI4
XDECO R6,XDEC edit i
MVC 0(6,R10),XDEC+6 output i
LA R10,6(R10) @buf=@buf+6
CH R8,=H'2005' if n=2005
BE ELOOPI4
NOTI4 LA R6,1(R6) i=i+1
B LOOPI4
ELOOPI4 XPRNT BUF,80 print buffer
XPRNT =C'Ludic triplets below 250:',25
LA R6,1 i=1
LOOPI5 CH R6,=H'243' do i=1 to 243
BH ELOOPI5
LA R1,LUDIC-1(R6) @ludic(i)
CLI 0(R1),X'01' if ludic(i)
BNE ITERI5
LA R1,LUDIC+1(R6) @ludic(i+2)
CLI 0(R1),X'01' if ludic(i+2)
BNE ITERI5
LA R1,LUDIC+5(R6) @ludic(i+6)
CLI 0(R1),X'01' if ludic(i+6)
BNE ITERI5
MVC BUF+0(1),=C'[' [
XDECO R6,XDEC edit i
MVC BUF+1(4),XDEC+8 output i
LA R2,2(R6) i+2
XDECO R2,XDEC edit i+2
MVC BUF+5(4),XDEC+8 output i+2
LA R2,6(R6) i+6
XDECO R2,XDEC edit i+6
MVC BUF+9(4),XDEC+8 output i+6
MVC BUF+13(1),=C']' ]
XPRNT BUF,14 print buffer
ITERI5 LA R6,1(R6) i=i+1
B LOOPI5
ELOOPI5 XR R15,R15 set return code
BR R14 return to caller
LTORG
BUF DS CL80 buffer
XDEC DS CL12 decimal editor
NMAX DC H'25000' nmax
LUDIC DC 25000X'01' ludic(nmax)=true
YREGS
END LUDICN</syntaxhighlight>
{{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
Ludic numbers below 1000: 142
Ludic numbers 2000 to 2005: 21475 21481 21487 21493 21503 21511
Ludic triplets below 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|ABAP}}==
Works with NW 7.40 SP8
<langsyntaxhighlight ABAPlang="abap">CLASS lcl_ludic DEFINITION CREATE PUBLIC.
 
PUBLIC SECTION.
Line 118 ⟶ 317:
ENDMETHOD.
 
ENDCLASS.</langsyntaxhighlight>
 
{{Output}}
Line 170 ⟶ 369:
233 235 239
</pre>
 
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<syntaxhighlight lang="action!">DEFINE NOTLUDIC="0"
DEFINE LUDIC="1"
DEFINE UNKNOWN="2"
 
PROC LudicSieve(BYTE ARRAY a INT count)
INT i,j,k
 
SetBlock(a,count,UNKNOWN)
a(0)=NOTLUDIC
a(1)=LUDIC
 
i=2
WHILE i<count
DO
IF a(i)=UNKNOWN THEN
a(i)=LUDIC
j=i k=0
WHILE j<count
DO
IF a(j)=UNKNOWN THEN
k==+1
IF k=i THEN
a(j)=NOTLUDIC
k=0
FI
FI
j==+1
OD
FI
i==+1
Poke(77,0) ;turn off the attract mode
OD
RETURN
 
PROC PrintLudicNumbers(BYTE ARRAY a INT count,first,last)
INT i,j
 
i=1 j=0
WHILE i<count AND j<=last
DO
IF a(i)=LUDIC THEN
IF j>=first THEN
PrintI(i) Put(32)
FI
j==+1
FI
i==+1
OD
PutE() PutE()
RETURN
 
INT FUNC CountLudicNumbers(BYTE ARRAY a INT max)
INT i,res
 
res=0
FOR i=1 TO max
DO
IF a(i)=LUDIC THEN
res==+1
FI
OD
RETURN (res)
 
PROC PrintLudicTriplets(BYTE ARRAY a INT max)
INT i,j
 
j=0
FOR i=0 TO max-6
DO
IF a(i)=LUDIC AND a(i+2)=LUDIC AND a(i+6)=LUDIC THEN
j==+1
PrintF("%I. %I-%I-%I%E",j,i,i+2,i+6)
FI
OD
RETURN
 
PROC Main()
DEFINE COUNT="22000"
BYTE ARRAY lud(COUNT+1)
INT i,n
 
PrintE("Please wait...")
LudicSieve(lud,COUNT+1)
Put(125) PutE() ;clear the screen
 
PrintE("First 25 ludic numbers:")
PrintLudicNumbers(lud,COUNT+1,0,24)
 
n=CountLudicNumbers(lud,1000)
PrintF("There are %I ludic numbers <= 1000%E%E",n)
 
PrintE("2000'th..2005'th ludic numbers:")
PrintLudicNumbers(lud,COUNT+1,1999,2004)
 
PrintE("Ludic triplets below 250")
PrintLudicTriplets(lud,249)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Ludic_numbers.png Screenshot from Atari 8-bit computer]
<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
 
There are 142 ludic numbers <= 1000
 
2000'th..2005'th ludic numbers:
21475 21481 21487 21493 21503 21511
 
Ludic triplets below 250
1. 1-3-7
2. 5-7-11
3. 11-13-17
4. 23-25-29
5. 41-43-47
6. 173-175-179
7. 221-223-227
8. 233-235-239
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
 
procedure Ludic_Numbers is
 
package Lucid_Lists is
new Ada.Containers.Vectors (Positive, Natural);
use Lucid_Lists;
 
List : Vector;
 
procedure Fill is
use type Ada.Containers.Count_Type;
Vec : Vector;
Lucid : Natural;
Index : Positive;
begin
Append (List, 1);
 
for I in 2 .. 22_000 loop
Append (Vec, I);
end loop;
 
loop
Lucid := First_Element (Vec);
Append (List, Lucid);
 
Index := First_Index (Vec);
loop
Delete (Vec, Index);
Index := Index + Lucid - 1;
exit when Index > Last_Index (Vec);
end loop;
 
exit when Length (Vec) <= 1;
end loop;
 
end Fill;
 
procedure Put_Lucid (First, Last : in Natural) is
use Ada.Text_IO;
begin
Put_Line ("Lucid numbers " & First'Image & " to " & Last'Image & ":");
for I in First .. Last loop
Put (Natural'(List (I))'Image);
end loop;
New_Line;
end Put_Lucid;
 
procedure Count_Lucid (Below : in Natural) is
Count : Natural := 0;
begin
for Lucid of List loop
if Lucid <= Below then
Count := Count + 1;
end if;
end loop;
Ada.Text_IO.Put_Line ("There are " & Count'Image & " lucid numbers <=" & Below'Image);
end Count_Lucid;
 
procedure Find_Triplets (Limit : in Natural) is
 
function Is_Lucid (Value : in Natural) return Boolean is
begin
for X in 1 .. Limit loop
if List (X) = Value then
return True;
end if;
end loop;
return False;
end Is_Lucid;
 
use Ada.Text_IO;
Index : Natural;
Lucid : Natural;
begin
Put_Line ("All triplets of lucid numbers <" & Limit'Image);
Index := First_Index (List);
while List (Index) < Limit loop
Lucid := List (Index);
if Is_Lucid (Lucid + 2) and Is_Lucid (Lucid + 6) then
Put ("(");
Put (Lucid'Image);
Put (Natural'(Lucid + 2)'Image);
Put (Natural'(Lucid + 6)'Image);
Put_Line (")");
end if;
Index := Index + 1;
end loop;
end Find_Triplets;
 
begin
Fill;
Put_Lucid (First => 1,
Last => 25);
Count_Lucid (Below => 1000);
Put_Lucid (First => 2000,
Last => 2005);
Find_Triplets (Limit => 250);
end Ludic_Numbers;</syntaxhighlight>
 
{{out}}
<pre>Lucid numbers 1 to 25:
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
There are 142 lucid numbers <= 1000
Lucid numbers 2000 to 2005:
21475 21481 21487 21493 21503 21511
All triplets of lucid numbers < 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|ALGOL 68}}==
<syntaxhighlight lang="algol68"># find some Ludic numbers #
 
# sieve the Ludic numbers up to 30 000 #
INT max number = 30 000;
[ 1 : max number ]INT candidates;
FOR n TO UPB candidates DO candidates[ n ] := n OD;
FOR n FROM 2 TO UPB candidates OVER 2 DO
IF candidates[ n ] /= 0 THEN
# have a ludic number #
INT number count := -1;
FOR remove pos FROM n TO UPB candidates DO
IF candidates[ remove pos ] /= 0 THEN
# have a number we haven't elminated yet #
number count +:= 1;
IF number count = n THEN
# this number should be removed #
candidates[ remove pos ] := 0;
number count := 0
FI
FI
OD
FI
OD;
# show some Ludic numbers and counts #
print( ( "Ludic numbers: " ) );
INT ludic count := 0;
FOR n TO UPB candidates DO
IF candidates[ n ] /= 0 THEN
# have a ludic number #
ludic count +:= 1;
IF ludic count < 26 THEN
# this is one of the first few Ludic numbers #
print( ( " ", whole( n, 0 ) ) );
IF ludic count = 25 THEN
print( ( " ...", newline ) )
FI
FI;
IF ludic count = 2000 THEN
print( ( "Ludic numbers 2000-2005: ", whole( n, 0 ) ) )
ELIF ludic count > 2000 AND ludic count < 2006 THEN
print( ( " ", whole( n, 0 ) ) );
IF ludic count = 2005 THEN
print( ( newline ) )
FI
FI
FI;
IF n = 1000 THEN
# count ludic numbers up to 1000 #
print( ( "There are ", whole( ludic count, 0 ), " Ludic numbers up to 1000", newline ) )
FI
OD;
# find the Ludic triplets below 250 #
print( ( "Ludic triplets below 250:", newline ) );
FOR n TO 250 - 6 DO
IF candidates[ n ] /= 0 AND candidates[ n + 2 ] /= 0 AND candidates[ n + 6 ] /= 0 THEN
# have a triplet #
print( ( " ", whole( n, -3 ), ", ", whole( n + 2, -3 ), ", ", whole( n + 6, -3 ), newline ) )
FI
OD</syntaxhighlight>
{{out}}
<pre>
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 ...
There are 142 Ludic numbers up to 1000
Ludic numbers 2000-2005: 21475 21481 21487 21493 21503 21511
Ludic triplets below 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|AppleScript}}==
 
<syntaxhighlight lang="applescript">-- Generate a list of the ludic numbers up to and including n.
on ludicsTo(n)
if (n < 1) then return {}
-- Start with an array of numbers from 2 to n and a ludic collection already containing 1.
script o
property array : {}
property ludics : {1}
end script
repeat with i from 2 to n
set end of o's array to i
end repeat
-- Collect ludics and sieve the array until a ludic matches or exceeds the remaining
-- array length, at which point the array contains just the remaining ludics.
set thisLudic to 2
set arrayLength to n - 1
repeat while (thisLudic < arrayLength)
set end of o's ludics to thisLudic
repeat with i from 1 to arrayLength by thisLudic
set item i of o's array to missing value
end repeat
set o's array to o's array's numbers
set thisLudic to beginning of o's array
set arrayLength to (count o's array)
end repeat
return (o's ludics) & (o's array)
end ludicsTo
 
on doTask()
script o
property ludics : ludicsTo(22000)
end script
set output to {}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set end of output to "First 25 ludic numbers:"
set end of output to (items 1 thru 25 of o's ludics) as text
repeat with i from 1 to (count o's ludics)
if (item i of o's ludics > 1000) then exit repeat
end repeat
set end of output to "There are " & (i - 1) & " ludic numbers ≤ 1000."
set end of output to "2000th-2005th ludic numbers:"
set end of output to (items 2000 thru 2005 of o's ludics) as text
set end of output to "Triplets < 250:"
set triplets to {}
repeat with x in o's ludics
set x to x's contents
if (x > 243) then exit repeat
if ((x + 2) is in o's ludics) and ((x + 6) is in o's ludics) then
set end of triplets to "{" & {x, x + 2, x + 6} & "}"
end if
end repeat
set end of output to triplets as text
set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output
end doTask
 
return doTask()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"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
There are 142 ludic numbers ≤ 1000.
2000th-2005th ludic numbers:
21475, 21481, 21487, 21493, 21503, 21511
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}"</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">ludicGen: function [nmax][
result: [1]
lst: new 2..nmax+1
i: 0
worked: false
while [and? [not? empty? lst] [i < size lst]][
item: lst\[i]
result: result ++ item
del: 0
worked: false
while [del < size lst][
worked: true
remove 'lst .index del
del: dec del + item
]
if not? worked -> i: i + 1
]
return result
]
 
ludics: ludicGen 25000
 
print "The first 25 ludic numbers:"
print first.n: 25 ludics
 
leThan1000: select ludics => [& =< 1000]
print ["\nThere are" size leThan1000 "ludic numbers less than/or equal to 1000\n"]
 
print ["The ludic numbers from 2000th to 2005th are:" slice ludics 1999 2004 "\n"]
 
print "The triplets of ludic numbers less than 250 are:"
print map select ludics 'x [
all? @[ x < 250
contains? ludics x+2
contains? ludics x+6
]
] 't -> @[t, t+2, t+6]</syntaxhighlight>
 
{{out}}
 
<pre>The 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
 
There are 142 ludic numbers less than/or equal to 1000
The ludic numbers from 2000th to 2005th are: [21475 21481 21487 21493 21503 21511]
The triplets of ludic numbers less than 250 are:
[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|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">#NoEnv
SetBatchLines, -1
Ludic := LudicSieve(22000)
Line 217 ⟶ 857:
Ludic.Insert(Arr[1])
return Ludic
}</langsyntaxhighlight>
{{Output}}
<pre>First 25: 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
Line 225 ⟶ 865:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 295 ⟶ 935:
free(x);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 303 ⟶ 943:
Triples below 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|C sharp}}==
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections.Generic;
 
public class Program
{
public static void Main()
{
Console.WriteLine("First 25 ludic numbers:");
Console.WriteLine(string.Join(", ", LudicNumbers(150).Take(25)));
Console.WriteLine();
Console.WriteLine($"There are {LudicNumbers(1001).Count()} ludic numbers below 1000");
Console.WriteLine();
foreach (var ludic in LudicNumbers(22000).Skip(1999).Take(6)
.Select((n, i) => $"#{i+2000} = {n}")) {
Console.WriteLine(ludic);
}
Console.WriteLine();
Console.WriteLine("Triplets below 250:");
var queue = new Queue<int>(5);
foreach (int x in LudicNumbers(255)) {
if (queue.Count == 5) queue.Dequeue();
queue.Enqueue(x);
if (x - 6 < 250 && queue.Contains(x - 6) && queue.Contains(x - 4)) {
Console.WriteLine($"{x-6}, {x-4}, {x}");
}
}
}
public static IEnumerable<int> LudicNumbers(int limit) {
yield return 1;
//Like a linked list, but with value types.
//Create 2 extra entries at the start to avoid ugly index calculations
//and another at the end to avoid checking for index-out-of-bounds.
Entry[] values = Enumerable.Range(0, limit + 1).Select(n => new Entry(n)).ToArray();
for (int i = 2; i < limit; i = values[i].Next) {
yield return values[i].N;
int start = i;
while (start < limit) {
Unlink(values, start);
for (int step = 0; step < i && start < limit; step++)
start = values[start].Next;
}
}
}
static void Unlink(Entry[] values, int index) {
values[values[index].Prev].Next = values[index].Next;
values[values[index].Next].Prev = values[index].Prev;
}
}
 
struct Entry
{
public Entry(int n) : this() {
N = n;
Prev = n - 1;
Next = n + 1;
}
public int N { get; }
public int Prev { get; set; }
public int Next { get; set; }
}</syntaxhighlight>
{{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
 
There are 142 ludic numbers below 1000
 
#2000 = 21475
#2001 = 21481
#2002 = 21487
#2003 = 21493
#2004 = 21503
#2005 = 21511
 
Triplets below 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|C++}}==
<langsyntaxhighlight lang="cpp">
#include <vector>
#include <iostream>
Line 387 ⟶ 1,120:
return system( "pause" );
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 410 ⟶ 1,143:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn ints-from [n]
(cons n (lazy-seq (ints-from (inc n)))))
 
Line 434 ⟶ 1,167:
(print "Triplets < 250: ")
(println (filter (partial every? ludic?)
(for [i (range 250)] (list i (+ i 2) (+ i 6)))))</langsyntaxhighlight>
{{output}}
<pre>First 25: (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)
Line 440 ⟶ 1,173:
2000th through 2005th: (21475 21481 21487 21493 21503 21511)
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|Common Lisp}}==
<syntaxhighlight lang="lisp">(defun ludic-numbers (max &optional n)
(loop with numbers = (make-array (1+ max) :element-type 'boolean :initial-element t)
for i from 2 to max
until (and n (= num-results (1- n))) ; 1 will be added at the end
when (aref numbers i)
collect i into results
and count t into num-results
and do (loop for j from i to max
count (aref numbers j) into counter
when (= (mod counter i) 1)
do (setf (aref numbers j) nil))
finally (return (cons 1 results))))
 
(defun main ()
(format t "First 25 ludic numbers:~%")
(format t "~{~D~^ ~}~%" (ludic-numbers 100 25))
(terpri)
(format t "How many ludic numbers <= 1000?~%")
(format t "~D~%" (length (ludic-numbers 1000)))
(terpri)
(let ((numbers (ludic-numbers 30000 2005)))
(format t "~{#~D: ~D~%~}"
(mapcan #'list '(2000 2001 2002 2003 2004 2005) (nthcdr 1999 numbers))))
(terpri)
(loop with numbers = (ludic-numbers 250)
initially (format t "Triplets:~%")
for x in numbers
when (and (find (+ x 2) numbers)
(find (+ x 6) numbers))
do (format t "~3D ~3D ~3D~%" x (+ x 2) (+ x 6))))</syntaxhighlight>
{{output}}
<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
 
How many ludic numbers <= 1000?
142
 
#2000: 21475
#2001: 21481
#2002: 21487
#2003: 21493
#2004: 21503
#2005: 21511
 
Triplets:
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|D}}==
===opApply Version===
{{trans|Python}}
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="d">struct Ludics(T) {
int opApply(int delegate(in ref T) dg) {
int result;
Line 495 ⟶ 1,283:
writefln("\nThere are %d triplets less than %d:\n%s",
triplets.length, m, triplets);
}</langsyntaxhighlight>
{{out}}
<pre>First 25 ludic primes:
Line 512 ⟶ 1,300:
===Range Version===
This is the same code modified to be a Range.
<langsyntaxhighlight lang="d">struct Ludics(T) {
T[] rotor, taken = [T(1)];
T i;
Line 569 ⟶ 1,357:
writefln("\nThere are %d triplets less than %d:\n%s",
triplets.length, m, triplets);
}</langsyntaxhighlight>
The output is the same. This version is slower, it takes about 3.3 seconds to generate 50_000 Ludic numbers with ldc2 compiler.
 
===Range Generator Version===
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.range, std.algorithm, std.concurrency;
 
Line 614 ⟶ 1,402:
writefln("\nThere are %d triplets less than %d:\n%s",
triplets.length, m, triplets);
}</langsyntaxhighlight>
The result is the same.
 
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Ludic_numbers#Pascal Pascal].
 
=={{header|EasyLang}}==
{{trans|Nim}}
<syntaxhighlight>
proc initLudicArray n . res[] .
len res[] n
res[1] = 1
for i = 2 to n
k = 0
for j = i - 1 downto 2
k = k * res[j] div (res[j] - 1) + 1
.
res[i] = k + 2
.
.
initLudicArray 2005 arr[]
for i = 1 to 25
write arr[i] & " "
.
print ""
print ""
i = 1
while arr[i] <= 1000
cnt += 1
i += 1
.
print cnt
print ""
for i = 2000 to 2005
write arr[i] & " "
.
</syntaxhighlight>
{{out}}
<pre>
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
 
142
 
21475 21481 21487 21493 21503 21511
</pre>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
LUDIC_NUMBERS
Line 708 ⟶ 1,539:
 
end
</syntaxhighlight>
</lang>
Test:
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 751 ⟶ 1,582:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 770 ⟶ 1,601:
 
=={{header|Elixir}}==
{{works with|Elixir|1.3.1}}
<lang elixir>defmodule Ludic do
<syntaxhighlight lang="elixir">defmodule Ludic do
def numbers, do: numbers(100000)
def numbers(n \\ 100000) do
def numbers(n) when is_integer(n) do
[h|t] = Enum.to_list(1..n)
numbers(t, [h])
Line 779 ⟶ 1,609:
defp numbers(list, nums) when length(list) < hd(list), do: Enum.reverse(nums, list)
defp numbers([h|_]=list, nums) do
Enum.drop_every(list, h) |> numbers([h | nums])
h = hd(list)
ludic = Enum.with_index(list) |>
Enum.filter_map(fn{_,i} -> rem(i,h)!=0 end, fn{n,_} -> n end)
numbers(ludic, [h | nums])
end
Line 790 ⟶ 1,617:
IO.puts "Below 1000: #{length(numbers(1000))}"
tuple = numbers(25000) |> List.to_tuple
IO.puts "2000..2005th: #{ inspect Enum.map(for i <- 1999..2004, fn i ->do: elem(tuple, i) end) }"
ludic = numbers(250)
triple = for x <-ludic, Enum.member?(ludic, x+2), Enum.member?(in ludic, x+6) in ludic, do: [x, x+2, x+6]
IO.puts "Triples below 250: #{inspect triple, char_lists: :as_lists}"
end
end
 
Ludic.task</langsyntaxhighlight>
 
{{out}}
Line 805 ⟶ 1,632:
2000..2005th: [21475, 21481, 21487, 21493, 21503, 21511]
Triples below 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|Factor}}==
<syntaxhighlight lang="factor">USING: formatting fry kernel make math math.ranges namespaces
prettyprint.config sequences sequences.extras ;
IN: rosetta-code.ludic-numbers
 
: next-ludic ( seq -- seq' )
dup first '[ nip _ mod zero? not ] filter-index ;
 
: ludics-upto-2005 ( -- a )
22,000 2 swap [a,b] [ ! 22k suffices to produce 2005 ludics
1 , [ building get length 2005 = ]
[ dup first , next-ludic ] until drop
] { } make ;
 
: ludic-demo ( -- )
100 margin set ludics-upto-2005
[ 6 tail* ] [ [ 1000 < ] count ] [ 25 head ] tri
"First 25 ludic numbers:\n%u\n\n"
"Count of ludic numbers less than 1000:\n%d\n\n"
"Ludic numbers 2000 to 2005:\n%u\n" [ printf ] tri@ ;
 
MAIN: ludic-demo</syntaxhighlight>
{{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 }
 
Count of ludic numbers less than 1000:
142
 
Ludic numbers 2000 to 2005:
{ 21475 21481 21487 21493 21503 21511 }
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program ludic_numbers
implicit none
Line 861 ⟶ 1,722:
end do
 
end program</langsyntaxhighlight>
Output:
<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
Line 867 ⟶ 1,728:
Ludic numbers 2000 to 2005: 21475 21481 21487 21493 21503 21511
Ludic Triplets below 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|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' As it would be too expensive to actually remove elements from the array
' we instead set an element to 0 if it has been removed.
 
Sub ludic(n As Integer, lu() As Integer)
If n < 1 Then Return
Redim lu(1 To n)
lu(1) = 1
If n = 1 Then Return
Dim As Integer count = 1, count2
Dim As Integer i, j, k = 1
Dim As Integer ub = 22000 '' big enough to deal with up to 2005 ludic numbers
Dim a(2 To ub) As Integer
For i = 2 To ub : a(i) = i : Next
 
Do
k += 1
 
For i = k to ub
If a(i) > 0 Then
count += 1
lu(count) = a(i)
If n = count Then Return
a(i) = 0
k = i
Exit For
End If
Next
 
count2 = 0
j = k + 1
 
While j <= ub
If a(j) > 0 Then
count2 +=1
If count2 = k Then
a(j) = 0
count2 = 0
End If
End If
j += 1
Wend
Loop
End Sub
 
Dim i As Integer
Dim lu() As Integer
ludic(2005, lu())
Print "The first 25 Ludic numbers are :"
For i = 1 To 25
Print Using "###"; lu(i);
Print " ";
Next
Print
 
Dim As Integer Count = 0
For i = 1 To 1000
If lu(i) <= 1000 Then
count += 1
Else
Exit For
End If
Next
Print
Print "There are"; count; " Ludic numbers <= 1000"
Print
 
Print "The 2000th to 2005th Ludics are :"
For i = 2000 To 2005
Print lu(i); " ";
Next
Print : Print
 
Print "The Ludic triplets below 250 are : "
Dim As Integer j, k, ldc
Dim b As Boolean
For i = 1 To 248
ldc = lu(i)
If ldc >= 244 Then Exit For
b = False
For j = i + 1 To 249
If lu(j) = ldc + 2 Then
b = True
k = j
Exit For
ElseIf lu(j) > ldc + 2 Then
Exit For
End If
Next j
If b = False Then Continue For
For j = k + 1 To 250
If lu(j) = ldc + 6 Then
Print "("; Str(ldc); ","; ldc + 2; ","; ldc + 6; ")"
Exit For
ElseIf lu(j) > ldc + 6 Then
Exit For
End If
Next j
Next i
Erase lu
Print
 
Print "Press any key to quit"
Sleep </syntaxhighlight>
 
{{out}}
<pre>
The first 25 Ludic numbers are :
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
 
There are 142 Ludic numbers <= 1000
 
The 2000th to 2005th Ludics are :
21475 21481 21487 21493 21503 21511
 
The Ludic triplets below 250 are :
(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|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 949 ⟶ 1,940:
}
fmt.Println()
}</langsyntaxhighlight>
[http://play.golang.org/p/pj7UmJnqoE Run in Go Playground].
{{out}}
Line 958 ⟶ 1,949:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr, genericSplitAt)
 
ludic :: [Integer]
ludic = 1 : unfoldr (\xs@(x:_) -> Just (x, dropEvery x xs)) [2 ..] where
where
dropEvery n = concat . map tail . unfoldr (Just . genericSplitAt n)
dropEvery n = concatMap tail . unfoldr (Just . genericSplitAt n)
 
main :: IO ()
main = do
print $ take 25 $ ludic
(print $. length) $ takeWhile (<= 1000) $ ludic
print $ take 6 $ drop 1999 $ ludic
-- haven't done triplets task yet</langsyntaxhighlight>
{{out}}
<pre>
Line 978 ⟶ 1,970:
 
The filter for dropping every n-th number can be delayed until it's needed, which speeds up the generator, more so when a longer sequence is taken.
<langsyntaxhighlight lang="haskell">ludic = 1:2 : f 3 [3..] [(4,2)] where
f n (x:xs) yy@((i,y):ys)
| n == i = f n (dropEvery y xs) ys
Line 986 ⟶ 1,978:
(a,b) = splitAt (n-1) s
 
main = print $ ludic !! 10000</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
This is inefficient, but was fun to code as a cascade of filters. Works in both languages.
<langsyntaxhighlight lang="unicon">global num, cascade, sieve, nfilter
 
procedure main(A)
Line 1,025 ⟶ 2,017:
if (count +:= 1) > limit then lds@&main
put(lds, ludic)
end</langsyntaxhighlight>
 
Output:
Line 1,038 ⟶ 2,030:
 
=={{header|J}}==
'''Solution''' (''naive'' / ''brute force''):<langsyntaxhighlight lang="j"> ludic =: _1 |.!.1 [: {."1 [: (#~ 0 ~: {. | i.@#)^:a: 2 + i.</langsyntaxhighlight>
'''Examples''':<langsyntaxhighlight lang="j"> # ludic 110 NB. 110 is sufficient to generate 25 Ludic numbers
25
ludic 110 NB. First 25 Ludic numbers
Line 1,060 ⟶ 2,052:
173 175 179
221 223 227
233 235 239</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
This example uses pre-calculated ranges for the first and third task items (noted in comments).
<langsyntaxhighlight lang="java5">import java.util.ArrayList;
import java.util.List;
 
Line 1,109 ⟶ 2,101:
System.out.println("Triplets up to 250: " + getTriplets(ludicUpTo(250)));
}
}</langsyntaxhighlight>
{{out}}
<pre>First 25 Ludics: [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]
Line 1,115 ⟶ 2,107:
2000th - 2005th Ludics: [21475, 21481, 21487, 21493, 21503, 21511]
Triplets up to 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|JavaScript}}==
===ES6===
<syntaxhighlight lang="javascript">/**
* Boilerplate to simply get an array filled between 2 numbers
* @param {!number} s Start here (inclusive)
* @param {!number} e End here (inclusive)
*/
const makeArr = (s, e) => new Array(e + 1 - s).fill(s).map((e, i) => e + i);
 
/**
* Remove every n-th element from the given array
* @param {!Array} arr
* @param {!number} n
* @return {!Array}
*/
const filterAtInc = (arr, n) => arr.filter((e, i) => (i + 1) % n);
 
/**
* Generate ludic numbers
* @param {!Array} arr
* @param {!Array} result
* @return {!Array}
*/
const makeLudic = (arr, result) => {
const iter = arr.shift();
result.push(iter);
return arr.length ? makeLudic(filterAtInc(arr, iter), result) : result;
};
 
/**
* Our Ludic numbers. This is a bit of a cheat, as we already know beforehand
* up to where our seed array needs to go in order to exactly get to the
* 2005th Ludic number.
* @type {!Array<!number>}
*/
const ludicResult = makeLudic(makeArr(2, 21512), [1]);
 
 
// Below is just logging out the results.
/**
* Given a number, return a function that takes an array, and return the
* count of all elements smaller than the given
* @param {!number} n
* @return {!Function}
*/
const smallerThanN = n => arr => {
return arr.reduce((p,c) => {
return c <= n ? p + 1 : p
}, 0)
};
const smallerThan1K = smallerThanN(1000);
 
console.log('\nFirst 25 Ludic Numbers:');
console.log(ludicResult.filter((e, i) => i < 25).join(', '));
 
console.log('\nTotal Ludic numbers smaller than 1000:');
console.log(smallerThan1K(ludicResult));
 
console.log('\nThe 2000th to 2005th ludic numbers:');
console.log(ludicResult.filter((e, i) => i > 1998).join(', '));
 
console.log('\nTriplets smaller than 250:');
ludicResult.forEach(e => {
if (e + 6 < 250 && ludicResult.indexOf(e + 2) > 0 && ludicResult.indexOf(e + 6) > 0) {
console.log([e, e + 2, e + 6].join(', '));
}
});</syntaxhighlight>
 
<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
 
Total Ludic numbers smaller than 1000:
142
 
The 2000th to 2005th ludic numbers:
21475, 21481, 21487, 21493, 21503, 21511
 
Triplets smaller than 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|jq}}==
{{works with|jq}}
In this entry, for each task, we do not assume any prior calculation of how big the initial sieve must be.
That is, an adaptive approach is taken.
 
<syntaxhighlight lang="jq"># This method for sieving turns out to be the fastest in jq.
# Input: an array to be sieved.
# Output: if the array length is less then $n then empty, else the sieved array.
def sieve($n):
if length<$n then empty
else . as $in
| reduce range(0;length) as $i ([]; if $i % $n == 0 then . else . + [$in[$i]] end)
end;
 
# Generate a stream of ludic numbers based on sieving range(2; $nmax+1)
def ludic($nmax):
def l:
.[0] as $next
| $next, (sieve($next)|l);
1, ([range(2; $nmax+1)] | l);
 
# Output: an array of the first . ludic primes (including 1)
def first_ludic_primes:
. as $n
| def l:
. as $k
| [ludic(10*$k)] as $a
| if ($a|length) >= $n then $a[:$n]
else (10*$k) | l
end;
l;
 
# Output: an array of the ludic numbers less than .
def ludic_primes:
. as $n
| def l:
. as $k
| [ludic(10*$k)] as $a
| if $a[-1] >= $n then $a | map(select(. < $n))
else (10*$k) | l
end;
l;
# Output; a stream of triplets of ludic numbers, where each member of the triplet is less than .
def triplets:
ludic_primes as $primes
| $primes[] as $p
| $primes
| bsearch($p) as $i
| if $i >= 0
then $primes[$i+1:]
| select( bsearch($p+2) >= 0 and
bsearch($p+6) >= 0)
| [$p, $p+2, $p+6]
else empty
end;
 
 
"First 25 ludic primes:", (25|first_ludic_primes),
"\nThere are \(1000|ludic_primes|length) ludic numbers <= 1000",
( "The \n2000th to 2005th ludic primes are:",
(2005|first_ludic_primes)[2000:]),
( [250 | triplets]
| "\nThere are \(length) triplets less than 250:",
.[] )</syntaxhighlight>
{{out}}
<pre>
First 25 ludic primes:
[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]
 
There are 142 ludic numbers <= 1000
 
2000th to 2005th ludic primes:
[21481,21487,21493,21503,21511]
 
There are 8 triplets less than 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|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
function ludic_filter{T<:Integer}(n::T)
0 < n || throw(DomainError())
Line 1,179 ⟶ 2,347:
println(" ", i, ", ", j, ", ", k)
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,209 ⟶ 2,377:
</pre>
 
=={{header|MathematicaKotlin}}==
{{trans|FreeBASIC}}
<lang Mathematica>n=10^5;
<syntaxhighlight lang="scala">// version 1.0.6
 
/* Rather than remove elements from a MutableList which would be a relatively expensive operation
we instead use two arrays:
1. An array of the Ludic numbers to be returned.
2. A 'working' array of a suitable size whose elements are set to 0 to denote removal. */
 
fun ludic(n: Int): IntArray {
if (n < 1) return IntArray(0)
val lu = IntArray(n) // array of Ludic numbers required
lu[0] = 1
if (n == 1) return lu
var count = 1
var count2: Int
var j: Int
var k = 1
var ub = n * 11 // big enough to deal with up to 2005 ludic numbers
val a = IntArray(ub) { it } // working array
while (true) {
k += 1
for (i in k until ub) {
if (a[i] > 0) {
count +=1
lu[count - 1] = a[i]
if (n == count) return lu
a[i] = 0
k = i
break
}
}
count2 = 0
j = k + 1
while (j < ub) {
if (a[j] > 0) {
count2 +=1
if (count2 == k) {
a[j] = 0
count2 = 0
}
}
j += 1
}
}
}
 
fun main(args: Array<String>) {
val lu: IntArray = ludic(2005)
println("The first 25 Ludic numbers are :")
for (i in 0 .. 24) print("%4d".format(lu[i]))
val count = lu.count { it <= 1000 }
println("\n\nThere are $count Ludic numbers <= 1000" )
 
println("\nThe 2000th to 2005th Ludics are :")
for (i in 1999 .. 2004) print("${lu[i]} ")
 
println("\n\nThe Ludic triplets below 250 are : ")
var k: Int = 0
var ldc: Int
var b: Boolean
for (i in 0 .. 247) {
ldc = lu[i]
if (ldc >= 244) break
b = false
for (j in i + 1 .. 248) {
if (lu[j] == ldc + 2) {
b = true
k = j
break
}
else if (lu[j] > ldc + 2) break
}
if (!b) continue
for (j in k + 1 .. 249) {
if (lu[j] == ldc + 6) {
println("($ldc, ${ldc + 2}, ${ldc + 6})")
break
}
else if (lu[j] > ldc + 6) break
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
The first 25 Ludic numbers are :
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
 
There are 142 Ludic numbers <= 1000
 
The 2000th to 2005th Ludics are :
21475 21481 21487 21493 21503 21511
 
The Ludic triplets below 250 are :
(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|Lua}}==
<syntaxhighlight lang="lua">-- Return table of ludic numbers below limit
function ludics (limit)
local ludList, numList, index = {1}, {}
for n = 2, limit do table.insert(numList, n) end
while #numList > 0 do
index = numList[1]
table.insert(ludList, index)
for key = #numList, 1, -1 do
if key % index == 1 then table.remove(numList, key) end
end
end
return ludList
end
 
-- Return true if n is found in t or false otherwise
function foundIn (t, n)
for k, v in pairs(t) do
if v == n then return true end
end
return false
end
 
-- Display msg followed by all values in t
function show (msg, t)
io.write(msg)
for _, v in pairs(t) do io.write(" " .. v) end
print("\n")
end
 
-- Main procedure
local first25, under1k, inRange, tripList, triplets = {}, 0, {}, {}, {}
for k, v in pairs(ludics(30000)) do
if k <= 25 then table.insert(first25, v) end
if v <= 1000 then under1k = under1k + 1 end
if k >= 2000 and k <= 2005 then table.insert(inRange, v) end
if v < 250 then table.insert(tripList, v) end
end
for _, x in pairs(tripList) do
if foundIn(tripList, x + 2) and foundIn(tripList, x + 6) then
table.insert(triplets, "\n{" .. x .. "," .. x+2 .. "," .. x+6 .. "}")
end
end
show("First 25:", first25)
print(under1k .. " are less than or equal to 1000\n")
show("2000th to 2005th:", inRange)
show("Triplets:", triplets)</syntaxhighlight>
{{out}}
<pre>First 25: 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
 
142 are less than or equal to 1000
 
2000th to 2005th: 21475 21481 21487 21493 21503 21511
 
Triplets:
{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|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">n=10^5;
Ludic={1};
seq=Range[2,n];
Line 1,219 ⟶ 2,558:
out
]
Nest[DoStep,seq,2500];</lang>
 
{{out}}
<pre>Ludic[[;; 25]]
LengthWhile[Ludic, # < 1000 &]
Ludic[[2000 ;; 2005]]
Select[Subsets[Select[Ludic, # < 250 &], {3}], Differences[#] == {2, 4} &]</syntaxhighlight>
{{out}}
 
<pre>{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}
142
{21475, 21481, 21487, 21493, 21503, 21511}
{{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|Nim}}==
Ludic number generation is inspired by Python lazy streaming generator.
Note that to store the ludic numbers we have chosen to use an array rather than a sequence, which allows to use 1-based indexes.
<syntaxhighlight lang="nim">import strutils
 
type LudicArray[N: static int] = array[1..N, int]
 
func initLudicArray[N: static int](): LudicArray[N] =
## Initialize an array of ludic numbers.
result[1] = 1
for i in 2..N:
var k = 0
for j in countdown(i - 1, 2):
k = k * result[j] div (result[j] - 1) + 1
result[i] = k + 2
 
 
proc print(text: string; list: openArray[int]) =
## Print a text followed by a list of ludic numbers.
var line = text
let start = line.len
for val in list:
line.addSep(", ", start)
line.add $val
echo line
 
 
func isLudic(ludicArray: LudicArray; n, start: Positive): bool =
## Check if a number "n" is ludic, starting search from index "start".
for idx in start..ludicArray.N:
let val = ludicArray[idx]
if n == val: return true
if n < val: break
 
 
when isMainModule:
 
let ludicArray = initLudicArray[2005]()
 
print "The 25 first ludic numbers are: ", ludicArray[1..25]
 
var count = 0
for n in ludicArray:
if n > 1000: break
inc count
echo "\nThere are ", count, " ludic numbers less or equal to 1000."
 
print "\nThe 2000th to 2005th ludic numbers are: ", ludicArray[2000..2005]
 
echo "\nThe triplets of ludic numbers less than 250 are:"
var line = ""
for i, n in ludicArray:
if n >= 244:
echo line
break
if ludicArray.isLudic(n + 2, i + 1) and ludicArray.isLudic(n + 6, i + 2):
line.addSep(", ")
line.add "($1, $2, $3)".format(n, n + 2, n + 6)</syntaxhighlight>
 
{{out}}
<pre>The 25 first ludic numbers are: 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
 
There are 142 ludic numbers less or equal to 1000.
 
The 2000th to 2005th ludic numbers are: 21475, 21481, 21487, 21493, 21503, 21511
 
The triplets of ludic numbers less than 250 are:
(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|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">use Collection.Generic;
 
class Ludic {
function : Main(args : String[]) ~ Nil {
ludics := LudicUpTo(110);
Show("First 25 Ludics: ", ludics, 0, ludics->Size());
System.IO.Console->Print("Ludics up to 1000: ")->PrintLine(LudicUpTo(1000)->Size());
ludics := LudicUpTo(22000);
Show("2000th - 2005th Ludics: ", ludics, 1999, 2005);
Show("Triplets up to 250: ", GetTriplets(LudicUpTo(250)));
}
function : LudicUpTo(n : Int) ~ CompareVector<IntHolder> {
ludics := CompareVector->New()<IntHolder>;
for(i := 1; i <= n; i++;){
ludics->AddBack(i);
};
for(cursor := 1; cursor < ludics->Size(); cursor++;) {
thisLudic := ludics->Get(cursor);
removeCursor := cursor + thisLudic;
while(removeCursor < ludics->Size()){
ludics->Remove(removeCursor);
removeCursor := removeCursor + thisLudic - 1;
};
};
 
return ludics;
}
 
function : GetTriplets(ludics : CompareVector<IntHolder>) ~ Vector<CompareVector<IntHolder> > {
triplets := Vector->New()<CompareVector<IntHolder> >;
 
for(i := 0; i < ludics->Size() - 2; i++;){
thisLudic := ludics->Get(i);
if(ludics->Has(thisLudic + 2) & ludics->Has(thisLudic + 6)){
triplet := CompareVector->New()<IntHolder>;
triplet->AddBack(thisLudic);
triplet->AddBack(thisLudic + 2);
triplet->AddBack(thisLudic + 6);
triplets->AddBack(triplet);
};
};
 
return triplets;
}
 
function : Show(title : String, ludics : CompareVector<IntHolder>, start : Int, end : Int) ~ Nil {
title->Print();
'['->Print();
for(i := start; i < end; i +=1;) {
ludics->Get(i)->Get()->Print();
if(i + 1 < ludics->Size()) {
','->Print();
};
};
']'->PrintLine();
}
 
function : Show(title : String, triplets : Vector<CompareVector<IntHolder> >) ~ Nil {
title->PrintLine();
each(i : triplets) {
triplet := triplets->Get(i);
Show("\t", triplet, 0, triplet->Size());
};
}
}
</syntaxhighlight>
 
{{output}}
<pre>
First 25 Ludics: [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]
Ludics up to 1000: 142
2000th - 2005th Ludics: [21475,21481,21487,21493,21503,21511,]
Triplets up to 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|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">func: ludic(n)
{
| ludics l p |
ListBuffer newSize(n) seqFrom(2, n) over addAll ->l
ListBuffer newSize(n) dup add(1) dup ->ludics
 
while(l notEmpty) [
l removeFirst dup ludics add ->p
l size p / p * while(dup 1 > ) [ dup l removeAt drop p - ] drop
] ;
ludics
}
 
func: ludics
{
| l i |
ludic(22000) ->l
Line 1,259 ⟶ 2,750:
l include(i 6 +) ifFalse: [ continue ]
i print ", " print i 2 + print ", " print i 6 + println
] ;</syntaxhighlight>
}</lang>
 
{{out}}
Line 1,276 ⟶ 2,766:
233, 235, 239
</pre>
 
=={{header|PARI/GP}}==
{{Works with|PARI/GP|2.7.4 and above}}
 
===Version #1. Creating vector of ludic numbers' flags, where the index of each flag=1 is the ludic number.===
 
<syntaxhighlight lang="parigp">
\\ Creating Vlf - Vector of ludic numbers' flags,
\\ where the index of each flag=1 is the ludic number.
\\ 2/28/16 aev
ludic(maxn)={my(Vlf=vector(maxn,z,1),n2=maxn\2,k,j1);
for(i=2,n2,
if(Vlf[i], k=0; j1=i+1;
for(j=j1,maxn, if(Vlf[j], k++); if(k==i, Vlf[j]=0; k=0))
);
);
return(Vlf);
}
 
{
\\ Required tests:
my(Vr,L=List(),k=0,maxn=25000);
Vr=ludic(maxn);
print("The first 25 Ludic numbers: ");
for(i=1,maxn, if(Vr[i]==1, k++; print1(i," "); if(k==25, break)));
print("");print("");
k=0;
for(i=1,999, if(Vr[i]==1, k++));
print("Ludic numbers below 1000: ",k);
print("");
k=0;
print("Ludic numbers 2000 to 2005: ");
for(i=1,maxn, if(Vr[i]==1, k++; if(k>=2000&&k<=2005, listput(L,i)); if(k>2005, break)));
for(i=1,6, print1(L[i]," "));
print(""); print("");
print("Ludic Triplets below 250: ");
for(i=1,250, if(Vr[i]&&Vr[i+2]&&Vr[i+6], print1("(",i," ",i+2," ",i+6,") ")));
}
</syntaxhighlight>
{{Output}}
<pre>
The 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
 
Ludic numbers below 1000: 142
 
Ludic numbers 2000 to 2005:
21475 21481 21487 21493 21503 21511
 
Ludic Triplets below 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>
 
===Version #2. Creating vector of ludic numbers.===
Upgraded script from [http://oeis.org/A003309 A003309] to meet task requirements.
 
<syntaxhighlight lang="parigp">
\\ Creating Vl - Vector of ludic numbers.
\\ 2/28/16 aev
ludic2(maxn)={my(Vw=vector(maxn, x, x+1),Vl=Vec([1]),vwn=#Vw,i);
while(vwn>0, i=Vw[1]; Vl=concat(Vl,[i]);
Vw=vector((vwn*(i-1))\i,x,Vw[(x*i+i-2)\(i-1)]); vwn=#Vw
); return(Vl);
}
{
\\ Required tests:
my(Vr,L=List(),k=0,maxn=22000,vrs,vi);
Vr=ludic2(maxn); vrs=#Vr;
print("The first 25 Ludic numbers: ");
for(i=1,25, print1(Vr[i]," "));
print("");print("");
k=0;
for(i=1,vrs, if(Vr[i]<1000, k++, break));
print("Ludic numbers below 1000: ",k);
print("");
k=0;
print("Ludic numbers 2000 to 2005: ");
for(i=2000,2005, print1(Vr[i]," "));
print("");print("");
print("Ludic Triplets below 250: ");
for(i=1,vrs, vi=Vr[i]; if(i==1,print1("(",vi," ",vi+2," ",vi+6,") "); next); if(vi+6<250,if(Vr[i+1]==vi+2&&Vr[i+2]==vi+6, print1("(",vi," ",vi+2," ",vi+6,") "))));
}
</syntaxhighlight>
 
{{Output}}
<pre>
The 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
 
Ludic numbers below 1000: 142
 
Ludic numbers 2000 to 2005:
21475 21481 21487 21493 21503 21511
 
Ludic Triplets below 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|Pascal}}==
=== ===
Inspired by "rotors" of Raku.
Runtime nearly quadratic: maxLudicCnt = 10000 -> 0.03 s =>maxLudicCnt= 100000 -> 3 s
<syntaxhighlight lang="pascal">program lucid;
{$IFDEF FPC}
{$MODE objFPC} // useful for x64
{$ENDIF}
 
const
//66164 -> last < 1000*1000;
maxLudicCnt = 2005;//must be > 1
type
 
tDelta = record
dNum,
dCnt : LongInt;
end;
 
tpDelta = ^tDelta;
tLudicList = array of tDelta;
 
tArrdelta =array[0..0] of tDelta;
tpLl = ^tArrdelta;
 
function isLudic(plL:tpLl;maxIdx:nativeInt):boolean;
var
i,
cn : NativeInt;
Begin
//check if n is 'hit' by a prior ludic number
For i := 1 to maxIdx do
with plL^[i] do
Begin
//Mask read modify write reread
//dec(dCnt);IF dCnt= 0
cn := dCnt;
IF cn = 1 then
Begin
dcnt := dNum;
isLudic := false;
EXIT;
end;
dcnt := cn-1;
end;
isLudic := true;
end;
 
procedure CreateLudicList(var Ll:tLudicList);
var
plL : tpLl;
n,LudicCnt : NativeUint;
begin
// special case 1
n := 1;
Ll[0].dNum := 1;
 
plL := @Ll[0];
LudicCnt := 0;
repeat
inc(n);
If isLudic(plL,LudicCnt ) then
Begin
inc(LudicCnt);
with plL^[LudicCnt] do
Begin
dNum := n;
dCnt := n;
end;
IF (LudicCnt >= High(LL)) then
BREAK;
end;
until false;
end;
 
procedure firstN(var Ll:tLudicList;cnt: NativeUint);
var
i : NativeInt;
Begin
writeln('First ',cnt,' ludic numbers:');
For i := 0 to cnt-2 do
write(Ll[i].dNum,',');
writeln(Ll[cnt-1].dNum);
end;
 
procedure triples(var Ll:tLudicList;max: NativeUint);
var
i,
chk : NativeUint;
Begin
// special case 1,3,7
writeln('Ludic triples below ',max);
write('(',ll[0].dNum,',',ll[2].dNum,',',ll[4].dNum,') ');
 
For i := 1 to High(Ll) do
Begin
chk := ll[i].dNum;
If chk> max then
break;
If (ll[i+2].dNum = chk+6) AND (ll[i+1].dNum = chk+2) then
write('(',ll[i].dNum,',',ll[i+1].dNum,',',ll[i+2].dNum,') ');
end;
writeln;
writeln;
end;
 
procedure LastLucid(var Ll:tLudicList;start,cnt: NativeUint);
var
limit,i : NativeUint;
Begin
dec(start);
limit := high(Ll);
IF cnt >= limit then
cnt := limit;
if start+cnt >limit then
start := limit-cnt;
writeln(Start+1,'.th to ',Start+cnt+1,'.th ludic number');
For i := 0 to cnt-1 do
write(Ll[i+start].dNum,',');
writeln(Ll[start+cnt].dNum);
writeln;
end;
 
function CountLudic(var Ll:tLudicList;Limit: NativeUint):NativeUint;
var
i,res : NativeUint;
Begin
res := 0;
For i := 0 to High(Ll) do begin
IF Ll[i].dnum <= Limit then
inc(res)
else
BREAK;
CountLudic:= res;
end;
 
end;
var
LudicList : tLudicList;
BEGIN
setlength(LudicList,maxLudicCnt);
CreateLudicList(LudicList);
firstN(LudicList,25);
writeln('There are ',CountLudic(LudicList,1000),' ludic numbers below 1000');
LastLucid(LudicList,2000,5);
LastLucid(LudicList,maxLudicCnt,5);
triples(LudicList,250);//all-> (LudicList,LudicList[High(LudicList)].dNum);
END.</syntaxhighlight>
{{Output}}
<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
There are 142 ludic numbers below 1000
2000.th to 2005.th ludic number
21475,21481,21487,21493,21503,21511
 
99995.th to 100000.th ludic number
1561243,1561291,1561301,1561307,1561313,1561333
 
Ludic triples below 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)
 
real 0m2.921s
</pre>
=== ===
Using an array of byte, each containing the distance to the next ludic number. 64-Bit needs only ~ 60% runtime of 32-Bit.
Three times slower than the Version 1. Much space left for improvements, like memorizing the count of ludics of intervals of size 1024 or so, to do bigger steps.Something like skiplist.
<syntaxhighlight lang="pascal">program ludic;
{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
const
MAXNUM =21511;// > 1
//1561333;-> 100000 ludic numbers
//1561243,1561291,1561301,1561307,1561313,1561333
type
tarrLudic = array of byte;
tLudics = array of LongWord;
 
var
Ludiclst : tarrLudic;
 
procedure Firsttwentyfive;
var
i,actLudic : NativeInt;
Begin
writeln('First 25 ludic numbers');
actLudic:= 1;
For i := 1 to 25 do
Begin
write(actLudic:3,',');
inc(actLudic,Ludiclst[actLudic]);
IF i MOD 5 = 0 then
writeln(#8#32);
end;
writeln;
end;
 
procedure CountBelowOneThousand;
var
cnt,actLudic : NativeInt;
Begin
write('Count of ludic numbers below 1000 = ');
actLudic:= 1;
cnt := 1;
while actLudic <= 1000 do
Begin
inc(actLudic,Ludiclst[actLudic]);
inc(cnt);
end;
dec(cnt);
writeln(cnt);writeln;
end;
 
procedure Show2000til2005;
var
cnt,actLudic : NativeInt;
Begin
writeln('ludic number #2000 to #2005');
actLudic:= 1;
cnt := 1;
while cnt < 2000 do
Begin
inc(actLudic,Ludiclst[actLudic]);
inc(cnt);
end;
while cnt < 2005 do
Begin
write(actLudic,',');
inc(actLudic,Ludiclst[actLudic]);
inc(cnt);
end;
writeln(actLudic);writeln;
end;
 
procedure ShowTriplets;
var
actLudic,lastDelta : NativeInt;
Begin
writeln('ludic numbers triplets below 250');
actLudic:= 1;
while actLudic < 250-5 do
Begin
IF (Ludiclst[actLudic] <> 0) AND
(Ludiclst[actLudic+2] <> 0) AND
(Ludiclst[actLudic+6] <> 0) then
writeln('{',actLudic,'|',actLudic+2,'|',actLudic+6,'} ');
inc(actLudic);
end;
writeln;
end;
 
procedure CheckMaxdist;
var
actLudic,Delta,MaxDelta : NativeInt;
Begin
MaxDelta := 0;
actLudic:= 1;
repeat
delta := Ludiclst[actLudic];
inc(actLudic,delta);
IF MAxDelta<delta then
MAxDelta:= delta;
until actLudic>= MAXNUM;
writeln('MaxDist ',MAxDelta);writeln;
end;
 
function GetLudics:tLudics;
//Array of byte containing the distance to next ludic number
//eliminated numbers are set to 0
var
i,actLudic,actcnt,delta,actPos,lastPos,ludicCnt: NativeInt;
Begin
setlength(Ludiclst,MAXNUM+1);
For i := MAXNUM downto 0 do
Ludiclst[i]:= 1;
actLudic := 1;
ludicCnt := 1;
 
repeat
inc(actLudic,Ludiclst[actLudic]);
IF actLudic> MAXNUM then
BREAK;
inc(ludicCnt);
actPos := actLudic;
actcnt := 0;
// Only if there are enough ludics left
IF MaxNum-ludicCnt-actPos > actPos then
Begin
//eliminate every element in actLudic-distance
//delta so i can set Ludiclst[actpos] to zero
delta := Ludiclst[actpos];
repeat
lastPos := actPos;
inc(actpos,delta);
if actPos>=MAXNUM then
BREAK;
delta := Ludiclst[actpos];
inc(actcnt);
IF actcnt= actLudic then
Begin
inc(Ludiclst[LastPos],delta);
//mark as not ludic
Ludiclst[actpos] := 0;
actcnt := 0;
end;
until false;
end;
until false;
writeln(ludicCnt,' ludic numbers upto ',MAXNUM,#13#10);
end;
 
BEGIN
GetLudics;
CheckMaxdist;
Firsttwentyfive;CountBelowOneThousand;Show2000til2005;ShowTriplets ;
setlength(Ludiclst,0)
END.</syntaxhighlight>
{{Out}}
<pre>2005 ludic numbers upto 21511
 
MaxDist 56
 
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
 
Count of ludic numbers below 1000 = 142
 
ludic number #2000 to #2005
21475,21481,21487,21493,21503,21511
 
ludic numbers triplets below 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}
real 0m0.003s
 
100000 ludic numbers upto 1561334
...
real 0m8.438s</pre>
 
=={{header|Perl}}==
The "ludic" subroutine caches the longest generated sequence so far. It also generates the candidates only if no candidates remain.
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 1,338 ⟶ 3,274:
say 'triplets < 250: ', join ' ',
map { '(' . join(' ',$_, $_ + 2, $_ + 6) . ')' }
sort { $a <=> $b } @triplet;</langsyntaxhighlight>
{{out}}
<pre>First 25: 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
Line 1,345 ⟶ 3,281:
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|Perl 6Phix}}==
{{trans|Fortran}}
This implementation has no arbitrary upper limit, since it can keep adding new rotors on the fly. It just gets slower and slower instead... <tt>:-)</tt>
<!--<syntaxhighlight lang="phix">-->
<lang perl6>constant ludic = gather {
<span style="color: #008080;">constant</span> <span style="color: #000000;">LUMAX</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">25000</span>
my @taken = take 1;
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ludic</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">LUMAX</span><span style="color: #0000FF;">)</span>
my @rotor;
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">LUMAX</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">LUMAX</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">LUMAX</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">25</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 25 Ludic numbers: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Ludic numbers below 1000: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">])})</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">LUMAX</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">2000</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2005</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Ludic numbers 2000 to 2005: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">243</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ludic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">6</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %d Ludic triplets below 250: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{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}
Ludic numbers below 1000: 142
Ludic numbers 2000 to 2005: {21475,21481,21487,21493,21503,21511}
There are 8 Ludic triplets below 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|Picat}}==
for 2..* -> $i {
===Recursion===
loop (my $j = 0; $j < @rotor; $j++) {
<syntaxhighlight lang="picat">ludic(N) = Ludic =>
--@rotor[$j] or last;
ludic(2..N, [1], Ludic).
}
ludic([], Ludic0, Ludic) =>
if $j < @rotor {
Ludic = Ludic0.reverse().
@rotor[$j] = @taken[$j+1];
ludic(T, Ludic0, Ludic) =>
}
T2 = ludic_keep(T),
else {
ludic(T2,[T[1]|Ludic0],Ludic).
push @taken, take $i;
push @rotor, @taken[$j+1];
}
}
}
 
% which elements to keep
say ludic[^25];
ludic_keep([]) = [].
say "Number of Ludic numbers <= 1000: ", +(ludic ...^ * > 1000);
ludic_keep([H|List]) = Ludic =>
say "Ludic numbers 2000..2005: ", ludic[1999..2004];
ludic_keep(H,1,List,[],Ludic).
 
ludic_keep(_H,_C,[],Ludic0,Ludic) ?=>
Ludic = Ludic0.reverse().
ludic_keep(H,C,[H1|T],Ludic0,Ludic) =>
(
C mod H > 0 ->
ludic_keep(H,C+1,T,[H1|Ludic0],Ludic)
;
ludic_keep(H,C+1,T,Ludic0,Ludic)
).</syntaxhighlight>
 
===Imperative approach===
<syntaxhighlight lang="picat">ludic2(N) = Ludic =>
A = 1..N,
Ludic = [1],
A := delete(A, 1),
while(A.length > 0)
T = A[1],
Ludic := Ludic ++ [T],
A := delete(A,T),
A := [A[J] : J in 1..A.length, J mod T > 0]
end.</syntaxhighlight>
 
===Test===
The recursive variant is about 10 times faster than the imperative.
<syntaxhighlight lang="picat">go =>
time(check(ludic)),
time(check(ludic2)),
nl.
 
check(LudicFunc) =>
println(ludicFunc=LudicFunc),
 
Ludic1000 = apply(LudicFunc,1000),
 
% first 25
println(first_25=Ludic1000[1..25]),
 
% below 1000
println(num_below_1000=Ludic1000.length),
% 2000..2005
Ludic22000 = apply(LudicFunc,22000),
println(len_22000=Ludic22000.length),
println(ludic_2000_2005=[Ludic22000[I] : I in 2000..2005]),
 
% Triplets
Ludic2500 = apply(LudicFunc,2500),
Triplets=[[N,N+2,N+6] : N in 1..Ludic2500.length,
membchk(N,Ludic2500),
membchk(N+2,Ludic2500),
membchk(N+6,Ludic2500)],
foreach(Triplet in Triplets)
println(Triplet)
end,
nl.
 
</syntaxhighlight>
 
my \l250 = set ludic ...^ * > 250;
say "Ludic triples < 250: ", gather
for l250.keys -> $a {
my $b = $a + 2;
my $c = $a + 6;
take "<$a $b $c>" if $b ∈ l250 and $c ∈ l250;
}</lang>
{{out}}
<pre>ludicFunc = ludic
<pre>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
first_25 = [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
num_below_1000 = 142
Ludic numbers 2000..2005: 21475 21481 21487 21493 21503 21511
len_22000 = 2042
Ludic triples < 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>
ludic_2000_2005 = [21475,21481,21487,21493,21503,21511]
[1,3,7]
[5,7,11]
[11,13,17]
[23,25,29]
[41,43,47]
[173,175,179]
[221,223,227]
[233,235,239]
 
CPU time 0.288 seconds.
 
ludicFunc = ludic2
first_25 = [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]
num_below_1000 = 142
len_22000 = 2042
ludic_2000_2005 = [21475,21481,21487,21493,21503,21511]
[1,3,7]
[5,7,11]
[11,13,17]
[23,25,29]
[41,43,47]
[173,175,179]
[221,223,227]
[233,235,239]
 
CPU time 2.835 seconds.</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de drop (Lst)
(let N (car Lst)
(make
Line 1,422 ⟶ 3,483:
(filter '((X) (< X 250)) L) ) ) ) )
(bye)</langsyntaxhighlight>
{{out}}<pre>
(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)
Line 1,430 ⟶ 3,491:
 
=={{header|PL/I}}==
 
{{incorrect|PL/I|Missing Triplet 1,3,7.}}
<langsyntaxhighlight PLlang="pl/Ii">Ludic_numbers: procedure options (main); /* 18 April 2014 */
declare V(2:22000) fixed, L(2200) fixed;
declare (step, i, j, k, n) fixed binary;
Line 1,465 ⟶ 3,526:
put skip;
i = 1;
put edit ('(', L(1), L(3), L(5), ') ' ) (A, 3 F(4), A);
do i = 1 by 1 while (L(i+2) <= 250);
if (L(i) = L(i+1) - 2) & (L(i) = L(i+2) - 6) then
Line 1,482 ⟶ 3,544:
call Ludic;
 
end Ludic_numbers;</langsyntaxhighlight>
Output:
<pre>The first 25 Ludic numbers are:
Line 1,491 ⟶ 3,553:
21475 21481 21487 21493 21503 21511
Triples are:
( 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|PL/SQL}}==
<langsyntaxhighlight lang="plsql">SET SERVEROUTPUT ON
DECLARE
c_limit CONSTANT PLS_INTEGER := 25000;
Line 1,573 ⟶ 3,635:
END;
/
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,592 ⟶ 3,654:
221, 223, 227
233, 235, 239</pre>
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
# Start with a pool large enough to meet the requirements
$Pool = [System.Collections.ArrayList]( 2..22000 )
# Start with 1, because it's grandfathered in
$Ludic = @( 1 )
# While the size of the pool is still larger than the next Ludic number...
While ( $Pool.Count -gt $Pool[0] )
{
# Add the next Ludic number to the list
$Ludic += $Pool[0]
# Remove from the pool all entries whose index is a multiple of the next Ludic number
[math]::Truncate( ( $Pool.Count - 1 )/ $Pool[0])..0 | ForEach { $Pool.RemoveAt( $_ * $Pool[0] ) }
}
# Add the rest of the numbers in the pool to the list of Ludic numbers
$Ludic += $Pool.ToArray()
</syntaxhighlight>
<syntaxhighlight lang="powershell">
# Display the first 25 Ludic numbers
$Ludic[0..24] -join ", "
''
# Display the count of all Ludic numbers under 1000
$Ludic.Where{ $_ -le 1000 }.Count
''
# Display the 2000th through the 2005th Ludic number
$Ludic[1999..2004] -join ", "
''
# Display all Ludic triplets less than 250
$TripletStart = $Ludic.Where{ $_ -lt 244 -and ( $_ + 2 ) -in $Ludic -and ( $_ + 6 ) -in $Ludic }
$TripletStart.ForEach{ $_, ( $_ + 2 ), ( $_ + 6 ) -join ", " }
</syntaxhighlight>
{{out}}
<pre>
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
 
142
 
21475, 21481, 21487, 21493, 21503, 21511
 
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|Prolog}}==
Simple, straightforward implementation
<syntaxhighlight lang="prolog">
% John Devou: 26-Nov-2021
 
d(_,_,[],[]).
d(N,N,[_|Xs],Rs):- d(N,1,Xs,Rs).
d(N,M,[X|Xs],[X|Rs]):- M < N, M_ is M+1, d(N,M_,Xs,Rs).
 
l([],[]).
l([X|Xs],[X|Rs]):- d(X,1,Xs,Ys), l(Ys,Rs).
 
% g(N,L):- generate in L a list with Ludic numbers up to N
 
g(N,[1|X]):- numlist(2,N,L), l(L,X).
 
s(0,Xs,[],Xs).
s(N,[X|Xs],[X|Ls],Rs):- N > 0, M is N-1, s(M,Xs,Ls,Rs).
 
t([X,Y,Z|_],[X,Y,Z]):- Y =:= X+2, Z =:= X+6.
t([_,Y,Z|Xs],R):- t([Y,Z|Xs],R).
 
% tasks
 
t1:- g(500,L), s(25,L,X,_), write(X), !.
t2:- g(1000,L), length(L,X), write(X), !.
t3:- g(22000,L), s(1999,L,_,R), s(6,R,X,_), write(X), !.
t4:- g(249,L), findall(A, t(L,A), X), write(X), !.
</syntaxhighlight>
{{out}}
<pre>
?- t1.
[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]
true.
 
?- t2.
142
true.
 
?- t3.
[21475,21481,21487,21493,21503,21511]
true.
 
?- t4.
[[5,7,11],[11,13,17],[23,25,29],[41,43,47],[173,175,179],[221,223,227],[233,235,239]]
true.
</pre>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">EnableExplicit
If Not OpenConsole() : End 1 : EndIf
 
#NMAX=25000
 
Dim ludic.b(#NMAX)
FillMemory(@ludic(0),SizeOf(Byte)*#NMAX,#True,#PB_Byte)
 
Define.i i,j,n,c1,c2
Define r1$,r2$,r3$,r4$
 
For i=2 To Int(#NMAX/2)
If ludic(i)
n=0
For j=i+1 To #NMAX
If ludic(j) : n+1 : EndIf
If n=i : ludic(j)=#False : n=0 : EndIf
Next j
EndIf
Next i
 
n=0 : c1=0 : c2=0
For i=1 To #NMAX
If ludic(i) And n<25 : n+1 : r1$+Str(i)+" " : EndIf
If i<=1000 : c1+Bool(ludic(i)) : EndIf
c2+Bool(ludic(i))
If c2>=2000 And c2<=2005 And ludic(i) : r3$+Str(i)+" " : EndIf
If i<243 And ludic(i) And ludic(i+2) And ludic(i+6)
r4$+"["+Str(i)+" "+Str(i+2)+" "+Str(i+6)+"] "
EndIf
Next
r2$=Str(c1)
 
PrintN("First 25 Ludic numbers: " +r1$)
PrintN("Ludic numbers below 1000: " +r2$)
PrintN("Ludic numbers 2000 to 2005: " +r3$)
PrintN("Ludic Triplets below 250: " +r4$)
Input()
End</syntaxhighlight>
{{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
Ludic numbers below 1000: 142
Ludic numbers 2000 to 2005: 21475 21481 21487 21493 21503 21511
Ludic Triplets below 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|Python}}==
===Python: Fast===
<langsyntaxhighlight lang="python">def ludic(nmax=100000):
yield 1
lst = list(range(2, nmax + 1))
Line 1,616 ⟶ 3,830:
if x+6 < n and x+2 in ludics and x+6 in ludics]
print('\nThere are %i triplets less than %i:\n %r'
% (len(triplets), n, triplets))</langsyntaxhighlight>
 
{{out}}
Line 1,632 ⟶ 3,846:
===Python: No set maximum===
The following version of function ludic will return ludic numbers until reaching system limits. It is less efficient than the fast version as all lucid numbers so far are cached; on exhausting the current lst a new list of twice the size is created and the previous deletions applied before continuing.
<langsyntaxhighlight lang="python">def ludic(nmax=64):
yield 1
taken = []
Line 1,643 ⟶ 3,857:
taken.append(t)
yield t
del lst[::t]</langsyntaxhighlight>
 
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.
<syntaxhighlight 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}]')
</syntaxhighlight>
{{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|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ 0 over of
swap times
[ i 1+ swap i poke ]
1 split
[ dup 0 peek
rot over join unrot
over size over > while
1 - temp put
[] swap
[ behead drop
temp share split
dip join
dup [] = until ]
drop temp release
again ]
drop behead drop join ] is ludic ( n --> [ )
 
999 ludic
say "First 25 ludic numbers: "
dup 25 split drop echo
cr cr
say "There are "
size echo
say " ludic numbers less than 1000."
cr cr
25000 ludic
say "Ludic numbers 2000 to 2005: "
1999 split nip 6 split drop echo</syntaxhighlight>
 
{{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 ]
 
There are 142 ludic numbers less than 1000.
 
Ludic numbers 2000 to 2005: [ 21475 21481 21487 21493 21503 21511 ]</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(define lucid-sieve-size 25000) ; this should be enough to do me!
(define lucid?
Line 1,687 ⟶ 3,996:
EOS
(for/list ((x (in-range 250)) #:when (and (lucid? x) (lucid? (+ x 2)) (lucid? (+ x 6))))
(list x (+ x 2) (+ x 6))))</langsyntaxhighlight>
 
{{out}}
Line 1,700 ⟶ 4,009:
((1 3 7) (5 7 11) (11 13 17) (23 25 29) (41 43 47) (173 175 179) (221 223 227) (233 235 239))
cpu time: 18753 real time: 18766 gc time: 80</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-18}}
This implementation has no arbitrary upper limit, since it can keep adding new rotors on the fly. It just gets slower and slower instead... <tt>:-)</tt>
<syntaxhighlight lang="raku" line>constant @ludic = gather {
my @taken = take 1;
my @rotor;
for 2..* -> $i {
loop (my $j = 0; $j < @rotor; $j++) {
--@rotor[$j] or last;
}
if $j < @rotor {
@rotor[$j] = @taken[$j+1];
}
else {
push @taken, take $i;
push @rotor, @taken[$j+1];
}
}
}
say @ludic[^25];
say "Number of Ludic numbers <= 1000: ", +(@ludic ...^ * > 1000);
say "Ludic numbers 2000..2005: ", @ludic[1999..2004];
my \l250 = set @ludic ...^ * > 250;
say "Ludic triples < 250: ", gather
for l250.keys.sort -> $a {
my $b = $a + 2;
my $c = $a + 6;
take "<$a $b $c>" if $b ∈ l250 and $c ∈ l250;
}</syntaxhighlight>
{{out}}
<pre>(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 triples < 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|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program to displaygens/shows (a range of) ludic numbers, or a count ofwhen a range is sameused.*/
parse arg N count bot top triples . /*obtain optional parameters/argsarguments from the CL*/
if N=='' | then N=25 ="," then N= 25 /*Not specified? Use Then use the default.*/
if count=='' | then count=1000 ="," then count= 1000 /* " " " " " " */
if bot=='' | then bot=2000 ="," then bot= 2000 /* " " " " " " */
if top=='' | then top=2005 ="," then top= 2005 /* " " " " " " */
if triples=='' | triples=="," then triples=250-1 249 /* " " " " " " */
#= 0 /*the number of ludic numbers (so far).*/
say 'The first ' N " ludic numbers: " ludic(n)
$= ludic( max(N, count, bot, top, triples) ) /*generate enough ludic nums*/
say 'The first ' N " ludic numbers: " subword($,1,25) /*display 1st N ludic nums*/
do j=1 until word($, j) > count /*search up to a specific #.*/
end /*j*/
say
say "There are " words(ludic( j -count)) 1 ' ludic numbers fromthat are ≤ 1───►'count " (inclusive)." count
say
say "The " bot '───►' to ' top " ' (inclusive) ludic numbers are: "' ludicsubword(bot$,top bot)
@= /*list of ludic triples found (so far).*/
$=ludic(-triples) 0 0; #=0; @=
do j=1 for words($)
_= word($, j) /*it is known that ludic _ exists. */
if _>=triples then leave /*only process up to a specific number.*/
if wordpos(_+2, $)==0 | wordpos(_+6, $)==0 then iterate /*Not triple? Skip it.*/
#= # + 1 /*bump the triple counter. */
@= @ '◄'_ _+2 _+6"► " /*append the newly found triple ──► @ */
end /*j*/
say
do j=1 for words($); _=word($,j) /*it is known that ludic _ exists*/
if wordpos(_+2,$)==0 | wordpos(_+6,$)==0 then iterate /*¬triple.*/
#=#+1; @=@ '◄'_ _+2 _+6"► " /*bump triple counter, and ··· */
end /*j*/ /* [↑] append found triple ──► @*/
 
if @=='' then say 'From 1──►'triples", no triples found."
else say 'From 1──►'triples", " # ' triples found:' @
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────LUDIC subroutine────────────────────*/
ludic: procedure; parse arg m 1 mm,h,@; am $=abs(m); 1 2 if h\=='' then/*$≡ludic am=hnumbers superset; @≡sequence*/
$=1 2; @= do j=3 by 2 to m*15; @= @ j /*$=ludicconstruct #san superset,initial @=#list of seriesnumbers.*/
end /* [↓] construct a ludic series.j*/
@= @' '; n= words(@) /*append a blank to the number sequence*/
do j=3 by 2 to am * max(1,15*((m>0)|h\=='')); @=@ j; end; @=@' '
do while n\==0; f= word(@, 1) /*examine the [↑]first word highin limit:the approx|exact@ list.*/
do while words(@)\= $=0 $ f /*add [↓]the word examineto the first word$ list. */
f=word(@,1); $=$ f do d=1 by f while d<=n; n= n-1 /*appenduse this1st firstnumber, wordelide toall list.occurrences*/
do d=1 by f while d<@=words changestr(' 'word(@, d)" ", @, ' . ') /*usecross─out 1sta #,number elidein all occurances@ */
@=changestr(' 'word(@, end /*d)"*/ ",@, ' . ') /*delete the[↑] # indone eliding the seq#"1st" number. */
end /*d*/ @= translate(@, , .) /*change [↑] dots doneto elidingblanks; "1st"count numbernumbers.*/
@=translate(@,,.) end /*while*/ /*translate periods[↑] to blanksdone eliding ludic numbers. */
end return subword($, 1, m) /*forever*/ /*return [↑]a donerange eliding ludicof #s. ludic numbers. */</syntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]].
@=space(@) /*remove extra blanks from list. */
 
if h=='' then return subword($,1,am) /*return a range of ludic numbers*/
return subword($,m,h-m+1) /*return a section of a range.*/</lang>
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]].
<br><br>
'''output''' &nbsp; using the defaultsdefault values for input:
<pre>
The 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
 
There are 142 ludic numbers fromthat 1───►1000are (inclusive).≤ 1000
 
The 2000 ───► to2005 2005(inclusive) ludic numbers are: 21475 21481 21487 21493 21503 21511
 
From 1──►249, 8 triples found: ◄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|Ring}}==
<syntaxhighlight lang="ring">
# Project : Ludic numbers
 
ludic = list(300)
resludic = []
nr = 1
for n = 1 to len(ludic)
ludic[n] = n+1
next
see "the first 25 Ludic numbers are:" + nl
ludicnumbers(ludic)
showarray(resludic)
see nl
 
see "Ludic numbers below 1000: "
ludic = list(3000)
resludic = []
for n = 1 to len(ludic)
ludic[n] = n+1
next
ludicnumbers(ludic)
showarray2(resludic)
see nr
see nl + nl
 
see "the 2000..2005th Ludic numbers are:" + nl
ludic = list(60000)
resludic = []
for n = 1 to len(ludic)
ludic[n] = n+1
next
ludicnumbers(ludic)
showarray3(resludic)
 
func ludicnumbers(ludic)
for n = 1 to len(ludic)
delludic = []
for m = 1 to len(ludic) step ludic[1]
add(delludic, m)
next
add(resludic, ludic[1])
for p = len(delludic) to 1 step -1
del(ludic, delludic[p])
next
next
 
func showarray(vect)
see "[1, "
svect = ""
for n = 1 to 24
svect = svect + vect[n] + ", "
next
svect = left(svect, len(svect) - 2)
see svect
see "]" + nl
 
func showarray2(vect)
for n = 1 to len(vect)
if vect[n] <= 1000
nr = nr + 1
ok
next
return nr
 
func showarray3(vect)
see "["
svect = ""
for n = 1999 to 2004
svect = svect + vect[n] + ", "
next
svect = left(svect, len(svect) - 2)
see svect
see "]" + nl
</syntaxhighlight>
Output:
<pre>
the first 25 Ludic numbers are:
[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]
 
Ludic numbers below 1000: 142
 
the 2000..2005th ludic numbers are:
[21475, 21481, 21487, 21493, 21503, 21511]
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def ludic(nmax=100000)
Enumerator.new do |y|
y << 1
Line 1,775 ⟶ 4,211:
ludics = ludic(250).to_a
puts "Ludic triples below 250:",
ludics.select{|x| ludics.include?(x+2) and ludics.include?(x+6)}.map{|x| [x, x+2, x+6]}.to_s</langsyntaxhighlight>
{{out}}
<pre>
Line 1,787 ⟶ 4,223:
[[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|Rust}}==
<syntaxhighlight lang="rust">
const ARRAY_MAX: usize = 25_000;
const LUDIC_MAX: usize = 2100;
 
/// Calculates and returns the first `LUDIC_MAX` Ludic numbers.
///
/// Needs a sufficiently large `ARRAY_MAX`.
fn ludic_numbers() -> Vec<usize> {
// The first two Ludic numbers
let mut numbers = vec![1, 2];
// We start the array with an immediate first removal to reduce memory usage by
// collecting only odd numbers.
numbers.extend((3..ARRAY_MAX).step_by(2));
 
// We keep the correct Ludic numbers in place, removing the incorrect ones.
for ludic_idx in 2..LUDIC_MAX {
let next_ludic = numbers[ludic_idx];
 
// We remove incorrect numbers by counting the indices after the correct numbers.
// We start from zero and keep until we reach the potentially incorrect numbers.
// Then we keep only those not divisible by the `next_ludic`.
let mut idx = 0;
numbers.retain(|_| {
let keep = idx <= ludic_idx || (idx - ludic_idx) % next_ludic != 0;
idx += 1;
keep
});
}
 
numbers
}
 
fn main() {
let ludic_numbers = ludic_numbers();
 
print!("First 25: ");
print_n_ludics(&ludic_numbers, 25);
println!();
print!("Number of Ludics below 1000: ");
print_num_ludics_upto(&ludic_numbers, 1000);
println!();
print!("Ludics from 2000 to 2005: ");
print_ludics_from_to(&ludic_numbers, 2000, 2005);
println!();
println!("Triplets below 250: ");
print_triplets_until(&ludic_numbers, 250);
}
 
/// Prints the first `n` Ludic numbers.
fn print_n_ludics(x: &[usize], n: usize) {
println!("{:?}", &x[..n]);
}
 
/// Calculates how many Ludic numbers are below `max_num`.
fn print_num_ludics_upto(x: &[usize], max_num: usize) {
let num = x.iter().take_while(|&&i| i < max_num).count();
println!("{}", num);
}
 
/// Prints Ludic numbers between two numbers.
fn print_ludics_from_to(x: &[usize], from: usize, to: usize) {
println!("{:?}", &x[from - 1..to - 1]);
}
 
/// Calculates triplets until a certain Ludic number.
fn triplets_below(ludics: &[usize], limit: usize) -> Vec<(usize, usize, usize)> {
ludics
.iter()
.enumerate()
.take_while(|&(_, &num)| num < limit)
.filter_map(|(idx, &number)| {
let triplet_2 = number + 2;
let triplet_3 = number + 6;
 
// Search for the other two triplet numbers. We know they are larger than
// `number` so we can give the searches lower bounds of `idx + 1` and
// `idx + 2`. We also know that the `n + 2` number can only ever be two
// numbers away from the previous and the `n + 6` number can only be four
// away (because we removed some in between). Short circuiting and doing
// the check more likely to fail first are also useful.
let is_triplet = ludics[idx + 1..idx + 3].binary_search(&triplet_2).is_ok()
&& ludics[idx + 2..idx + 5].binary_search(&triplet_3).is_ok();
 
if is_triplet {
Some((number, triplet_2, triplet_3))
} else {
None
}
})
.collect()
}
 
/// Prints triplets until a certain Ludic number.
fn print_triplets_until(ludics: &[usize], limit: usize) {
for (number, triplet_2, triplet_3) in triplets_below(ludics, limit) {
println!("{} {} {}", number, triplet_2, triplet_3);
}
}
</syntaxhighlight>
{{out}}
<pre>
First 25: [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 Ludics below 1000: 142
 
Ludics from 2000 to 2005: [21475, 21481, 21487, 21493, 21503]
 
Triplets below 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|Scala}}==
In this example, we define a function to drop every n<sup>th</sup> element from a list and use it to build a lazily evaluated list of all Ludic numbers. We then generate a lazy list of triplets and filter for the triplets of Ludic numbers.
 
<syntaxhighlight lang="scala">object Ludic {
def main(args: Array[String]): Unit = {
println(
s"""|First 25 Ludic Numbers: ${ludic.take(25).mkString(", ")}
|Ludic Numbers <= 1000: ${ludic.takeWhile(_ <= 1000).size}
|2000-2005th Ludic Numbers: ${ludic.slice(1999, 2005).mkString(", ")}
|Triplets <= 250: ${triplets.takeWhile(_._3 <= 250).mkString(", ")}""".stripMargin)
}
def dropByN[T](lst: LazyList[T], len: Int): LazyList[T] = lst.grouped(len).flatMap(_.init).to(LazyList)
def ludic: LazyList[Int] = 1 #:: LazyList.unfold(LazyList.from(2)){case n +: ns => Some((n, dropByN(ns, n)))}
def triplets: LazyList[(Int, Int, Int)] = LazyList.from(1).map(n => (n, n + 2, n + 6)).filter{case (a, b, c) => Seq(a, b, c).forall(ludic.takeWhile(_ <= c).contains)}
}</syntaxhighlight>
 
{{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
Ludic Numbers <= 1000: 142
2000-2005th Ludic Numbers: 21475, 21481, 21487, 21493, 21503, 21511
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|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func set of integer: ludicNumbers (in integer: n) is func
Line 1,849 ⟶ 4,427:
end for;
writeln;
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,857 ⟶ 4,435:
Ludic 2000 to 2005: 21475 21481 21487 21493 21503 21511
Triples below 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|SequenceL}}==
<syntaxhighlight lang="sequencel">
import <Utilities/Set.sl>;
 
ludic(v(1), result(1)) :=
let
n := head(v);
filtered[i] := v[i] when (i-1) mod n /= 0;
in
result when size(v) < 1 else
ludic(filtered, result ++ [n]);
count : int(1) * int * int -> int;
count(v(1), top, index) :=
index-1 when v[index] > top else
count(v, top, index + 1);
 
main() :=
let
ludics := ludic(2...100000, [1]);
ludics250 := ludics[1 ... count(ludics, 250, 1)];
triplets[i] := [i, i+2, i+6] when elementOf(i+2, ludics250) and elementOf(i+6, ludics250)
foreach i within ludics250;
in
"First 25:\n" ++ toString(ludics[1...25]) ++
"\n\nLudics below 1000:\n" ++ toString(count(ludics, 1000, 1)) ++
"\n\nLudic 2000 to 2005:\n" ++ toString(ludics[2000...2005]) ++
"\n\nTriples below 250:\n" ++ toString(triplets) ;
</syntaxhighlight>
{{out}}
<pre>
First 25:
[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]
 
Ludics below 1000:
142
 
Ludic 2000 to 2005:
[21475,21481,21487,21493,21503,21511]
 
Triples below 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|Sidef}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">func ludics_upto(nmax=100000) {
Enumerator({ |collect|
collect(1)
var arr = @(2..nmax)
while (arr) {
collect(var n = arr[0])
arr.range.by(n).each {|i| arr[i] = nil}
arr.compact!
}
})
}
 
func ludics_first(n) {
ludics_upto(n * n.log2).first(n)
}
 
say("First 25 Ludic numbers: ", ludics_first(25).join(' '))
say("Ludics below 1000: ", ludics_upto(1000).len)
say("Ludic numbers 2000 to 2005: ", ludics_first(2005).last(6).join(' '))
 
var a = ludics_upto(250).to_a
say("Ludic triples below 250: ", a.grep{|x| a.contains_all([x+2, x+6]) } \
.map {|x| '(' + [x, x+2, x+6].join(' ') + ')' } \
.join(' '))</syntaxhighlight>
{{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
Ludics below 1000: 142
Ludic numbers 2000 to 2005: 21475 21481 21487 21493 21503 21511
Ludic triples below 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|Standard ML}}==
<syntaxhighlight lang="ocaml">
open List;
 
fun Ludi [] = []
| Ludi (T as h::L) =
let
fun next (h:: L ) =
let
val nw = #2 (ListPair.unzip (filter (fn (a,b) => a mod #2 h <> 0) L) )
in
ListPair.zip ( List.tabulate(List.length nw,fn i=>i) ,nw)
end
in
h :: Ludi ( next T)
end;
 
val ludics = 1:: (#2 (ListPair.unzip(Ludi (ListPair.zip ( List.tabulate(25000,fn i=>i),tabulate (25000,fn i=>i+2)) )) ));
 
app ((fn e => print (e^" ")) o Int.toString ) (take (ludics,25));
length (filter (fn e=> e <= 1000) ludics);
drop (take (ludics,2005),1999);
</syntaxhighlight>
output
<pre>
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 val it = () : unit
- val it = 142 : int
- val it = [21475,21481,21487,21493,21503,21511] : int list
</pre>
 
Line 1,862 ⟶ 4,548:
{{works with|Tcl|8.6}}
The limit on the number of values generated is the depth of stack; this can be set to arbitrarily deep to go as far as you want. Provided you are prepared to wait for the values to be generated.
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc ludic n {
Line 1,904 ⟶ 4,590:
}
}
puts "triplets: [join $l ,]"</langsyntaxhighlight>
{{out}}
<pre>
Line 1,911 ⟶ 4,597:
2000-2005: 21475,21481,21487,21493,21503,21511
triplets: (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|VBScript}}==
<syntaxhighlight lang="vb">
Set list = CreateObject("System.Collections.Arraylist")
Set ludic = CreateObject("System.Collections.Arraylist")
 
'populate the list
For i = 1 To 25000
list.Add i
Next
 
'set 1 as the first ludic number
ludic.Add list(0)
list.RemoveAt(0)
 
'variable to count ludic numbers <= 1000
up_to_1k = 1
 
'determine the succeeding ludic numbers
For j = 2 To 2005
If list.Count > 0 Then
If list(0) <= 1000 Then
up_to_1k = up_to_1k + 1
End If
ludic.Add list(0)
Else
Exit For
End If
increment = list(0) - 1
n = 0
Do While n <= list.Count - 1
list.RemoveAt(n)
n = n + increment
Loop
Next
 
'the first 25 ludics
WScript.StdOut.WriteLine "First 25 Ludic Numbers:"
For k = 0 To 24
If k < 24 Then
WScript.StdOut.Write ludic(k) & ", "
Else
WScript.StdOut.Write ludic(k)
End If
Next
WScript.StdOut.WriteBlankLines(2)
 
'the number of ludics up to 1000
WScript.StdOut.WriteLine "Ludics up to 1000: "
WScript.StdOut.WriteLine up_to_1k
WScript.StdOut.WriteBlankLines(1)
 
'2000th - 2005th ludics
WScript.StdOut.WriteLine "The 2000th - 2005th Ludic Numbers:"
For k = 1999 To 2004
If k < 2004 Then
WScript.StdOut.Write ludic(k) & ", "
Else
WScript.StdOut.Write ludic(k)
End If
Next
WScript.StdOut.WriteBlankLines(2)
 
'triplets up to 250: x, x+2, and x+6
WScript.StdOut.WriteLine "Ludic Triplets up to 250: "
triplets = ""
k = 0
Do While ludic(k) + 6 <= 250
x2 = ludic(k) + 2
x6 = ludic(k) + 6
If ludic.IndexOf(x2,1) > 0 And ludic.IndexOf(x6,1) > 0 Then
triplets = triplets & ludic(k) & ", " & x2 & ", " & x6 & vbCrLf
End If
k = k + 1
Loop
WScript.StdOut.WriteLine triplets
</syntaxhighlight>
 
{{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
 
Ludics up to 1000:
142
 
The 2000th - 2005th Ludic Numbers:
21475, 21481, 21487, 21493, 21503, 21511
 
Ludic Triplets up to 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|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">const max_i32 = 1<<31 - 1 // i.e. math.MaxInt32
// ludic returns a slice of ludic numbers stopping after
// either n entries or when max is exceeded.
// Either argument may be <=0 to disable that limit.
fn ludic(nn int, m int) []u32 {
mut n := nn
mut max := m
if max > 0 && n < 0 {
n = max_i32
}
if n < 1 {
return []
}
if max < 0 {
max = max_i32
}
mut sieve := []u32{len: 10760} // XXX big enough for 2005 ludics
sieve[0] = 1
sieve[1] = 2
if n > 2 {
// We start with even numbers already removed
for i, j := 2, u32(3); i < sieve.len; i, j = i+1, j+2 {
sieve[i] = j
}
// We leave the ludic numbers in place,
// k is the index of the next ludic
for k := 2; k < n; k++ {
mut l := int(sieve[k])
if l >= max {
n = k
break
}
mut i := l
l--
// last is the last valid index
mut last := k + i - 1
for j := k + i + 1; j < sieve.len; i, j = i+1, j+1 {
last = k + i
sieve[last] = sieve[j]
if i%l == 0 {
j++
}
}
// Truncate down to only the valid entries
if last < sieve.len-1 {
sieve = sieve[..last+1]
}
}
}
if n > sieve.len {
panic("program error") // should never happen
}
return sieve[..n]
}
fn has(x []u32, v u32) bool {
for i := 0; i < x.len && x[i] <= v; i++ {
if x[i] == v {
return true
}
}
return false
}
fn main() {
// ludic() is so quick we just call it repeatedly
println("First 25: ${ludic(25, -1)}")
println("Numner of ludics below 1000: ${ludic(-1, 1000).len}")
println("ludic 2000 to 2005: ${ludic(2005, -1)[1999..]}")
print("Tripples below 250:")
x := ludic(-1, 250)
for i, v in x[..x.len-2] {
if has(x[i+1..], v+2) && has(x[i+2..], v+6) {
print(", ($v ${v+2} ${v+6})")
}
}
println('')
}</syntaxhighlight>
 
{{out}}
<pre>
First 25: [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]
Numner of ludics below 1000: 142
ludic 2000 to 2005: [21475, 21481, 21487, 21493, 21503, 21511]
Tripples below 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|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var ludic = Fn.new { |n, max|
var maxInt32 = 2.pow(31) - 1
if (max > 0 && n < 0) n = maxInt32
if (n < 1) return []
if (max < 0) max = maxInt32
var sieve = List.filled(10760, 0)
sieve[0] = 1
sieve[1] = 2
if (n > 2) {
var j = 3
for (i in 2...sieve.count) {
sieve[i] = j
j = j + 2
}
for (k in 2...n) {
var l = sieve[k]
if (l >= max) {
n = k
break
}
var i = l
l = l - 1
var last = k + i - 1
var j = k + i + 1
while (j < sieve.count) {
last = k + i
sieve[last] = sieve[j]
if (i%l == 0) j = j + 1
i = i + 1
j = j + 1
}
if (last < sieve.count-1) sieve = sieve[0..last]
}
}
if (n > sieve.count) Fiber.abort("Program error.")
return sieve[0...n]
}
 
var has = Fn.new { |x, v|
var i = 0
while (i < x.count && x[i] <= v) {
if (x[i] == v) return true
i = i + 1
}
return false
}
 
System.print("First 25: %(ludic.call(25, -1))")
System.print("Number of Ludics below 1000: %(ludic.call(-1, 1000).count)")
System.print("Ludics 2000 to 2005: %(ludic.call(2005, -1)[1999..-1])")
System.print("Triplets below 250:")
var x = ludic.call(-1, 250)
var i = 0
var triples = []
for (v in x.take(x.count-2)) {
if (has.call(x[i+1..-1], v+2) && has.call(x[i+2..-1], v+6)) {
triples.add([v, v+2, v+6])
}
i = i + 1
}
System.print(triples)</syntaxhighlight>
 
{{out}}
<pre>
First 25: [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 Ludics below 1000: 142
Ludics 2000 to 2005: [21475, 21481, 21487, 21493, 21503, 21511]
Triplets below 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|XPL0}}==
<syntaxhighlight lang "XPL0">def Size = 25000;
char Sieve(1+Size);
int I, Count, Ludic;
[\Make sieve for Ludic numbers
for I:= 1 to Size do Sieve(I):= true;
Ludic:= 2;
loop [I:= Ludic; Count:= Ludic;
loop [repeat I:= I+1;
if I > Size then quit;
until Sieve(I);
Count:= Count-1;
if Count = 0 then
[Sieve(I):= false;
Count:= Ludic;
];
];
repeat Ludic:= Ludic+1;
if Ludic > Size then quit;
until Sieve(Ludic);
];
\Show first 25 Ludic numbers
Count:= 0; I:= 1;
loop [if Sieve(I) then
[IntOut(0, I); ChOut(0, ^ );
Count:= Count+1;
if Count >= 25 then quit;
];
I:= I+1;
];
CrLf(0);
\Show how many Ludic numbers are <= 1000
Count:= 0;
for I:= 1 to 1000 do
if Sieve(I) then Count:= Count+1;
IntOut(0, Count);
CrLf(0);
\Show Ludic numbers from 2000 to 2005
Count:= 0; I:= 1;
loop [if Sieve(I) then
[Count:= Count+1;
if Count >= 2000 then
[IntOut(0, I); ChOut(0, ^ )];
if Count >= 2005 then quit;
];
I:= I+1;
];
CrLf(0);
\Show triplets of Ludic numbers < 250
for I:= 1 to 250-1-6 do
if Sieve(I) & Sieve(I+2) & Sieve(I+6) then
[ChOut(0, ^();
IntOut(0, I); ChOut(0, ^ );
IntOut(0, I+2); ChOut(0, ^ );
IntOut(0, I+6); Text(0, ") ");
];
CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
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
142
21475 21481 21487 21493 21503 21511
(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|zkl}}==
This solution builds aan stackiterator ofwith iteratorsfilters, one for each Ludic number, each extending the previous iteratorfilter. A "master" iterator sits atopat the stacktop and provides the interface to the stack. When the next Ludic number is requested, athe "pulsenext train"odd ripplesnumber sent down andthe uplist of filters and downif ...it makes to the stackend, asit numbersis arethe crossednext offLudic number. A new filter is then attached [to the list(s)iterator] (figurewith a starting index of speech,1 noand numberswhich areindexes to cached)strike.
<langsyntaxhighlight lang="zkl">fcn dropNth(n,seq){
if(n==2) return(seq.tweak(fcn(n,skipper,idx){ if(n0==idx.isEveninc()%skipper) Void.Skip else n })); // uggg, special case
.fp1(n,Ref(1))) // skip every nth number of previous sequence
seq.tweak(fcn(n,skipper,w){ if(0==(w.idx+1)%skipper) Void.Skip else n }.fp1(n),
Void,Void,True);
}
fcn ludic{ //-->Walker
Walker(fcn(rw){ w:=rw.value; n:=w.next(); rw.set(dropNth(n,w)); n }.fp(Ref([2..]))).push(1);
.fp(Ref([3..*,2]))) // odd numbers starting at 3
}</lang>
.push(1,2); // first two Ludic numbers
<lang zkl>ludic().walk(25).toString(*).println();
}</syntaxhighlight>
<syntaxhighlight lang="zkl">ludic().walk(25).toString(*).println();
ludic().reduce(fcn(sum,n){ if(n<1000) return(sum+1); return(Void.Stop,sum); },0).println();
ludic().drop(1999).walk(6).println(); // Ludic's between 2000 & 2005
 
ls:=ludic().filter(fcn(n){ (n<250) and True or Void.Stop }); // Ludic's < 250
ls.filter('wrap(n){ ls.holds(n+2) and ls.holds(n+6) }).apply(fcn(n){ T(n,n+2,n+6) }).println();</langsyntaxhighlight>
{{out}}
<pre>
2,063

edits