Ludic numbers: Difference between revisions
Content added Content deleted
Line 3,296: | Line 3,296: | ||
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}} |
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> |
</pre> |
||
=={{header|Picat}}== |
|||
The recursive variant is about 10 times faster than the imperative (using while). |
|||
<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. |
|||
% Recursive (faster) |
|||
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) |
|||
). |
|||
% Imperative approach (slower) |
|||
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.</lang> |
|||
Output: |
|||
<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}}== |
=={{header|PicoLisp}}== |