Ludic numbers: Difference between revisions
Content added Content deleted
(→{{header|Picat}}: Split into subsections. Added {{out}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 45: | Line 45: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F ludic(nmax = 100000) |
||
V r = [1] |
V r = [1] |
||
V lst = Array(2..nmax) |
V lst = Array(2..nmax) |
||
Line 68: | Line 68: | ||
x + 2 C :ludics & |
x + 2 C :ludics & |
||
x + 6 C :ludics).map(x -> (x, x + 2, x + 6)) |
x + 6 C :ludics).map(x -> (x, x + 2, x + 6)) |
||
print("\nThere are #. triplets less than #.:\n #.".format(triplets.len, n, triplets))</ |
print("\nThere are #. triplets less than #.:\n #.".format(triplets.len, n, triplets))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 86: | Line 86: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
{{trans|Fortran}} |
{{trans|Fortran}} |
||
< |
<syntaxhighlight lang="360asm">* Ludic numbers 23/04/2016 |
||
LUDICN CSECT |
LUDICN CSECT |
||
USING LUDICN,R15 set base register |
USING LUDICN,R15 set base register |
||
Line 207: | Line 207: | ||
LUDIC DC 25000X'01' ludic(nmax)=true |
LUDIC DC 25000X'01' ludic(nmax)=true |
||
YREGS |
YREGS |
||
END LUDICN</ |
END LUDICN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 231: | Line 231: | ||
=={{header|ABAP}}== |
=={{header|ABAP}}== |
||
Works with NW 7.40 SP8 |
Works with NW 7.40 SP8 |
||
< |
<syntaxhighlight lang="abap">CLASS lcl_ludic DEFINITION CREATE PUBLIC. |
||
PUBLIC SECTION. |
PUBLIC SECTION. |
||
Line 317: | Line 317: | ||
ENDMETHOD. |
ENDMETHOD. |
||
ENDCLASS.</ |
ENDCLASS.</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 372: | Line 372: | ||
=={{header|Action!}}== |
=={{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. |
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 LUDIC="1" |
||
DEFINE UNKNOWN="2" |
DEFINE UNKNOWN="2" |
||
Line 468: | Line 468: | ||
PrintE("Ludic triplets below 250") |
PrintE("Ludic triplets below 250") |
||
PrintLudicTriplets(lud,249) |
PrintLudicTriplets(lud,249) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Ludic_numbers.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Ludic_numbers.png Screenshot from Atari 8-bit computer] |
||
Line 492: | Line 492: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
with Ada.Containers.Vectors; |
with Ada.Containers.Vectors; |
||
Line 591: | Line 591: | ||
Last => 2005); |
Last => 2005); |
||
Find_Triplets (Limit => 250); |
Find_Triplets (Limit => 250); |
||
end Ludic_Numbers;</ |
end Ludic_Numbers;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 610: | Line 610: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68"># find some Ludic numbers # |
||
# sieve the Ludic numbers up to 30 000 # |
# sieve the Ludic numbers up to 30 000 # |
||
Line 668: | Line 668: | ||
print( ( " ", whole( n, -3 ), ", ", whole( n + 2, -3 ), ", ", whole( n + 6, -3 ), newline ) ) |
print( ( " ", whole( n, -3 ), ", ", whole( n + 2, -3 ), ", ", whole( n + 6, -3 ), newline ) ) |
||
FI |
FI |
||
OD</ |
OD</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 687: | Line 687: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">-- Generate a list of the ludic numbers up to and including n. |
||
on ludicsTo(n) |
on ludicsTo(n) |
||
if (n < 1) then return {} |
if (n < 1) then return {} |
||
Line 748: | Line 748: | ||
end doTask |
end doTask |
||
return doTask()</ |
return doTask()</syntaxhighlight> |
||
{{output}} |
{{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 |
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. |
There are 142 ludic numbers ≤ 1000. |
||
Line 757: | Line 757: | ||
21475, 21481, 21487, 21493, 21503, 21511 |
21475, 21481, 21487, 21493, 21503, 21511 |
||
Triplets < 250: |
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}"</ |
{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}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">ludicGen: function [nmax][ |
||
result: [1] |
result: [1] |
||
lst: new 2..nmax+1 |
lst: new 2..nmax+1 |
||
Line 797: | Line 797: | ||
contains? ludics x+6 |
contains? ludics x+6 |
||
] |
] |
||
] 't -> @[t, t+2, t+6]</ |
] 't -> @[t, t+2, t+6]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 813: | Line 813: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{works with|AutoHotkey 1.1}} |
{{works with|AutoHotkey 1.1}} |
||
< |
<syntaxhighlight lang="autohotkey">#NoEnv |
||
SetBatchLines, -1 |
SetBatchLines, -1 |
||
Ludic := LudicSieve(22000) |
Ludic := LudicSieve(22000) |
||
Line 857: | Line 857: | ||
Ludic.Insert(Arr[1]) |
Ludic.Insert(Arr[1]) |
||
return Ludic |
return Ludic |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{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 |
<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 865: | Line 865: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 935: | Line 935: | ||
free(x); |
free(x); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 945: | Line 945: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Linq; |
using System.Linq; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 1,012: | Line 1,012: | ||
public int Prev { get; set; } |
public int Prev { get; set; } |
||
public int Next { get; set; } |
public int Next { get; set; } |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,038: | Line 1,038: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <vector> |
#include <vector> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,120: | Line 1,120: | ||
return system( "pause" ); |
return system( "pause" ); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,143: | Line 1,143: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn ints-from [n] |
||
(cons n (lazy-seq (ints-from (inc n))))) |
(cons n (lazy-seq (ints-from (inc n))))) |
||
Line 1,167: | Line 1,167: | ||
(print "Triplets < 250: ") |
(print "Triplets < 250: ") |
||
(println (filter (partial every? ludic?) |
(println (filter (partial every? ludic?) |
||
(for [i (range 250)] (list i (+ i 2) (+ i 6)))))</ |
(for [i (range 250)] (list i (+ i 2) (+ i 6)))))</syntaxhighlight> |
||
{{output}} |
{{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) |
<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,175: | Line 1,175: | ||
=={{header|Common Lisp}}== |
=={{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) |
(loop with numbers = (make-array (1+ max) :element-type 'boolean :initial-element t) |
||
for i from 2 to max |
for i from 2 to max |
||
Line 1,204: | Line 1,204: | ||
when (and (find (+ x 2) numbers) |
when (and (find (+ x 2) numbers) |
||
(find (+ x 6) numbers)) |
(find (+ x 6) numbers)) |
||
do (format t "~3D ~3D ~3D~%" x (+ x 2) (+ x 6))))</ |
do (format t "~3D ~3D ~3D~%" x (+ x 2) (+ x 6))))</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre>First 25 ludic numbers: |
<pre>First 25 ludic numbers: |
||
Line 1,233: | Line 1,233: | ||
{{trans|Python}} |
{{trans|Python}} |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="d">struct Ludics(T) { |
||
int opApply(int delegate(in ref T) dg) { |
int opApply(int delegate(in ref T) dg) { |
||
int result; |
int result; |
||
Line 1,283: | Line 1,283: | ||
writefln("\nThere are %d triplets less than %d:\n%s", |
writefln("\nThere are %d triplets less than %d:\n%s", |
||
triplets.length, m, triplets); |
triplets.length, m, triplets); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 25 ludic primes: |
<pre>First 25 ludic primes: |
||
Line 1,300: | Line 1,300: | ||
===Range Version=== |
===Range Version=== |
||
This is the same code modified to be a Range. |
This is the same code modified to be a Range. |
||
< |
<syntaxhighlight lang="d">struct Ludics(T) { |
||
T[] rotor, taken = [T(1)]; |
T[] rotor, taken = [T(1)]; |
||
T i; |
T i; |
||
Line 1,357: | Line 1,357: | ||
writefln("\nThere are %d triplets less than %d:\n%s", |
writefln("\nThere are %d triplets less than %d:\n%s", |
||
triplets.length, m, triplets); |
triplets.length, m, triplets); |
||
}</ |
}</syntaxhighlight> |
||
The output is the same. This version is slower, it takes about 3.3 seconds to generate 50_000 Ludic numbers with ldc2 compiler. |
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=== |
===Range Generator Version=== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.range, std.algorithm, std.concurrency; |
import std.stdio, std.range, std.algorithm, std.concurrency; |
||
Line 1,402: | Line 1,402: | ||
writefln("\nThere are %d triplets less than %d:\n%s", |
writefln("\nThere are %d triplets less than %d:\n%s", |
||
triplets.length, m, triplets); |
triplets.length, m, triplets); |
||
}</ |
}</syntaxhighlight> |
||
The result is the same. |
The result is the same. |
||
Line 1,409: | Line 1,409: | ||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
LUDIC_NUMBERS |
LUDIC_NUMBERS |
||
Line 1,499: | Line 1,499: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test: |
Test: |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 1,542: | Line 1,542: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,562: | Line 1,562: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{works with|Elixir|1.3.1}} |
{{works with|Elixir|1.3.1}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Ludic do |
||
def numbers(n \\ 100000) do |
def numbers(n \\ 100000) do |
||
[h|t] = Enum.to_list(1..n) |
[h|t] = Enum.to_list(1..n) |
||
Line 1,584: | Line 1,584: | ||
end |
end |
||
Ludic.task</ |
Ludic.task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,595: | Line 1,595: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: formatting fry kernel make math math.ranges namespaces |
||
prettyprint.config sequences sequences.extras ; |
prettyprint.config sequences sequences.extras ; |
||
IN: rosetta-code.ludic-numbers |
IN: rosetta-code.ludic-numbers |
||
Line 1,615: | Line 1,615: | ||
"Ludic numbers 2000 to 2005:\n%u\n" [ printf ] tri@ ; |
"Ludic numbers 2000 to 2005:\n%u\n" [ printf ] tri@ ; |
||
MAIN: ludic-demo</ |
MAIN: ludic-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,630: | Line 1,630: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|95 and later}} |
{{works with|Fortran|95 and later}} |
||
< |
<syntaxhighlight lang="fortran">program ludic_numbers |
||
implicit none |
implicit none |
||
Line 1,682: | Line 1,682: | ||
end do |
end do |
||
end program</ |
end program</syntaxhighlight> |
||
Output: |
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 |
<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,690: | Line 1,690: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
' As it would be too expensive to actually remove elements from the array |
' As it would be too expensive to actually remove elements from the array |
||
Line 1,795: | Line 1,795: | ||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep </ |
Sleep </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,820: | Line 1,820: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,900: | Line 1,900: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
[http://play.golang.org/p/pj7UmJnqoE Run in Go Playground]. |
[http://play.golang.org/p/pj7UmJnqoE Run in Go Playground]. |
||
{{out}} |
{{out}} |
||
Line 1,909: | Line 1,909: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (unfoldr, genericSplitAt) |
||
ludic :: [Integer] |
ludic :: [Integer] |
||
Line 1,921: | Line 1,921: | ||
(print . length) $ takeWhile (<= 1000) ludic |
(print . length) $ takeWhile (<= 1000) ludic |
||
print $ take 6 $ drop 1999 ludic |
print $ take 6 $ drop 1999 ludic |
||
-- haven't done triplets task yet</ |
-- haven't done triplets task yet</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,930: | Line 1,930: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="haskell">ludic = 1:2 : f 3 [3..] [(4,2)] where |
||
f n (x:xs) yy@((i,y):ys) |
f n (x:xs) yy@((i,y):ys) |
||
| n == i = f n (dropEvery y xs) ys |
| n == i = f n (dropEvery y xs) ys |
||
Line 1,938: | Line 1,938: | ||
(a,b) = splitAt (n-1) s |
(a,b) = splitAt (n-1) s |
||
main = print $ ludic !! 10000</ |
main = print $ ludic !! 10000</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
This is inefficient, but was fun to code as a cascade of filters. Works in both languages. |
This is inefficient, but was fun to code as a cascade of filters. Works in both languages. |
||
< |
<syntaxhighlight lang="unicon">global num, cascade, sieve, nfilter |
||
procedure main(A) |
procedure main(A) |
||
Line 1,977: | Line 1,977: | ||
if (count +:= 1) > limit then lds@&main |
if (count +:= 1) > limit then lds@&main |
||
put(lds, ludic) |
put(lds, ludic) |
||
end</ |
end</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,990: | Line 1,990: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution''' (''naive'' / ''brute force''):< |
'''Solution''' (''naive'' / ''brute force''):<syntaxhighlight lang="j"> ludic =: _1 |.!.1 [: {."1 [: (#~ 0 ~: {. | i.@#)^:a: 2 + i.</syntaxhighlight> |
||
'''Examples''':< |
'''Examples''':<syntaxhighlight lang="j"> # ludic 110 NB. 110 is sufficient to generate 25 Ludic numbers |
||
25 |
25 |
||
ludic 110 NB. First 25 Ludic numbers |
ludic 110 NB. First 25 Ludic numbers |
||
Line 2,012: | Line 2,012: | ||
173 175 179 |
173 175 179 |
||
221 223 227 |
221 223 227 |
||
233 235 239</ |
233 235 239</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
This example uses pre-calculated ranges for the first and third task items (noted in comments). |
This example uses pre-calculated ranges for the first and third task items (noted in comments). |
||
< |
<syntaxhighlight lang="java5">import java.util.ArrayList; |
||
import java.util.List; |
import java.util.List; |
||
Line 2,061: | Line 2,061: | ||
System.out.println("Triplets up to 250: " + getTriplets(ludicUpTo(250))); |
System.out.println("Triplets up to 250: " + getTriplets(ludicUpTo(250))); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{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] |
<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 2,070: | Line 2,070: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES6=== |
===ES6=== |
||
<syntaxhighlight lang="javascript">/** |
|||
<lang JavaScript>/** |
|||
* Boilerplate to simply get an array filled between 2 numbers |
* Boilerplate to simply get an array filled between 2 numbers |
||
* @param {!number} s Start here (inclusive) |
* @param {!number} s Start here (inclusive) |
||
Line 2,134: | Line 2,134: | ||
console.log([e, e + 2, e + 6].join(', ')); |
console.log([e, e + 2, e + 6].join(', ')); |
||
} |
} |
||
});</ |
});</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 2,162: | Line 2,162: | ||
That is, an adaptive approach is taken. |
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. |
# Input: an array to be sieved. |
||
# Output: if the array length is less then $n then empty, else the sieved array. |
# Output: if the array length is less then $n then empty, else the sieved array. |
||
Line 2,221: | Line 2,221: | ||
( [250 | triplets] |
( [250 | triplets] |
||
| "\nThere are \(length) triplets less than 250:", |
| "\nThere are \(length) triplets less than 250:", |
||
.[] )</ |
.[] )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,245: | Line 2,245: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
function ludic_filter{T<:Integer}(n::T) |
function ludic_filter{T<:Integer}(n::T) |
||
0 < n || throw(DomainError()) |
0 < n || throw(DomainError()) |
||
Line 2,307: | Line 2,307: | ||
println(" ", i, ", ", j, ", ", k) |
println(" ", i, ", ", j, ", ", k) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,339: | Line 2,339: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
/* Rather than remove elements from a MutableList which would be a relatively expensive operation |
/* Rather than remove elements from a MutableList which would be a relatively expensive operation |
||
Line 2,421: | Line 2,421: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,445: | Line 2,445: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- Return table of ludic numbers below limit |
||
function ludics (limit) |
function ludics (limit) |
||
local ludList, numList, index = {1}, {} |
local ludList, numList, index = {1}, {} |
||
Line 2,490: | Line 2,490: | ||
print(under1k .. " are less than or equal to 1000\n") |
print(under1k .. " are less than or equal to 1000\n") |
||
show("2000th to 2005th:", inRange) |
show("2000th to 2005th:", inRange) |
||
show("Triplets:", triplets)</ |
show("Triplets:", triplets)</syntaxhighlight> |
||
{{out}} |
{{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 |
<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,509: | Line 2,509: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">n=10^5; |
||
Ludic={1}; |
Ludic={1}; |
||
seq=Range[2,n]; |
seq=Range[2,n]; |
||
Line 2,523: | Line 2,523: | ||
LengthWhile[Ludic, # < 1000 &] |
LengthWhile[Ludic, # < 1000 &] |
||
Ludic[[2000 ;; 2005]] |
Ludic[[2000 ;; 2005]] |
||
Select[Subsets[Select[Ludic, # < 250 &], {3}], Differences[#] == {2, 4} &]</ |
Select[Subsets[Select[Ludic, # < 250 &], {3}], Differences[#] == {2, 4} &]</syntaxhighlight> |
||
{{out}} |
{{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} |
<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,533: | Line 2,533: | ||
Ludic number generation is inspired by Python lazy streaming generator. |
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. |
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] |
type LudicArray[N: static int] = array[1..N, int] |
||
Line 2,587: | Line 2,587: | ||
if ludicArray.isLudic(n + 2, i + 1) and ludicArray.isLudic(n + 6, i + 2): |
if ludicArray.isLudic(n + 2, i + 1) and ludicArray.isLudic(n + 6, i + 2): |
||
line.addSep(", ") |
line.addSep(", ") |
||
line.add "($1, $2, $3)".format(n, n + 2, n + 6)</ |
line.add "($1, $2, $3)".format(n, n + 2, n + 6)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,601: | Line 2,601: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="objeck">use Collection.Generic; |
||
class Ludic { |
class Ludic { |
||
Line 2,668: | Line 2,668: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
Line 2,688: | Line 2,688: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: ludic(n) |
||
| ludics l p | |
| ludics l p | |
||
ListBuffer newSize(n) seqFrom(2, n) over addAll ->l |
ListBuffer newSize(n) seqFrom(2, n) over addAll ->l |
||
Line 2,710: | Line 2,710: | ||
l include(i 6 +) ifFalse: [ continue ] |
l include(i 6 +) ifFalse: [ continue ] |
||
i print ", " print i 2 + print ", " print i 6 + println |
i print ", " print i 2 + print ", " print i 6 + println |
||
] ;</ |
] ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,732: | Line 2,732: | ||
===Version #1. Creating vector of ludic numbers' flags, where the index of each flag=1 is the ludic number.=== |
===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, |
\\ Creating Vlf - Vector of ludic numbers' flags, |
||
\\ where the index of each flag=1 is the ludic number. |
\\ where the index of each flag=1 is the ludic number. |
||
Line 2,764: | Line 2,764: | ||
for(i=1,250, if(Vr[i]&&Vr[i+2]&&Vr[i+6], print1("(",i," ",i+2," ",i+6,") "))); |
for(i=1,250, if(Vr[i]&&Vr[i+2]&&Vr[i+6], print1("(",i," ",i+2," ",i+6,") "))); |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,782: | Line 2,782: | ||
Upgraded script from [http://oeis.org/A003309 A003309] to meet task requirements. |
Upgraded script from [http://oeis.org/A003309 A003309] to meet task requirements. |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Creating Vl - Vector of ludic numbers. |
\\ Creating Vl - Vector of ludic numbers. |
||
\\ 2/28/16 aev |
\\ 2/28/16 aev |
||
Line 2,808: | Line 2,808: | ||
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,") ")))); |
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}} |
{{Output}} |
||
Line 2,828: | Line 2,828: | ||
Inspired by "rotors" of Raku. |
Inspired by "rotors" of Raku. |
||
Runtime nearly quadratic: maxLudicCnt = 10000 -> 0.03 s =>maxLudicCnt= 100000 -> 3 s |
Runtime nearly quadratic: maxLudicCnt = 10000 -> 0.03 s =>maxLudicCnt= 100000 -> 3 s |
||
< |
<syntaxhighlight lang="pascal">program lucid; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE objFPC} // useful for x64 |
{$MODE objFPC} // useful for x64 |
||
Line 2,971: | Line 2,971: | ||
LastLucid(LudicList,maxLudicCnt,5); |
LastLucid(LudicList,maxLudicCnt,5); |
||
triples(LudicList,250);//all-> (LudicList,LudicList[High(LudicList)].dNum); |
triples(LudicList,250);//all-> (LudicList,LudicList[High(LudicList)].dNum); |
||
END.</ |
END.</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,990: | Line 2,990: | ||
Using an array of byte, each containing the distance to the next ludic number. 64-Bit needs only ~ 60% runtime of 32-Bit. |
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. |
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} |
{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF} |
||
uses |
uses |
||
Line 3,140: | Line 3,140: | ||
Firsttwentyfive;CountBelowOneThousand;Show2000til2005;ShowTriplets ; |
Firsttwentyfive;CountBelowOneThousand;Show2000til2005;ShowTriplets ; |
||
setlength(Ludiclst,0) |
setlength(Ludiclst,0) |
||
END.</ |
END.</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2005 ludic numbers upto 21511 |
<pre>2005 ludic numbers upto 21511 |
||
Line 3,175: | Line 3,175: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
The "ludic" subroutine caches the longest generated sequence so far. It also generates the candidates only if no candidates remain. |
The "ludic" subroutine caches the longest generated sequence so far. It also generates the candidates only if no candidates remain. |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use warnings; |
use warnings; |
||
use strict; |
use strict; |
||
Line 3,234: | Line 3,234: | ||
say 'triplets < 250: ', join ' ', |
say 'triplets < 250: ', join ' ', |
||
map { '(' . join(' ',$_, $_ + 2, $_ + 6) . ')' } |
map { '(' . join(' ',$_, $_ + 2, $_ + 6) . ')' } |
||
sort { $a <=> $b } @triplet;</ |
sort { $a <=> $b } @triplet;</syntaxhighlight> |
||
{{out}} |
{{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 |
<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 3,243: | Line 3,243: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Fortran}} |
{{trans|Fortran}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">LUMAX</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">25000</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">LUMAX</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">25000</span> |
||
<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> |
<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> |
||
Line 3,288: | Line 3,288: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,299: | Line 3,299: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
===Recursion=== |
===Recursion=== |
||
< |
<syntaxhighlight lang="picat">ludic(N) = Ludic => |
||
ludic(2..N, [1], Ludic). |
ludic(2..N, [1], Ludic). |
||
ludic([], Ludic0, Ludic) => |
ludic([], Ludic0, Ludic) => |
||
Line 3,320: | Line 3,320: | ||
; |
; |
||
ludic_keep(H,C+1,T,Ludic0,Ludic) |
ludic_keep(H,C+1,T,Ludic0,Ludic) |
||
).</ |
).</syntaxhighlight> |
||
===Imperative approach=== |
===Imperative approach=== |
||
< |
<syntaxhighlight lang="picat">ludic2(N) = Ludic => |
||
A = 1..N, |
A = 1..N, |
||
Ludic = [1], |
Ludic = [1], |
||
Line 3,332: | Line 3,332: | ||
A := delete(A,T), |
A := delete(A,T), |
||
A := [A[J] : J in 1..A.length, J mod T > 0] |
A := [A[J] : J in 1..A.length, J mod T > 0] |
||
end.</ |
end.</syntaxhighlight> |
||
===Test=== |
===Test=== |
||
The recursive variant is about 10 times faster than the imperative. |
The recursive variant is about 10 times faster than the imperative. |
||
< |
<syntaxhighlight lang="picat">go => |
||
time(check(ludic)), |
time(check(ludic)), |
||
time(check(ludic2)), |
time(check(ludic2)), |
||
Line 3,368: | Line 3,368: | ||
nl. |
nl. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,404: | Line 3,404: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de drop (Lst) |
||
(let N (car Lst) |
(let N (car Lst) |
||
(make |
(make |
||
Line 3,443: | Line 3,443: | ||
(filter '((X) (< X 250)) L) ) ) ) ) |
(filter '((X) (< X 250)) L) ) ) ) ) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
{{out}}<pre> |
{{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) |
(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 3,452: | Line 3,452: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">Ludic_numbers: procedure options (main); /* 18 April 2014 */ |
||
declare V(2:22000) fixed, L(2200) fixed; |
declare V(2:22000) fixed, L(2200) fixed; |
||
declare (step, i, j, k, n) fixed binary; |
declare (step, i, j, k, n) fixed binary; |
||
Line 3,504: | Line 3,504: | ||
call Ludic; |
call Ludic; |
||
end Ludic_numbers;</ |
end Ludic_numbers;</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>The first 25 Ludic numbers are: |
<pre>The first 25 Ludic numbers are: |
||
Line 3,517: | Line 3,517: | ||
=={{header|PL/SQL}}== |
=={{header|PL/SQL}}== |
||
< |
<syntaxhighlight lang="plsql">SET SERVEROUTPUT ON |
||
DECLARE |
DECLARE |
||
c_limit CONSTANT PLS_INTEGER := 25000; |
c_limit CONSTANT PLS_INTEGER := 25000; |
||
Line 3,595: | Line 3,595: | ||
END; |
END; |
||
/ |
/ |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,617: | Line 3,617: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{works with|PowerShell|2}} |
{{works with|PowerShell|2}} |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
# Start with a pool large enough to meet the requirements |
# Start with a pool large enough to meet the requirements |
||
$Pool = [System.Collections.ArrayList]( 2..22000 ) |
$Pool = [System.Collections.ArrayList]( 2..22000 ) |
||
Line 3,636: | Line 3,636: | ||
# Add the rest of the numbers in the pool to the list of Ludic numbers |
# Add the rest of the numbers in the pool to the list of Ludic numbers |
||
$Ludic += $Pool.ToArray() |
$Ludic += $Pool.ToArray() |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
# Display the first 25 Ludic numbers |
# Display the first 25 Ludic numbers |
||
$Ludic[0..24] -join ", " |
$Ludic[0..24] -join ", " |
||
Line 3,653: | Line 3,653: | ||
$TripletStart = $Ludic.Where{ $_ -lt 244 -and ( $_ + 2 ) -in $Ludic -and ( $_ + 6 ) -in $Ludic } |
$TripletStart = $Ludic.Where{ $_ -lt 244 -and ( $_ + 2 ) -in $Ludic -and ( $_ + 6 ) -in $Ludic } |
||
$TripletStart.ForEach{ $_, ( $_ + 2 ), ( $_ + 6 ) -join ", " } |
$TripletStart.ForEach{ $_, ( $_ + 2 ), ( $_ + 6 ) -join ", " } |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,674: | Line 3,674: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Simple, straightforward implementation |
Simple, straightforward implementation |
||
< |
<syntaxhighlight lang="prolog"> |
||
% John Devou: 26-Nov-2021 |
% John Devou: 26-Nov-2021 |
||
Line 3,700: | Line 3,700: | ||
t3:- g(22000,L), s(1999,L,_,R), s(6,R,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), !. |
t4:- g(249,L), findall(A, t(L,A), X), write(X), !. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,721: | Line 3,721: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">EnableExplicit |
||
If Not OpenConsole() : End 1 : EndIf |
If Not OpenConsole() : End 1 : EndIf |
||
Line 3,759: | Line 3,759: | ||
PrintN("Ludic Triplets below 250: " +r4$) |
PrintN("Ludic Triplets below 250: " +r4$) |
||
Input() |
Input() |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{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 |
<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 3,769: | Line 3,769: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python: Fast=== |
===Python: Fast=== |
||
< |
<syntaxhighlight lang="python">def ludic(nmax=100000): |
||
yield 1 |
yield 1 |
||
lst = list(range(2, nmax + 1)) |
lst = list(range(2, nmax + 1)) |
||
Line 3,790: | Line 3,790: | ||
if x+6 < n and x+2 in ludics and x+6 in ludics] |
if x+6 < n and x+2 in ludics and x+6 in ludics] |
||
print('\nThere are %i triplets less than %i:\n %r' |
print('\nThere are %i triplets less than %i:\n %r' |
||
% (len(triplets), n, triplets))</ |
% (len(triplets), n, triplets))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,806: | Line 3,806: | ||
===Python: No set maximum=== |
===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. |
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. |
||
< |
<syntaxhighlight lang="python">def ludic(nmax=64): |
||
yield 1 |
yield 1 |
||
taken = [] |
taken = [] |
||
Line 3,817: | Line 3,817: | ||
taken.append(t) |
taken.append(t) |
||
yield t |
yield t |
||
del lst[::t]</ |
del lst[::t]</syntaxhighlight> |
||
Output is the same as for the fast version. |
Output is the same as for the fast version. |
||
Line 3,826: | Line 3,826: | ||
<br>Based on the similar algorithm for lucky numbers at https://oeis.org/A000959/a000959.txt. |
<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. |
<br>Function triplets wraps ludic and uses a similar stream-filtering approach to find triplets. |
||
< |
<syntaxhighlight lang="python">def ludic(): |
||
yield 1 |
yield 1 |
||
ludics = [] |
ludics = [] |
||
Line 3,861: | Line 3,861: | ||
break |
break |
||
print(f'[{a}, {b}, {c}]') |
print(f'[{a}, {b}, {c}]') |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{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] |
<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 3,878: | Line 3,878: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define lucid-sieve-size 25000) ; this should be enough to do me! |
(define lucid-sieve-size 25000) ; this should be enough to do me! |
||
(define lucid? |
(define lucid? |
||
Line 3,917: | Line 3,917: | ||
EOS |
EOS |
||
(for/list ((x (in-range 250)) #:when (and (lucid? x) (lucid? (+ x 2)) (lucid? (+ x 6)))) |
(for/list ((x (in-range 250)) #:when (and (lucid? x) (lucid? (+ x 2)) (lucid? (+ x 6)))) |
||
(list x (+ x 2) (+ x 6))))</ |
(list x (+ x 2) (+ x 6))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,935: | Line 3,935: | ||
{{works with|rakudo|2015-09-18}} |
{{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> |
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 |
<syntaxhighlight lang="raku" line>constant @ludic = gather { |
||
my @taken = take 1; |
my @taken = take 1; |
||
my @rotor; |
my @rotor; |
||
Line 3,963: | Line 3,963: | ||
my $c = $a + 6; |
my $c = $a + 6; |
||
take "<$a $b $c>" if $b ∈ l250 and $c ∈ l250; |
take "<$a $b $c>" if $b ∈ l250 and $c ∈ l250; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{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) |
<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 3,971: | Line 3,971: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program gens/shows (a range of) ludic numbers, or a count when a range is used.*/ |
||
parse arg N count bot top triples . /*obtain optional arguments from the CL*/ |
parse arg N count bot top triples . /*obtain optional arguments from the CL*/ |
||
if N=='' | N=="," then N= 25 /*Not specified? Then use the default.*/ |
if N=='' | N=="," then N= 25 /*Not specified? Then use the default.*/ |
||
Line 4,011: | Line 4,011: | ||
@= translate(@, , .) /*change dots to blanks; count numbers.*/ |
@= translate(@, , .) /*change dots to blanks; count numbers.*/ |
||
end /*while*/ /* [↑] done eliding ludic numbers. */ |
end /*while*/ /* [↑] done eliding ludic numbers. */ |
||
return subword($, 1, m) /*return a range of ludic numbers. */</ |
return subword($, 1, m) /*return a range of ludic numbers. */</syntaxhighlight> |
||
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. |
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. |
||
<br><br> |
<br><br> |
||
Line 4,026: | Line 4,026: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Ludic numbers |
# Project : Ludic numbers |
||
Line 4,099: | Line 4,099: | ||
see svect |
see svect |
||
see "]" + nl |
see "]" + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,112: | Line 4,112: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def ludic(nmax=100000) |
||
Enumerator.new do |y| |
Enumerator.new do |y| |
||
y << 1 |
y << 1 |
||
Line 4,132: | Line 4,132: | ||
ludics = ludic(250).to_a |
ludics = ludic(250).to_a |
||
puts "Ludic triples below 250:", |
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</ |
ludics.select{|x| ludics.include?(x+2) and ludics.include?(x+6)}.map{|x| [x, x+2, x+6]}.to_s</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,146: | Line 4,146: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
const ARRAY_MAX: usize = 25_000; |
const ARRAY_MAX: usize = 25_000; |
||
const LUDIC_MAX: usize = 2100; |
const LUDIC_MAX: usize = 2100; |
||
Line 4,244: | Line 4,244: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,267: | Line 4,267: | ||
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. |
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 = { |
def main(args: Array[String]): Unit = { |
||
println( |
println( |
||
Line 4,279: | Line 4,279: | ||
def ludic: LazyList[Int] = 1 #:: LazyList.unfold(LazyList.from(2)){case n +: ns => Some((n, dropByN(ns, n)))} |
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)} |
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}} |
{{out}} |
||
Line 4,288: | Line 4,288: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func set of integer: ludicNumbers (in integer: n) is func |
const func set of integer: ludicNumbers (in integer: n) is func |
||
Line 4,348: | Line 4,348: | ||
end for; |
end for; |
||
writeln; |
writeln; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,359: | Line 4,359: | ||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
<syntaxhighlight lang="sequencel"> |
|||
<lang sequenceL> |
|||
import <Utilities/Set.sl>; |
import <Utilities/Set.sl>; |
||
Line 4,386: | Line 4,386: | ||
"\n\nLudic 2000 to 2005:\n" ++ toString(ludics[2000...2005]) ++ |
"\n\nLudic 2000 to 2005:\n" ++ toString(ludics[2000...2005]) ++ |
||
"\n\nTriples below 250:\n" ++ toString(triplets) ; |
"\n\nTriples below 250:\n" ++ toString(triplets) ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,404: | Line 4,404: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">func ludics_upto(nmax=100000) { |
||
Enumerator({ |collect| |
Enumerator({ |collect| |
||
collect(1) |
collect(1) |
||
Line 4,427: | Line 4,427: | ||
say("Ludic triples below 250: ", a.grep{|x| a.contains_all([x+2, x+6]) } \ |
say("Ludic triples below 250: ", a.grep{|x| a.contains_all([x+2, x+6]) } \ |
||
.map {|x| '(' + [x, x+2, x+6].join(' ') + ')' } \ |
.map {|x| '(' + [x, x+2, x+6].join(' ') + ')' } \ |
||
.join(' '))</ |
.join(' '))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,437: | Line 4,437: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<syntaxhighlight lang="ocaml"> |
|||
<lang OCaml> |
|||
open List; |
open List; |
||
Line 4,458: | Line 4,458: | ||
length (filter (fn e=> e <= 1000) ludics); |
length (filter (fn e=> e <= 1000) ludics); |
||
drop (take (ludics,2005),1999); |
drop (take (ludics,2005),1999); |
||
</syntaxhighlight> |
|||
</lang> |
|||
output |
output |
||
<pre> |
<pre> |
||
Line 4,469: | Line 4,469: | ||
{{works with|Tcl|8.6}} |
{{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. |
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. |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
proc ludic n { |
proc ludic n { |
||
Line 4,511: | Line 4,511: | ||
} |
} |
||
} |
} |
||
puts "triplets: [join $l ,]"</ |
puts "triplets: [join $l ,]"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,521: | Line 4,521: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Set list = CreateObject("System.Collections.Arraylist") |
Set list = CreateObject("System.Collections.Arraylist") |
||
Set ludic = CreateObject("System.Collections.Arraylist") |
Set ludic = CreateObject("System.Collections.Arraylist") |
||
Line 4,595: | Line 4,595: | ||
Loop |
Loop |
||
WScript.StdOut.WriteLine triplets |
WScript.StdOut.WriteLine triplets |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 4,621: | Line 4,621: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="vlang">const max_i32 = 1<<31 - 1 // i.e. math.MaxInt32 |
||
// ludic returns a slice of ludic numbers stopping after |
// ludic returns a slice of ludic numbers stopping after |
||
// either n entries or when max is exceeded. |
// either n entries or when max is exceeded. |
||
Line 4,699: | Line 4,699: | ||
} |
} |
||
println('') |
println('') |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,712: | Line 4,712: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var ludic = Fn.new { |n, max| |
var ludic = Fn.new { |n, max| |
||
Line 4,774: | Line 4,774: | ||
i = i + 1 |
i = i + 1 |
||
} |
} |
||
System.print(triples)</ |
System.print(triples)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,788: | Line 4,788: | ||
=={{header|zkl}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="zkl">fcn dropNth(n,seq){ |
||
seq.tweak(fcn(n,skipper,idx){ if(0==idx.inc()%skipper) Void.Skip else n } |
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 |
.fp1(n,Ref(1))) // skip every nth number of previous sequence |
||
Line 4,796: | Line 4,796: | ||
.fp(Ref([3..*,2]))) // odd numbers starting at 3 |
.fp(Ref([3..*,2]))) // odd numbers starting at 3 |
||
.push(1,2); // first two Ludic numbers |
.push(1,2); // first two Ludic numbers |
||
}</ |
}</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().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 |
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:=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();</ |
ls.filter('wrap(n){ ls.holds(n+2) and ls.holds(n+6) }).apply(fcn(n){ T(n,n+2,n+6) }).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |