Pascal's triangle/Puzzle: Difference between revisions
Content added Content deleted
(Pascal's triangle/Puzzle en FreeBASIC) |
|||
Line 2,226: | Line 2,226: | ||
Two-liner: x=5, y=13, z=8 |
Two-liner: x=5, y=13, z=8 |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
Below are three different approaches using constraint modelling (cp solver). |
|||
{{trans|Prolog}} |
|||
<lang Picat>import cp. |
|||
go => |
|||
puzzle(T, X, Y,Z), |
|||
foreach(TT in T) |
|||
println(TT) |
|||
end, |
|||
println([x=X,y=Y,z=Z]), |
|||
nl, |
|||
fail, % are there any more solutions? |
|||
nl. |
|||
% Port of the Prolog solution |
|||
puzzle(Ts, X, Y, Z) :- |
|||
Ts = [ [151], |
|||
[_, _], |
|||
[40, _, _], |
|||
[_, _, _, _], |
|||
[X, 11, Y, 4, Z]], |
|||
Y #= X + Z, |
|||
triangle(Ts), |
|||
Vs = vars(Ts), |
|||
Vs :: 0..10000, |
|||
solve(Vs). |
|||
triangle([T|Ts]) :- |
|||
( Ts = [N|_] -> triangle_(T, N), triangle(Ts) ; true ). |
|||
triangle_([], _). |
|||
triangle_([T|Ts],[A,B|Rest]) :- |
|||
T #= A + B, triangle_(Ts, [B|Rest]).</lang> |
|||
{{out}} |
|||
<pre>[151] |
|||
[81,70] |
|||
[40,41,29] |
|||
[16,24,17,12] |
|||
[5,11,13,4,8] |
|||
[x = 5,y = 13,z = 8]</pre> |
|||
Here is a contraint model which calculates the positions in the triangle that should be added together. |
|||
<lang Picat>import cp. |
|||
puzzle2 => |
|||
N = 5, % number of rows |
|||
Len = (N*(N+1)) div 2, % number of entries |
|||
% The triangle numbers for 1..N |
|||
T = [I*(I+1) div 2 : I in 1..N], |
|||
% The index of first number to use in addition |
|||
% create the indices of the numbers to add, |
|||
% i.e. Adds[I] + Adds[I+1] |
|||
Adds = new_list(T[N-1]), |
|||
Adds[1] := 2, |
|||
foreach(I in 2..T[N-1]) |
|||
% "jump" of 2 when i-1 is a triangle number |
|||
if membchk(I-1,T) then |
|||
Adds[I] := Adds[I-1] + 2 |
|||
else |
|||
Adds[I] := Adds[I-1] + 1 |
|||
end |
|||
end, |
|||
% the pyramid |
|||
MaxVal = 10_000, |
|||
L = new_list(Len), |
|||
L :: 1..MaxVal, |
|||
% The clues. |
|||
L = [ 151, |
|||
_, _, |
|||
40, _, _, |
|||
_, _,_ , _ , |
|||
X, 11, Y, 4, Z |
|||
], |
|||
% The sums |
|||
foreach(I in 1..T[N-1]) |
|||
L[I] #= L[Adds[I]]+L[Adds[I]+1] |
|||
end, |
|||
% The extra constraint |
|||
Y #= X + Z, |
|||
solve(L), |
|||
println([x=X,y=Y,z=Z]), |
|||
fail, % check if there is another solution |
|||
nl.</lang> |
|||
{{out}} |
|||
<pre>[x = 5,y = 13,z = 8]</pre> |
|||
And then a version with hard coded constraints: |
|||
<lang Picat>import cp. |
|||
puzzle3 => |
|||
% 1 |
|||
% 2 3 |
|||
% 4 5 6 |
|||
% 7 8 9 10 |
|||
%11 12 13 14 15 |
|||
X = new_list(15), |
|||
X :: 0..10_000, |
|||
X[1] #= X[2]+X[3], |
|||
X[2] #= X[4]+X[5], |
|||
X[3] #= X[5]+X[6], |
|||
X[4] #= X[7]+X[8], |
|||
X[5] #= X[8]+X[9], |
|||
X[6] #= X[9]+X[10], |
|||
X[7] #= X[11]+X[12], |
|||
X[8] #= X[12]+X[13], |
|||
X[9] #= X[13]+X[14], |
|||
X[10] #= X[14]+X[15], |
|||
X[13] #= X[11] + X[15], % Y=X+Z, |
|||
% The hints |
|||
X[1] #= 151, |
|||
X[4] #= 40, |
|||
X[12] #= 11, |
|||
X[14] #= 4, |
|||
solve(X), |
|||
println([x=X[11],y=X[13],z=X[15]]), |
|||
fail, |
|||
nl.</lang> |
|||
{{out}} |
|||
<pre>[x = 5,y = 13,z = 8]</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |