Ludic numbers: Difference between revisions

45,844 bytes added ,  14 days ago
Added Easylang
(Added Easylang)
 
(33 intermediate revisions by 24 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.
Line 40 ⟶ 41:
 
<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}}
<langsyntaxhighlight lang="360asm">* Ludic numbers 23/04/2016
LUDICN CSECT
USING LUDICN,R15 set base register
Line 164 ⟶ 207:
LUDIC DC 25000X'01' ludic(nmax)=true
YREGS
END LUDICN</langsyntaxhighlight>
{{out}}
<pre>
Line 188 ⟶ 231:
=={{header|ABAP}}==
Works with NW 7.40 SP8
<langsyntaxhighlight ABAPlang="abap">CLASS lcl_ludic DEFINITION CREATE PUBLIC.
 
PUBLIC SECTION.
Line 274 ⟶ 317:
ENDMETHOD.
 
ENDCLASS.</langsyntaxhighlight>
 
{{Output}}
Line 326 ⟶ 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}}==
<langsyntaxhighlight lang="algol68"># find some Ludic numbers #
 
# sieve the Ludic numbers up to 30 000 #
Line 386 ⟶ 668:
print( ( " ", whole( n, -3 ), ", ", whole( n + 2, -3 ), ", ", whole( n + 6, -3 ), newline ) )
FI
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 402 ⟶ 684:
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 449 ⟶ 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 457 ⟶ 865:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 527 ⟶ 935:
free(x);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 537 ⟶ 945:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections.Generic;
Line 604 ⟶ 1,012:
public int Prev { get; set; }
public int Next { get; set; }
}</langsyntaxhighlight>
{{out}}
<pre>
Line 630 ⟶ 1,038:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <vector>
#include <iostream>
Line 712 ⟶ 1,120:
return system( "pause" );
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 735 ⟶ 1,143:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn ints-from [n]
(cons n (lazy-seq (ints-from (inc n)))))
 
Line 759 ⟶ 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 767 ⟶ 1,175:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight 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
Line 796 ⟶ 1,204:
when (and (find (+ x 2) numbers)
(find (+ x 6) numbers))
do (format t "~3D ~3D ~3D~%" x (+ x 2) (+ x 6))))</langsyntaxhighlight>
{{output}}
<pre>First 25 ludic numbers:
Line 824 ⟶ 1,232:
===opApply Version===
{{trans|Python}}
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="d">struct Ludics(T) {
int opApply(int delegate(in ref T) dg) {
int result;
Line 875 ⟶ 1,283:
writefln("\nThere are %d triplets less than %d:\n%s",
triplets.length, m, triplets);
}</langsyntaxhighlight>
{{out}}
<pre>First 25 ludic primes:
Line 892 ⟶ 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 949 ⟶ 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 994 ⟶ 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 1,088 ⟶ 1,539:
 
end
</syntaxhighlight>
</lang>
Test:
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,131 ⟶ 1,582:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,151 ⟶ 1,602:
=={{header|Elixir}}==
{{works with|Elixir|1.3.1}}
<langsyntaxhighlight lang="elixir">defmodule Ludic do
def numbers(n \\ 100000) do
[h|t] = Enum.to_list(1..n)
Line 1,173 ⟶ 1,624:
end
 
Ludic.task</langsyntaxhighlight>
 
{{out}}
Line 1,184 ⟶ 1,635:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry kernel make math math.ranges namespaces
prettyprint.config sequences sequences.extras ;
IN: rosetta-code.ludic-numbers
Line 1,204 ⟶ 1,655:
"Ludic numbers 2000 to 2005:\n%u\n" [ printf ] tri@ ;
 
MAIN: ludic-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 1,219 ⟶ 1,670:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<langsyntaxhighlight lang="fortran">program ludic_numbers
implicit none
Line 1,271 ⟶ 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 1,279 ⟶ 1,730:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' As it would be too expensive to actually remove elements from the array
Line 1,384 ⟶ 1,835:
 
Print "Press any key to quit"
Sleep </langsyntaxhighlight>
 
{{out}}
Line 1,409 ⟶ 1,860:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,489 ⟶ 1,940:
}
fmt.Println()
}</langsyntaxhighlight>
[http://play.golang.org/p/pj7UmJnqoE Run in Go Playground].
{{out}}
Line 1,498 ⟶ 1,949:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr, genericSplitAt)
 
ludic :: [Integer]
Line 1,510 ⟶ 1,961:
(print . length) $ takeWhile (<= 1000) ludic
print $ take 6 $ drop 1999 ludic
-- haven't done triplets task yet</langsyntaxhighlight>
{{out}}
<pre>
Line 1,519 ⟶ 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 1,527 ⟶ 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,566 ⟶ 2,017:
if (count +:= 1) > limit then lds@&main
put(lds, ludic)
end</langsyntaxhighlight>
 
Output:
Line 1,579 ⟶ 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,601 ⟶ 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,650 ⟶ 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,659 ⟶ 2,110:
=={{header|JavaScript}}==
===ES6===
<syntaxhighlight lang="javascript">/**
<lang JavaScript>/**
* Boilerplate to simply get an array filled between 2 numbers
* @param {!number} s Start here (inclusive)
Line 1,723 ⟶ 2,174:
console.log([e, e + 2, e + 6].join(', '));
}
});</langsyntaxhighlight>
 
<pre>
Line 1,745 ⟶ 2,196:
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,809 ⟶ 2,347:
println(" ", i, ", ", j, ", ", k)
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,841 ⟶ 2,379:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="scala">// version 1.0.6
 
/* Rather than remove elements from a MutableList which would be a relatively expensive operation
Line 1,923 ⟶ 2,461:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,947 ⟶ 2,485:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Return table of ludic numbers below limit
function ludics (limit)
local ludList, numList, index = {1}, {}
Line 1,992 ⟶ 2,530:
print(under1k .. " are less than or equal to 1000\n")
show("2000th to 2005th:", inRange)
show("Triplets:", triplets)</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 2,010 ⟶ 2,548:
{233,235,239}</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">n=10^5;
Ludic={1};
seq=Range[2,n];
Line 2,020 ⟶ 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}}
<langsyntaxhighlight lang="objeck">use Collection.Generic;
 
class Ludic {
Line 2,101 ⟶ 2,708:
}
}
</syntaxhighlight>
</lang>
 
{{output}}
Line 2,121 ⟶ 2,728:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: ludic(n)
| ludics l p |
ListBuffer newSize(n) seqFrom(2, n) over addAll ->l
Line 2,143 ⟶ 2,750:
l include(i 6 +) ifFalse: [ continue ]
i print ", " print i 2 + print ", " print i 6 + println
] ;</langsyntaxhighlight>
 
{{out}}
Line 2,165 ⟶ 2,772:
===Version #1. Creating vector of ludic numbers' flags, where the index of each flag=1 is the ludic number.===
 
<langsyntaxhighlight lang="parigp">
\\ Creating Vlf - Vector of ludic numbers' flags,
\\ where the index of each flag=1 is the ludic number.
Line 2,197 ⟶ 2,804:
for(i=1,250, if(Vr[i]&&Vr[i+2]&&Vr[i+6], print1("(",i," ",i+2," ",i+6,") ")));
}
</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,215 ⟶ 2,822:
Upgraded script from [http://oeis.org/A003309 A003309] to meet task requirements.
 
<langsyntaxhighlight lang="parigp">
\\ Creating Vl - Vector of ludic numbers.
\\ 2/28/16 aev
Line 2,241 ⟶ 2,848:
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,") "))));
}
</langsyntaxhighlight>
 
{{Output}}
Line 2,259 ⟶ 2,866:
=={{header|Pascal}}==
=== ===
Inspired by "rotors" of perl 6 Raku.
Runtime nearly quadratic: maxLudicCnt = 10000 -> 0.03 s =>maxLudicCnt= 100000 -> 3 s
<langsyntaxhighlight lang="pascal">program lucid;
{$IFDEF FPC}
{$MODE objFPC} // useful for x64
Line 2,404 ⟶ 3,011:
LastLucid(LudicList,maxLudicCnt,5);
triples(LudicList,250);//all-> (LudicList,LudicList[High(LudicList)].dNum);
END.</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,423 ⟶ 3,030:
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.
<langsyntaxhighlight lang="pascal">program ludic;
{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
Line 2,573 ⟶ 3,180:
Firsttwentyfive;CountBelowOneThousand;Show2000til2005;ShowTriplets ;
setlength(Ludiclst,0)
END.</langsyntaxhighlight>
{{Out}}
<pre>2005 ludic numbers upto 21511
Line 2,608 ⟶ 3,215:
=={{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 2,667 ⟶ 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 2,673 ⟶ 3,280:
2000..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|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>
<lang perl6>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;
}</lang>
{{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|Phix}}==
{{trans|Fortran}}
<!--<syntaxhighlight lang="phix">-->
<lang Phix>constant LUMAX = 25000
<span style="color: #008080;">constant</span> <span style="color: #000000;">LUMAX</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">25000</span>
sequence ludic = repeat(1,LUMAX)
<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>
integer n
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span>
for i=2 to LUMAX/2 do
<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>
if ludic[i] then
<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>
n = 0
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for j=i+1 to LUMAX do
<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>
n += ludic[j]
<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>
if n=i then
<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>
ludic[j] = 0
<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>
n = 0
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
sequence s = {}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
for i=1 to LUMAX do
<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>
if ludic[i] then
<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>
s &= i
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
if length(s)=25 then exit end if
<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>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"First 25 Ludic numbers: %s\n",{sprint(s)})
<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>
printf(1,"Ludic numbers below 1000: %d\n",{sum(ludic[1..1000])})
<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>
s = {}
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
n = 0
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for i=1 to LUMAX do
<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>
if ludic[i] then
<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>
n += 1
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if n>=2000 then
<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>
s &= i
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
if n=2005 then exit end if
<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>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"Ludic numbers 2000 to 2005: %s\n",{sprint(s)})
<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>
s = {}
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
for i=1 to 243 do
<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>
if ludic[i] and ludic[i+2] and ludic[i+6] then
<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>
s = append(s,{i,i+2,i+6})
<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>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"There are %d Ludic triplets below 250: %s\n",{length(s),sprint(s)})</lang>
<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>
Line 2,765 ⟶ 3,336:
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}}==
===Recursion===
<syntaxhighlight lang="picat">ludic(N) = Ludic =>
ludic(2..N, [1], Ludic).
ludic([], Ludic0, Ludic) =>
Ludic = Ludic0.reverse().
ludic(T, Ludic0, Ludic) =>
T2 = ludic_keep(T),
ludic(T2,[T[1]|Ludic0],Ludic).
 
% which elements to keep
ludic_keep([]) = [].
ludic_keep([H|List]) = Ludic =>
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>
 
{{out}}
<pre>ludicFunc = ludic
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 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 2,806 ⟶ 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 2,815 ⟶ 3,492:
=={{header|PL/I}}==
 
<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 2,867 ⟶ 3,544:
call Ludic;
 
end Ludic_numbers;</langsyntaxhighlight>
Output:
<pre>The first 25 Ludic numbers are:
Line 2,880 ⟶ 3,557:
 
=={{header|PL/SQL}}==
<langsyntaxhighlight lang="plsql">SET SERVEROUTPUT ON
DECLARE
c_limit CONSTANT PLS_INTEGER := 25000;
Line 2,958 ⟶ 3,635:
END;
/
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,980 ⟶ 3,657:
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
# Start with a pool large enough to meet the requirements
$Pool = [System.Collections.ArrayList]( 2..22000 )
Line 2,999 ⟶ 3,676:
# Add the rest of the numbers in the pool to the list of Ludic numbers
$Ludic += $Pool.ToArray()
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
# Display the first 25 Ludic numbers
$Ludic[0..24] -join ", "
Line 3,016 ⟶ 3,693:
$TripletStart = $Ludic.Where{ $_ -lt 244 -and ( $_ + 2 ) -in $Ludic -and ( $_ + 6 ) -in $Ludic }
$TripletStart.ForEach{ $_, ( $_ + 2 ), ( $_ + 6 ) -join ", " }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,033 ⟶ 3,710:
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 3,058 ⟶ 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 3,074 ⟶ 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 3,085 ⟶ 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 3,129 ⟶ 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 3,142 ⟶ 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 displaysgens/shows (a range of) ludic numbers, or a count of when a range is used.*/
parse arg N count bot top triples . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=25 25 /*Not specified? Then use the default.*/
if count=='' | count=="," then count=1000 1000 /* " " " " " " */
if bot=='' | bot=="," then bot=2000 2000 /* " " " " " " */
if top=='' | top=="," then top=2005 2005 /* " " " " " " */
if triples=='' | triples=="," then triples=250-1 249 /* " " " " " " */
$#=ludic( max(N,0 count, bot, top, triples) ) /*generatethe enoughnumber of ludic numsnumbers (so far).*/
say$= 'Theludic( firstmax(N, 'count, bot, Ntop, triples) ") ludic numbers: " subword($,1,25) /*display 1st N /*generate enough ludic nums.*/
say 'The first ' N " ludic numbers: do" j=1 until wordsubword($, j1,25) > count; end /*processdisplay 1st up toN a specificludic #.nums*/
do j=1 until word($, j) > count /*search up to a specific #.*/
end /*j*/
say
say "There are " j - 1 ' ludic numbers that are ≤ ' count
say
say "The " bot '───►' top ' (inclusive) ludic numbers are: ' subword($, bot)
@= /*list of ludic triples found (so far).*/
#=0
@=; do j=1 for words($); _=word($,j) /*it is known that ludic _ exists. */
_= 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; @=@ '◄'_ _+2 _+6"► " /*bump the triple counter,. and ··· */
end /*j*/ @= @ '◄'_ _+2 _+6"► " /* [↑] append the newly found triple ──► @ */
end /*j*/
say
if @=='' then say 'From 1──►'triples", no triples found."
Line 3,169 ⟶ 4,079:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ludic: procedure; parse arg m,,@; $= 1 2 /*$≡ludic numbers superset; @≡sequence*/
do j=3 by 2 to m*15; @= @ j; end /*construct an initial list of numbers.*/
end /*j*/
@=@' '; n=words(@) /*append a blank to the number sequence*/
@= @' '; do while n\==0; f n=word words(@,1); $=$ f /*examineappend thea firstblank wordto inthe @; add tonumber $sequence*/
do while n\==0; do d=1 by f= whileword(@, d<=n; n=n-1) /*useexamine 1stthe number,first word in elidethe all@ occurrenceslist.*/
$= $ f @=changestr(' 'word(@, d)" ", @, ' . ') /*crossoutadd the word to the $ list. a number in @ */
do d=1 by endf while /*d*/ <=n; n= n-1 /*use [↑]1st number, doneelide eliding the "1st" number.all occurrences*/
@=translate changestr(' 'word(@, , .d)" ", @, ' . ') /*changecross─out dotsa tonumber blanks;in count numbers.@ */
end end /*whiled*/ /* [↑] done eliding ludicthe numbers"1st" number. */
return subword @= translate($@, 1, m.) /*returnchange adots to range of ludicblanks; count numbers. */</lang>
end /*while*/ /* [↑] done eliding ludic numbers. */
return subword($, 1, m) /*return a range of ludic numbers. */</syntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]].
<br><br>
Line 3,193 ⟶ 4,105:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Ludic numbers
 
Line 3,266 ⟶ 4,178:
see svect
see "]" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,279 ⟶ 4,191:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def ludic(nmax=100000)
Enumerator.new do |y|
y << 1
Line 3,299 ⟶ 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 3,310 ⟶ 4,222:
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|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>
 
Line 3,315 ⟶ 4,346:
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.
 
<langsyntaxhighlight lang="scala">object Ludic {
def main(args: Array[String]): Unit = {
println(
Line 3,327 ⟶ 4,358:
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)}
}</langsyntaxhighlight>
 
{{out}}
Line 3,336 ⟶ 4,367:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func set of integer: ludicNumbers (in integer: n) is func
Line 3,396 ⟶ 4,427:
end for;
writeln;
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,407 ⟶ 4,438:
 
=={{header|SequenceL}}==
<syntaxhighlight lang="sequencel">
<lang sequenceL>
import <Utilities/Set.sl>;
 
Line 3,434 ⟶ 4,465:
"\n\nLudic 2000 to 2005:\n" ++ toString(ludics[2000...2005]) ++
"\n\nTriples below 250:\n" ++ toString(triplets) ;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,452 ⟶ 4,483:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func ludics_upto(nmax=100000) {
Enumerator({ |collect|
collect(1)
Line 3,475 ⟶ 4,506:
say("Ludic triples below 250: ", a.grep{|x| a.contains_all([x+2, x+6]) } \
.map {|x| '(' + [x, x+2, x+6].join(' ') + ')' } \
.join(' '))</langsyntaxhighlight>
{{out}}
<pre>
Line 3,482 ⟶ 4,513:
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 3,487 ⟶ 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 3,529 ⟶ 4,590:
}
}
puts "triplets: [join $l ,]"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,539 ⟶ 4,600:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Set list = CreateObject("System.Collections.Arraylist")
Set ludic = CreateObject("System.Collections.Arraylist")
Line 3,613 ⟶ 4,674:
Loop
WScript.StdOut.WriteLine triplets
</syntaxhighlight>
</lang>
 
{{Out}}
Line 3,635 ⟶ 4,696:
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 an iterator with filters, one for each Ludic number, each extending the previous filter. A "master" iterator sits at the top and provides the interface. When the next Ludic number is requested, the next odd number sent down the list of filters and if it makes to the end, it is the next Ludic number. A new filter is then attached [to the iterator] with a starting index of 1 and which indexes to strike.
<langsyntaxhighlight lang="zkl">fcn dropNth(n,seq){
seq.tweak(fcn(n,skipper,idx){ if(0==idx.inc()%skipper) Void.Skip else n }
.fp1(n,Ref(1))) // skip every nth number of previous sequence
Line 3,647 ⟶ 4,940:
.fp(Ref([3..*,2]))) // odd numbers starting at 3
.push(1,2); // first two Ludic numbers
}</langsyntaxhighlight>
<langsyntaxhighlight 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>
1,973

edits