4-rings or 4-squares puzzle: Difference between revisions

no edit summary
(Added Rust implementation)
imported>Chinhouse
No edit summary
 
(98 intermediate revisions by 43 users not shown)
Line 1:
[[Category:Games]]
[[Category:Puzzles]]
 
{{task}}
<!-- Squares were chosen for the diagram as it's much easier to display squares instead of rings. !-->
 
;Task:
Line 42 ⟶ 43:
* [[Solve the no connection puzzle]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F foursquares(lo, hi, unique, show)
V solutions = 0
L(c) lo .. hi
L(d) lo .. hi
I !unique | (c != d)
V a = c + d
I a >= lo & a <= hi
I !unique | (c != 0 & d != 0)
L(e) lo .. hi
I !unique | (e !C (a, c, d))
V g = d + e
I g >= lo & g <= hi
I !unique | (g !C (a, c, d, e))
L(f) lo .. hi
I !unique | (f !C (a, c, d, g, e))
V b = e + f - c
I b >= lo & b <= hi
I !unique | (b !C (a, c, d, g, e, f))
solutions++
I show
print(String((a, b, c, d, e, f, g))[1 .< (len)-1])
 
V uorn = I unique {‘unique’} E ‘non-unique’
 
print(solutions‘ ’uorn‘ solutions in ’lo‘ to ’hi)
print()
 
foursquares(1, 7, 1B, 1B)
foursquares(3, 9, 1B, 1B)
foursquares(0, 9, 0B, 0B)</syntaxhighlight>
 
{{out}}
<pre>
4, 7, 1, 3, 2, 6, 5
6, 4, 1, 5, 2, 3, 7
3, 7, 2, 1, 5, 4, 6
5, 6, 2, 3, 1, 7, 4
7, 3, 2, 5, 1, 4, 6
4, 5, 3, 1, 6, 2, 7
6, 4, 5, 1, 2, 7, 3
7, 2, 6, 1, 3, 5, 4
8 unique solutions in 1 to 7
 
7, 8, 3, 4, 5, 6, 9
8, 7, 3, 5, 4, 6, 9
9, 6, 4, 5, 3, 7, 8
9, 6, 5, 4, 3, 8, 7
4 unique solutions in 3 to 9
 
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program square4_64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ NBBOX, 7
 
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessDeb: .asciz "a= @ b= @ c= @ d= @ e= @ f= @ g= @ \n***********************\n"
szCarriageReturn: .asciz "\n************************\n"
sMessNbSolution: .asciz "Number of solutions : @ \n\n\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 8
sZoneConv: .skip 24
qValues_a: .skip 8 * NBBOX
qValues_b: .skip 8 * NBBOX - 1
qValues_c: .skip 8 * NBBOX - 2
qValues_d: .skip 8 * NBBOX - 3
qValues_e: .skip 8 * NBBOX - 4
qValues_f: .skip 8 * NBBOX - 5
qValues_g: .skip 8 * NBBOX - 6
qCounterSol: .skip 8
 
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x0,#1
mov x1,#7
mov x2,#3 // 0 = rien 1 = display 2 = count 3 = les deux
bl searchPb
mov x0,#3
mov x1,#9
mov x2,#3 // 0 = rien 1 = display 2 = count 3 = les deux
bl searchPb
mov x0,#0
mov x1,#9
mov x2,#2 // 0 = rien 1 = display 2 = count 3 = les deux
bl prepSearchNU
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
/******************************************************************/
/* search problèm value not unique */
/******************************************************************/
/* x0 contains start digit */
/* x1 contains end digit */
/* x2 contains action (0 display 1 count) */
prepSearchNU:
stp x12,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
stp x4,x5,[sp,-16]! // save registres
stp x6,x7,[sp,-16]! // save registres
stp x8,x9,[sp,-16]! // save registres
stp x10,fp,[sp,-16]! // save registres
mov x5,#0 // counter
mov x12,x0 // a
1:
mov x11,x0 // b
2:
mov x10,x0 // c
3:
mov x9,x0 // d
4:
add x4,x12,x11 // a + b reference
add x3,x11,x10
add x3,x3,x9 // b + c + d
cmp x4,x3
bne 10f
mov x8,x0 // e
5:
mov x7,x0 // f
6:
add x3,x9,x8
add x3,x3,x7 // d + e + f
cmp x3,x4
bne 9f
mov x6,x0 // g
7:
add x3,x7,x6 // f + g
cmp x3,x4
bne 8f // not OK
// OK
add x5,x5,1 // increment counter
8:
add x6,x6,1 // increment g
cmp x6,x1
ble 7b
9:
add x7,x7,1 // increment f
cmp x7,x1
ble 6b
add x8,x8,1 // increment e
cmp x8,x1
ble 5b
10:
add x9,x9,1 // increment d
cmp x9,x1
ble 4b
add x10,x10,1 // increment c
cmp x10,x1
ble 3b
add x11,x11,1 // increment b
cmp x11,x1
ble 2b
add x12,x12,1 // increment a
cmp x12,x1
ble 1b
// end
tst x2,#0b10 // print count ?
beq 100f
mov x0,x5 // counter
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsMessNbSolution
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
bl affichageMess
 
100:
 
ldp x10,fp,[sp],16 // restaur des 2 registres
ldp x8,x9,[sp],16 // restaur des 2 registres
ldp x6,x7,[sp],16 // restaur des 2 registres
ldp x4,x5,[sp],16 // restaur des 2 registres
ldp x2,x3,[sp],16 // restaur des 2 registres
ldp x12,lr,[sp],16 // restaur des 2 registres
ret
//qAdrsMessCounter: .quad sMessCounter
qAdrsMessNbSolution: .quad sMessNbSolution
qAdrsZoneConv: .quad sZoneConv
/******************************************************************/
/* search problem unique solution */
/******************************************************************/
/* x0 contains start digit */
/* x1 contains end digit */
/* x2 contains action (0 display 1 count) */
searchPb:
stp x12,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
stp x4,x5,[sp,-16]! // save registres
stp x6,x7,[sp,-16]! // save registres
stp x8,x9,[sp,-16]! // save registres
stp x10,fp,[sp,-16]! // save registres
mov x14,x2 // save action
// init
ldr x3,qAdrqValues_a // area value a
mov x4,#0
1: // loop init value a
str x0,[x3,x4,lsl #3]
add x4,x4,1
add x0,x0,1
cmp x0,x1
ble 1b
mov x5,#0 // solution counter
mov x12,#-1
2:
add x12,x12,1 // increment indice a
cmp x12,#NBBOX-1
bgt 90f
ldr x0,qAdrqValues_a // area value a
ldr x1,qAdrqValues_b // area value b
mov x2,x12 // indice a
mov x3,#NBBOX // number of origin values
bl prepValues
mov x11,#-1
3:
add x11,x11,1 // increment indice b
cmp x11,#NBBOX - 2
bgt 2b
ldr x0,qAdrqValues_b // area value b
ldr x1,qAdrqValues_c // area value c
mov x2,x11 // indice b
mov x3,#NBBOX -1 // number of origin values
bl prepValues
mov x10,#-1
4:
add x10,x10,1
cmp x10,#NBBOX - 3
bgt 3b
ldr x0,qAdrqValues_c
ldr x1,qAdrqValues_d
mov x2,x10
mov x3,#NBBOX - 2
bl prepValues
mov x9,#-1
5:
add x9,x9,1
cmp x9,#NBBOX - 4
bgt 4b
// control 2 firsts squares
ldr x0,qAdrqValues_a
ldr x0,[x0,x12,lsl #3]
ldr x1,qAdrqValues_b
ldr x1,[x1,x11,lsl #3]
add x4,x0,x1 // a + b value first square
ldr x0,qAdrqValues_c
ldr x0,[x0,x10,lsl #3]
add x7,x1,x0 // b + c
ldr x1,qAdrqValues_d
ldr x1,[x1,x9,lsl #3]
add x7,x7,x1 // b + c + d
cmp x7,x4 // equal first square ?
bne 5b
ldr x0,qAdrqValues_d
ldr x1,qAdrqValues_e
mov x2,x9
mov x3,#NBBOX - 3
bl prepValues
mov x8,#-1
6:
add x8,x8,1
cmp x8,#NBBOX - 5
bgt 5b
ldr x0,qAdrqValues_e
ldr x1,qAdrqValues_f
mov x2,x8
mov x3,#NBBOX - 4
bl prepValues
mov x7,#-1
7:
add x7,x7,1
cmp x7,#NBBOX - 6
bgt 6b
ldr x0,qAdrqValues_d
ldr x0,[x0,x9,lsl #3]
ldr x1,qAdrqValues_e
ldr x1,[x1,x8,lsl #3]
add x3,x0,x1 // d + e
ldr x1,qAdrqValues_f
ldr x1,[x1,x7,lsl #3]
add x3,x3,x1 // d + e + f
cmp x3,x4 // equal first square ?
bne 7b
ldr x0,qAdrqValues_f
ldr x1,qAdrqValues_g
mov x2,x7
mov x3,#NBBOX - 5
bl prepValues
mov x6,#-1
8:
add x6,x6,1
cmp x6,#NBBOX - 7
bgt 7b
ldr x0,qAdrqValues_f
ldr x0,[x0,x7,lsl #3]
ldr x1,qAdrqValues_g
ldr x1,[x1,x6,lsl #3]
add x3,x0,x1 // f +g
cmp x4,x3 // equal first square ?
bne 8b
add x5,x5,1 // increment counter
tst x14,#0b1
beq 9f // display solution ?
ldr x0,qAdrqValues_a
ldr x0,[x0,x12,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsMessDeb
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x2,x0
ldr x0,qAdrqValues_b
ldr x0,[x0,x11,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
mov x0,x2
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x2,x0
ldr x0,qAdrqValues_c
ldr x0,[x0,x10,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
mov x0,x2
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x2,x0
ldr x0,qAdrqValues_d
ldr x0,[x0,x9,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
mov x0,x2
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x2,x0
ldr x0,qAdrqValues_e
ldr x0,[x0,x8,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
mov x0,x2
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x2,x0
ldr x0,qAdrqValues_f
ldr x0,[x0,x7,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
mov x0,x2
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x2,x0
ldr x0,qAdrqValues_g
ldr x0,[x0,x6,lsl #3]
ldr x1,qAdrsZoneConv
bl conversion10
mov x0,x2
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
bl affichageMess
9:
b 8b // suite
90:
tst x14,#0b10
beq 100f // display counter ?
mov x0,x5
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsMessNbSolution
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
bl affichageMess
100:
ldp x10,fp,[sp],16 // restaur des 2 registres
ldp x8,x9,[sp],16 // restaur des 2 registres
ldp x6,x7,[sp],16 // restaur des 2 registres
ldp x4,x5,[sp],16 // restaur des 2 registres
ldp x2,x3,[sp],16 // restaur des 2 registres
ldp x12,lr,[sp],16 // restaur des 2 registres
ret
qAdrqValues_a: .quad qValues_a
qAdrqValues_b: .quad qValues_b
qAdrqValues_c: .quad qValues_c
qAdrqValues_d: .quad qValues_d
qAdrqValues_e: .quad qValues_e
qAdrqValues_f: .quad qValues_f
qAdrqValues_g: .quad qValues_g
qAdrsMessDeb: .quad sMessDeb
qAdrqCounterSol: .quad qCounterSol
/******************************************************************/
/* copy value area and substract value of indice */
/******************************************************************/
/* x0 contains the address of values origin */
/* x1 contains the address of values destination */
/* x2 contains value indice to substract */
/* x3 contains origin values number */
prepValues:
stp x1,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
stp x4,x5,[sp,-16]! // save registres
stp x6,x7,[sp,-16]! // save registres
mov x4,#0 // indice origin value
mov x5,#0 // indice destination value
1:
cmp x4,x2 // substract indice ?
beq 2f // yes -> jump
ldr x6,[x0,x4,lsl #3] // no -> copy value
str x6,[x1,x5,lsl #3]
add x5,x5,1 // increment destination indice
2:
add x4,x4,1 // increment origin indice
cmp x4,x3 // end ?
blt 1b
100:
ldp x6,x7,[sp],16 // restaur des 2 registres
ldp x4,x5,[sp],16 // restaur des 2 registres
ldp x2,x3,[sp],16 // restaur des 2 registres
ldp x1,lr,[sp],16 // restaur des 2 registres
ret
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{out}}
<pre>
a= 3 b= 7 c= 2 d= 1 e= 5 f= 4 g= 6
***********************
a= 4 b= 5 c= 3 d= 1 e= 6 f= 2 g= 7
***********************
a= 4 b= 7 c= 1 d= 3 e= 2 f= 6 g= 5
***********************
a= 5 b= 6 c= 2 d= 3 e= 1 f= 7 g= 4
***********************
a= 6 b= 4 c= 1 d= 5 e= 2 f= 3 g= 7
***********************
a= 6 b= 4 c= 5 d= 1 e= 2 f= 7 g= 3
***********************
a= 7 b= 2 c= 6 d= 1 e= 3 f= 5 g= 4
***********************
a= 7 b= 3 c= 2 d= 5 e= 1 f= 4 g= 6
***********************
Number of solutions : 8
 
 
a= 7 b= 8 c= 3 d= 4 e= 5 f= 6 g= 9
***********************
a= 8 b= 7 c= 3 d= 5 e= 4 f= 6 g= 9
***********************
a= 9 b= 6 c= 4 d= 5 e= 3 f= 7 g= 8
***********************
a= 9 b= 6 c= 5 d= 4 e= 3 f= 8 g= 7
***********************
Number of solutions : 4
 
 
Number of solutions : 2860
</pre>
 
=={{header|Action!}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="action!">
;;; solve the 4 rings or 4 squares puzzle
 
DEFINE TRUE = "1", FALSE = "0"
 
;;; finds solutions to the equations:
;;; a + b = b + c + d = d + e + f = f + g
;;; where a, b, c, d, e, f, g in lo : hi ( not necessarily unique )
;;; depending on show, the solutions will be printed or not
PROC fourRings( INT lo, hi BYTE allowDuplicates, show )
INT solutions, t, a, b, c, d, e, f, g, uniqueOrNot
solutions = 0
FOR a = lo TO hi DO
FOR b = lo TO hi DO
IF allowDuplicates OR a <> b THEN
t = a + b
FOR c = lo TO hi DO
IF allowDuplicates OR ( a <> c AND b <> c ) THEN
d = t - ( b + c )
IF d >= lo AND d <= hi
AND ( allowDuplicates OR ( a <> d AND b <> d AND c <> d ) )
THEN
FOR e = lo TO hi DO
IF allowDuplicates
OR ( a <> e AND b <> e AND c <> e AND d <> e )
THEN
g = d + e
f = t - g
IF f >= lo AND f <= hi
AND g >= lo AND g <= hi
AND ( allowDuplicates
OR ( a <> f AND b <> f AND c <> f
AND d <> f AND e <> f
AND a <> g AND b <> g AND c <> g
AND d <> g AND e <> g AND f <> g
)
)
THEN
solutions ==+ 1
IF show THEN
PrintF( " %U %U %U %U", a, b, c, d )
PrintF( " %U %U %U%E", e, f, g )
FI
FI
FI
OD
FI
FI
OD
FI
OD
OD
IF allowDuplicates
THEN uniqueOrNot = "non-unique"
ELSE uniqueOrNot = "unique"
FI
PrintF( "%U %S solutions in %U to %U%E%E", solutions, uniqueOrNot, lo, hi )
RETURN
 
;;; find the solutions as required for the task
PROC Main()
fourRings( 1, 7, FALSE, TRUE )
fourRings( 3, 9, FALSE, TRUE )
fourRings( 0, 9, TRUE, FALSE )
RETURN
</syntaxhighlight>
{{out}}
<pre>
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
8 unique solutions in 1 to 7
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
4 unique solutions in 3 to 9
 
2860 non-unique solutions in 0 to 9
 
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO;
 
procedure Puzzle_Square_4 is
 
procedure Four_Rings (Low, High : in Natural; Unique, Show : in Boolean) is
subtype Test_Range is Natural range Low .. High;
 
type Value_List is array (Positive range <>) of Natural;
function Is_Unique (Values : Value_List) return Boolean is
Count : array (Test_Range) of Natural := (others => 0);
begin
for Value of Values loop
Count (Value) := Count (Value) + 1;
if Count (Value) > 1 then
return False;
end if;
end loop;
return True;
end Is_Unique;
 
function Is_Valid (A,B,C,D,E,F,G : in Natural) return Boolean is
Ring_1 : constant Integer := A + B;
Ring_2 : constant Integer := B + C + D;
Ring_3 : constant Integer := D + E + F;
Ring_4 : constant Integer := F + G;
begin
return
Ring_1 = Ring_2 and
Ring_1 = Ring_3 and
Ring_1 = Ring_4;
end Is_Valid;
 
use Ada.Text_IO;
Count : Natural := 0;
begin
for A in Test_Range loop
for B in Test_Range loop
for C in Test_Range loop
for D in Test_Range loop
for E in Test_Range loop
for F in Test_Range loop
for G in Test_Range loop
if Is_Valid (A,B,C,D,E,F,G) then
if not Unique or (Unique and Is_Unique ((A,B,C,D,E,F,G))) then
Count := Count + 1;
if Show then
Put_Line (A'Image & B'Image & C'Image & D'Image & E'Image & F'Image & G'Image);
end if;
end if;
end if;
end loop;
end loop;
end loop;
end loop;
end loop;
end loop;
end loop;
Put_Line ("There are " & Count'Image &
(if Unique then " unique " else " non-unique ") &
"solutions in " & Low'Image & " .." & High'Image);
New_Line;
end Four_Rings;
 
begin
Four_Rings (Low => 1, High => 7, Unique => True, Show => True);
Four_Rings (Low => 3, High => 9, Unique => True, Show => True);
Four_Rings (Low => 0, High => 9, Unique => False, Show => False);
end Puzzle_Square_4;</syntaxhighlight>
 
{{out}}
<pre> 3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
There are 8 unique solutions in 1 .. 7
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
There are 4 unique solutions in 3 .. 9
 
There are 2860 non-unique solutions in 0 .. 9
</pre>
 
=={{header|ALGOL 68}}==
As with the REXX solution, we use explicit loops to generate the permutations.
<langsyntaxhighlight lang="algol68">BEGIN
# solve the 4 rings or 4 squares puzzle #
# we need to find solutions to the equations: a + b = b + c + d = d + e + f = f + g #
# where a, b, c, d, e, f, g in lo : hi ( not necessarily unique ) #
# depending on show, the solutions will be printed or not #
PROC four rings = ( INT lo, hi, BOOL uniqueallow duplicates, show )VOID:
BEGIN
INT solutions := 0;
BOOL allow duplicates = NOT unique;
# calculate field width for printinhg solutions #
INT width := -1;
Line 68 ⟶ 744:
FOR c FROM lo TO hi DO
IF allow duplicates OR ( a /= c AND b /= c ) THEN
FORINT d FROM= lot TO- hi( DOb + c );
IF allow duplicatesd OR ( a />= dlo AND b /= d AND c /<= d )hi
AND ( allow THENduplicates
OR ( a /= IFd AND b +/= cd +AND dc /= td THEN)
FOR e FROM lo TO hi DO)
IF allow duplicatesTHEN
OR ( a /=FOR e ANDFROM blo /=TO e AND c /= e AND d /= ehi )DO
IF allow THENduplicates
OR ( a /= e AND b /= e AND c /= FORe fAND FROMd lo/= TOe hi DO)
IF allow duplicatesTHEN
INT g OR ( a /= f AND b /= f AND c /= f AND d /= f AND+ e /= f );
INT f = t - THENg;
IF f >= lo IF d + e +AND f <= t THENhi
AND g >= lo FORAND g FROM lo TO<= hi DO
AND ( IF allow duplicates
OR ( OR ( a /= gf AND b /= gf AND c /= g AND d /= g AND e /= g AND f /= g )
AND d /= f AND e /= THENf
AND a /= g AND b /= g AND c IF f +/= g = t THEN
AND d /= g AND e /= g AND f solutions +:/= 1;g
IF show THEN)
print( ( whole( a, width ), whole( b, width )
, whole( c, width ), whole( d, width )THEN
solutions +:= , whole( e, width ), whole( f, width )1;
IF show , whole( g, width ), newlineTHEN
print( ( whole( a, width ), whole( b, width )
, whole( c, width ), whole( d, width )
, whole( e, width ), whole( f, width FI)
, whole( g, width ), FInewline
FI)
OD # g #)
FI
FI
OD # f #
FI
OD # e #FI
FI
FIOD # e #
OD # d #FI
FI
OD # c #
Line 112 ⟶ 785:
OD # a # ;
print( ( whole( solutions, 0 )
, IF uniqueallow duplicates THEN " non-unique" ELSE " non-unique" FI
, " solutions in "
, whole( lo, 0 )
Line 124 ⟶ 797:
 
# find the solutions as required for the task #
four rings( 1, 7, TRUEFALSE, TRUE );
four rings( 3, 9, TRUEFALSE, TRUE );
four rings( 0, 9, FALSETRUE, FALSE )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 147 ⟶ 820:
 
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="ada">begin % -- solve the 4 rings or 4 squares puzzle i.e., find solutions to the %
% -- equations: a + b = b + c + d = d + e + f = f + g %
% -- where a, b, c, d, e, f, g in lo : hi ( not necessarily unique ) %
% -- depending on show, the solutions will be printed or not %
procedure fourRings ( integer value lo, hi; logical value allowDuplicates, show ) ;
begin
integer solutions, width, maxLimit;
solutions := 0;
% -- calculate field width for printinhg solutions %
width := 1;
maxLimit := abs ( if abs lo > abs hi then lo else hi );
while maxLimit > 0 do begin
width := width + 1;
maxLimit := maxLimit div 10
end while_maxLimit_gt_0 ;
% -- find solutions %
for a := lo until hi do begin
for b := lo until hi do begin
if allowduplicates or a not = b then begin
integer t;
t := a + b;
for c := lo until hi do begin
if allowDuplicates
or ( a not = c and b not = c )
then begin
integer d;
d := t - ( b + c );
if d >= lo and d <= hi
and ( allowduplicates
or ( a not = d and b not = d and c not = d )
)
then begin
for e := lo until hi do begin
if allowDuplicates
or ( a not = e and b not = e and c not = e and d not = e )
then begin
integer f, g;
g := d + e;
f := t - g;
if f >= lo and f <= hi
and g >= lo and g <= hi
and ( allowDuplicates
or ( a not = f and b not = f and c not = f
and d not = f and e not = f
and a not = g and b not = g and c not = g
and d not = g and e not = g and f not = g
)
)
then begin
solutions := solutions + 1;
if show then write( i_w := width, s_w := 0, a, b, c, d, e, f, g )
end
end
end for_e
end
end
end for_c
end
end for_b
end for_a ;
write( i_w := 1, s_w := 0, solutions, if allowDuplicates then " non-unique" else " unique", " solutions in ", lo, " to ", hi );
write()
end % -- fourRings % ;
 
% -- find the solutions as required for the task %
fourRings( 1, 7, false, true );
fourRings( 3, 9, false, true );
fourRings( 0, 9, true, false )
end.</syntaxhighlight>
{{out}}
<pre>
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
8 unique solutions in 1 to 7
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
4 unique solutions in 3 to 9
 
2860 non-unique solutions in 0 to 9
 
</pre>
 
Line 153 ⟶ 919:
{{Trans|Haskell}}
(Structured search example)
<langsyntaxhighlight lang="applescript">use framework "Foundation" -- for basic NSArray sort
 
on run
Line 459 ⟶ 1,225:
on unlines(xs)
intercalate(linefeed, xs)
end unlines</langsyntaxhighlight>
{{Out}}
<pre>rings(true, enumFromTo(1, 7))
Line 482 ⟶ 1,248:
 
2860</pre>
 
=={{header|Applesoft BASIC}}==
{{trans|C}}
<syntaxhighlight lang="gwbasic"> 100 TRUE = NOT FALSE
110 PLO = 1:PHI = 7:PUNIQUE = TRUE:PSHOW = TRUE: GOSUB 150"FOURSQUARES"
120 PLO = 3:PHI = 9:PUNIQUE = TRUE:PSHOW = TRUE: GOSUB 150"FOURSQUARES"
130 PLO = 0:PHI = 9:PUNIQUE = FALSE:PSHOW = FALSE: GOSUB 150"FOURSQUARES"
140 END
150 LO = PLO
160 HI = PHI
170 UNIQUE = PUNIQUE
180 SHOW = PSHOW
190 S = 0: REM SOLUTIONS
200 PRINT
210 GOSUB 270"ACD"
220 PRINT
230 PRINT S" ";
240 IF NOT UNIQUE THEN PRINT "NON-";
250 PRINT "UNIQUE SOLUTIONS IN "LO" TO "HI
260 RETURN
270 FOR C = LO TO HI
280 FOR D = LO TO HI
290 IF ( NOT UNIQUE) OR (C < > D) THEN A = C + D: IF (A > = LO) AND (A < = HI) AND (( NOT UNIQUE) OR ((C < > 0) AND (D < > 0))) THEN GOSUB 320"GE"
300 NEXT D,C
310 RETURN
320 FOR E = LO TO HI
330 IF ( NOT UNIQUE) OR ((E < > A) AND (E < > C) AND (E < > D)) THEN G = D + E: IF (G > = LO) AND (G < = HI) AND (( NOT UNIQUE) OR ((G < > A) AND (G < > C) AND (G < > D) AND (G < > E))) THEN GOSUB 360"BF"
340 NEXT E
350 RETURN
360 FOR F = LO TO HI
370 IF (( NOT UNIQUE) OR ((F < > A) AND (F < > C) AND (F < > D) AND (F < > G) AND (F < > E))) THEN GOSUB 400
380 NEXT F
390 RETURN
400 B = E + F - C: IF ((B > = LO) AND (B < = HI) AND (( NOT UNIQUE) OR ((B < > A) AND (B < > C) AND (B < > D) AND (B < > G) AND (B < > E) AND (B < > F)))) THEN S = S + 1: IF (SHOW) THEN PRINT A" "B" "C" "D" "E" "F" "G
410 RETURN</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
 
/* ARM assembly Raspberry PI */
/* program square4.s */
/************************************/
/* Constantes */
/************************************/
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
 
.equ NBBOX, 7
 
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessDeb: .ascii "a="
sMessValeur_a: .fill 11, 1, ' ' @ size => 11
.ascii "b="
sMessValeur_b: .fill 11, 1, ' ' @ size => 11
.ascii "c="
sMessValeur_c: .fill 11, 1, ' ' @ size => 11
.ascii "d="
sMessValeur_d: .fill 11, 1, ' ' @ size => 11
.ascii "\n"
.ascii "e="
sMessValeur_e: .fill 11, 1, ' ' @ size => 11
.ascii "f="
sMessValeur_f: .fill 11, 1, ' ' @ size => 11
.ascii "g="
sMessValeur_g: .fill 11, 1, ' ' @ size => 11
 
szCarriageReturn: .asciz "\n************************\n"
 
sMessNbSolution: .ascii "Number of solutions :"
sMessCounter: .fill 11, 1, ' ' @ size => 11
.asciz "\n\n\n"
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
.align 4
iValues_a: .skip 4 * NBBOX
iValues_b: .skip 4 * NBBOX - 1
iValues_c: .skip 4 * NBBOX - 2
iValues_d: .skip 4 * NBBOX - 3
iValues_e: .skip 4 * NBBOX - 4
iValues_f: .skip 4 * NBBOX - 5
iValues_g: .skip 4 * NBBOX - 6
iCounterSol: .skip 4
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
mov r0,#1
mov r1,#7
mov r2,#3 @ 0 = rien 1 = display 2 = count 3 = les deux
bl searchPb
mov r0,#3
mov r1,#9
mov r2,#3 @ 0 = rien 1 = display 2 = count 3 = les deux
bl searchPb
mov r0,#0
mov r1,#9
mov r2,#2 @ 0 = rien 1 = display 2 = count 3 = les deux
bl prepSearchNU
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
 
/******************************************************************/
/* search problèm value not unique */
/******************************************************************/
/* r0 contains start digit */
/* r1 contains end digit */
/* r2 contains action (0 display 1 count) */
prepSearchNU:
push {r3-r12,lr} @ save registers
mov r5,#0 @ counter
mov r12,r0 @ a
1:
mov r11,r0 @ b
2:
mov r10,r0 @ c
3:
mov r9,r0 @ d
4:
add r4,r12,r11 @ a + b reference
add r3,r11,r10
add r3,r9 @ b + c + d
cmp r4,r3
bne 10f
mov r8,r0 @ e
5:
mov r7,r0 @ f
6:
add r3,r9,r8
add r3,r7 @ d + e + f
cmp r3,r4
bne 9f
mov r6,r0 @ g
7:
add r3,r7,r6 @ f + g
cmp r3,r4
bne 8f @ not OK
@ OK
add r5,#1 @ increment counter
 
8:
add r6,#1 @ increment g
cmp r6,r1
ble 7b
9:
add r7,#1 @ increment f
cmp r7,r1
ble 6b
add r8,#1 @ increment e
cmp r8,r1
ble 5b
10:
add r9,#1 @ increment d
cmp r9,r1
ble 4b
add r10,#1 @ increment c
cmp r10,r1
ble 3b
add r11,#1 @ increment b
cmp r11,r1
ble 2b
add r12,#1 @ increment a
cmp r12,r1
ble 1b
 
@ end
tst r2,#0b10 @ print count ?
beq 100f
mov r0,r5 @ counter
ldr r1,iAdrsMessCounter
bl conversion10
ldr r0,iAdrsMessNbSolution
bl affichageMess
 
100:
pop {r3-r12,lr} @ restaur registers
bx lr @return
iAdrsMessCounter: .int sMessCounter
iAdrsMessNbSolution: .int sMessNbSolution
 
/******************************************************************/
/* search problem unique solution */
/******************************************************************/
/* r0 contains start digit */
/* r1 contains end digit */
/* r2 contains action (0 display 1 count) */
searchPb:
push {r0-r12,lr} @ save registers
@ init
ldr r3,iAdriValues_a @ area value a
mov r4,#0
1: @ loop init value a
str r0,[r3,r4,lsl #2]
add r4,#1
add r0,#1
cmp r0,r1
ble 1b
 
mov r5,#0 @ solution counter
mov r12,#-1
2:
add r12,#1 @ increment indice a
cmp r12,#NBBOX-1
bgt 90f
ldr r0,iAdriValues_a @ area value a
ldr r1,iAdriValues_b @ area value b
mov r2,r12 @ indice a
mov r3,#NBBOX @ number of origin values
bl prepValues
mov r11,#-1
3:
add r11,#1 @ increment indice b
cmp r11,#NBBOX - 2
bgt 2b
ldr r0,iAdriValues_b @ area value b
ldr r1,iAdriValues_c @ area value c
mov r2,r11 @ indice b
mov r3,#NBBOX -1 @ number of origin values
bl prepValues
mov r10,#-1
4:
add r10,#1
cmp r10,#NBBOX - 3
bgt 3b
ldr r0,iAdriValues_c
ldr r1,iAdriValues_d
mov r2,r10
mov r3,#NBBOX - 2
bl prepValues
mov r9,#-1
5:
add r9,#1
cmp r9,#NBBOX - 4
bgt 4b
@ control 2 firsts squares
ldr r0,iAdriValues_a
ldr r0,[r0,r12,lsl #2]
ldr r1,iAdriValues_b
ldr r1,[r1,r11,lsl #2]
add r4,r0,r1 @ a + b value first square
ldr r0,iAdriValues_c
ldr r0,[r0,r10,lsl #2]
add r7,r1,r0 @ b + c
ldr r1,iAdriValues_d
ldr r1,[r1,r9,lsl #2]
add r7,r1 @ b + c + d
cmp r7,r4 @ equal first square ?
bne 5b
ldr r0,iAdriValues_d
ldr r1,iAdriValues_e
mov r2,r9
mov r3,#NBBOX - 3
bl prepValues
mov r8,#-1
6:
add r8,#1
cmp r8,#NBBOX - 5
bgt 5b
ldr r0,iAdriValues_e
ldr r1,iAdriValues_f
mov r2,r8
mov r3,#NBBOX - 4
bl prepValues
mov r7,#-1
7:
add r7,#1
cmp r7,#NBBOX - 6
bgt 6b
ldr r0,iAdriValues_d
ldr r0,[r0,r9,lsl #2]
ldr r1,iAdriValues_e
ldr r1,[r1,r8,lsl #2]
add r3,r0,r1 @ d + e
ldr r1,iAdriValues_f
ldr r1,[r1,r7,lsl #2]
add r3,r1 @ de + e + f
cmp r3,r4 @ equal first square ?
bne 7b
ldr r0,iAdriValues_f
ldr r1,iAdriValues_g
mov r2,r7
mov r3,#NBBOX - 5
bl prepValues
mov r6,#-1
8:
add r6,#1
cmp r6,#NBBOX - 7
bgt 7b
ldr r0,iAdriValues_f
ldr r0,[r0,r7,lsl #2]
ldr r1,iAdriValues_g
ldr r1,[r1,r6,lsl #2]
add r3,r0,r1 @ f +g
cmp r4,r3 @ equal first square ?
bne 8b
add r5,#1 @ increment counter
ldr r0,[sp,#8] @ load action for two parameter in stack
tst r0,#0b1
beq 9f @ display solution ?
ldr r0,iAdriValues_a
ldr r0,[r0,r12,lsl #2]
ldr r1,iAdrsMessValeur_a
bl conversion10
ldr r0,iAdriValues_b
ldr r0,[r0,r11,lsl #2]
ldr r1,iAdrsMessValeur_b
bl conversion10
ldr r0,iAdriValues_c
ldr r0,[r0,r10,lsl #2]
ldr r1,iAdrsMessValeur_c
bl conversion10
ldr r0,iAdriValues_d
ldr r0,[r0,r9,lsl #2]
ldr r1,iAdrsMessValeur_d
bl conversion10
ldr r0,iAdriValues_e
ldr r0,[r0,r8,lsl #2]
ldr r1,iAdrsMessValeur_e
bl conversion10
ldr r0,iAdriValues_f
ldr r0,[r0,r7,lsl #2]
ldr r1,iAdrsMessValeur_f
bl conversion10
ldr r0,iAdriValues_g
ldr r0,[r0,r6,lsl #2]
ldr r1,iAdrsMessValeur_g
bl conversion10
ldr r0,iAdrsMessDeb
bl affichageMess
9:
b 8b @ suite
 
90:
ldr r0,[sp,#8] @ load action for two parameter in stack
tst r0,#0b10
beq 100f @ display counter ?
mov r0,r5
ldr r1,iAdrsMessCounter
bl conversion10
ldr r0,iAdrsMessNbSolution
bl affichageMess
100:
pop {r0-r12,lr} @ restaur registers
bx lr @return
iAdriValues_a: .int iValues_a
iAdriValues_b: .int iValues_b
iAdriValues_c: .int iValues_c
iAdriValues_d: .int iValues_d
iAdriValues_e: .int iValues_e
iAdriValues_f: .int iValues_f
iAdriValues_g: .int iValues_g
 
iAdrsMessValeur_a: .int sMessValeur_a
iAdrsMessValeur_b: .int sMessValeur_b
iAdrsMessValeur_c: .int sMessValeur_c
iAdrsMessValeur_d: .int sMessValeur_d
iAdrsMessValeur_e: .int sMessValeur_e
iAdrsMessValeur_f: .int sMessValeur_f
iAdrsMessValeur_g: .int sMessValeur_g
iAdrsMessDeb: .int sMessDeb
iAdriCounterSol: .int iCounterSol
/******************************************************************/
/* copy value area and substract value of indice */
/******************************************************************/
/* r0 contains the address of values origin */
/* r1 contains the address of values destination */
/* r2 contains value indice to substract */
/* r3 contains origin values number */
prepValues:
push {r1-r6,lr} @ save registres
mov r4,#0 @ indice origin value
mov r5,#0 @ indice destination value
1:
cmp r4,r2 @ substract indice ?
beq 2f @ yes -> jump
ldr r6,[r0,r4,lsl #2] @ no -> copy value
str r6,[r1,r5,lsl #2]
add r5,#1 @ increment destination indice
2:
add r4,#1 @ increment origin indice
cmp r4,r3 @ end ?
blt 1b
100:
pop {r1-r6,lr} @ restaur registres
bx lr @return
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
/******************************************************************/
/* Converting a register to a decimal unsigned */
/******************************************************************/
/* r0 contains value and r1 address area */
/* r0 return size of result (no zero final in area) */
/* area size => 11 bytes */
.equ LGZONECAL, 10
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#LGZONECAL
1: @ start loop
bl divisionpar10U @ unsigned r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
cmp r0,#0 @ stop if quotient = 0
subne r2,#1 @ else previous position
bne 1b @ and loop
@ and move digit from left of area
mov r4,#0
2:
ldrb r1,[r3,r2]
strb r1,[r3,r4]
add r2,#1
add r4,#1
cmp r2,#LGZONECAL
ble 2b
@ and move spaces in end on area
mov r0,r4 @ result length
mov r1,#' ' @ space
3:
strb r1,[r3,r4] @ store space in area
add r4,#1 @ next position
cmp r4,#LGZONECAL
ble 3b @ loop if r4 <= area size
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 unsigned */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10U:
push {r2,r3,r4, lr}
mov r4,r0 @ save value
ldr r3,iMagicNumber @ r3 <- magic_number raspberry 1 2
umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0)
mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
pop {r2,r3,r4,lr}
bx lr @ leave function
iMagicNumber: .int 0xCCCCCCCD
 
</syntaxhighlight>
{{out}}
 
<pre>
a=3 b=7 c=2 d=1
e=5 f=4 g=6
************************
a=4 b=5 c=3 d=1
e=6 f=2 g=7
************************
a=4 b=7 c=1 d=3
e=2 f=6 g=5
************************
a=5 b=6 c=2 d=3
e=1 f=7 g=4
************************
a=6 b=4 c=1 d=5
e=2 f=3 g=7
************************
a=6 b=4 c=5 d=1
e=2 f=7 g=3
************************
a=7 b=2 c=6 d=1
e=3 f=5 g=4
************************
a=7 b=3 c=2 d=5
e=1 f=4 g=6
************************
Number of solutions :8
 
a=7 b=8 c=3 d=4
e=5 f=6 g=9
************************
a=8 b=7 c=3 d=5
e=4 f=6 g=9
************************
a=9 b=6 c=4 d=5
e=3 f=7 g=8
************************
a=9 b=6 c=5 d=4
e=3 f=8 g=7
************************
Number of solutions :4
 
Number of solutions :2860
 
</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
rotina(min,max,unique)
{
global totalcount := 0
global totalunique := 0
global result := "min=" min " max=" max " unique=" unique "`n`n"
max := max - min + 1
loop %max%
{
a := min + A_Index - 1
loop %max%
{
b := min + A_Index - 1
loop %max%
{
c := min + A_Index - 1
loop %max%
{
d := min + A_Index - 1
loop %max%
{
e := min + A_Index - 1
loop %max%
{
f := min + A_Index - 1
loop %max%
{
g := min + A_Index - 1
sum := a+b
if (b+c+d = sum and d+e+f = sum and f+g = sum)
{
totalcount += 1
if (unique=0)
continue
if not (a=b or a=c or a=d or a=e or a=f or a=g or b=c or b=d or b=e or b=f or b=g or c=d or c=e or c=f or c=g or d=e or d=f or d=g or e=f or e=g or f=g)
{
result .= a " " b " " c " " d " " e " " f " " g "`n"
totalunique += 1
}
}
}
}
}
}
}
}
}
}
rotina(1,7,1)
MsgBox %result% `ntotal unique = %totalunique% `ntotalcount = %totalcount%
rotina(3,9,1)
MsgBox %result% `ntotal unique = %totalunique% `ntotalcount = %totalcount%
rotina(0,9,0)
MsgBox %result% `ntotalcount = %totalcount%
ExitApp
return
</syntaxhighlight>
 
{{Output}}
<pre>
min=1 max=7 unique=1
 
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
total unique = 8
totalcount = 497
---------------------------
OK
---------------------------
min=3 max=9 unique=1
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
total unique = 4
totalcount = 180
---------------------------
OK
---------------------------
min=0 max=9 unique=0
totalcount = 2860
---------------------------
OK
---------------------------
</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f 4-RINGS_OR_4-SQUARES_PUZZLE.AWK
# converted from C
BEGIN {
cmd = "SORT /+16"
four_squares(1,7,1,1)
four_squares(3,9,1,1)
four_squares(0,9,0,0)
four_squares(0,6,1,0)
four_squares(2,8,1,0)
exit(0)
}
function four_squares(plo,phi,punique,pshow) {
lo = plo
hi = phi
unique = punique
show = pshow
solutions = 0
print("")
if (show) {
print("A B C D E F G sum A+B B+C+D D+E+F F+G")
print("------------- --- -------------------")
}
acd()
close(cmd)
tmp = (unique) ? "unique" : "non-unique"
printf("%d-%d: %d %s solutions\n",lo,hi,solutions,tmp)
}
function acd() {
for (c=lo; c<=hi; c++) {
for (d=lo; d<=hi; d++) {
if (!unique || c != d) {
a = c + d
if (a >= lo && a <= hi && (!unique || (c != 0 && d != 0))) {
ge()
}
}
}
}
}
function bf() {
for (f=lo; f<=hi; f++) {
if (!unique || (f != a && f != c && f != d && f != g && f != e)) {
b = e + f - c
if (b >= lo && b <= hi && (!unique || (b != a && b != c && b != d && b != g && b != e && b != f))) {
solutions++
if (show) {
printf("%d %d %d %d %d %d %d %4d ",a,b,c,d,e,f,g,a+b) | cmd
printf("%d+%d ",a,b) | cmd
printf("%d+%d+%d ",b,c,d) | cmd
printf("%d+%d+%d ",d,e,f) | cmd
printf("%d+%d\n",f,g) | cmd
}
}
}
}
}
function ge() {
for (e=lo; e<=hi; e++) {
if (!unique || (e != a && e != c && e != d)) {
g = d + e
if (g >= lo && g <= hi && (!unique || (g != a && g != c && g != d && g != e))) {
bf()
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
A B C D E F G sum A+B B+C+D D+E+F F+G
------------- --- -------------------
4 5 3 1 6 2 7 9 4+5 5+3+1 1+6+2 2+7
7 2 6 1 3 5 4 9 7+2 2+6+1 1+3+5 5+4
3 7 2 1 5 4 6 10 3+7 7+2+1 1+5+4 4+6
6 4 1 5 2 3 7 10 6+4 4+1+5 5+2+3 3+7
6 4 5 1 2 7 3 10 6+4 4+5+1 1+2+7 7+3
7 3 2 5 1 4 6 10 7+3 3+2+5 5+1+4 4+6
4 7 1 3 2 6 5 11 4+7 7+1+3 3+2+6 6+5
5 6 2 3 1 7 4 11 5+6 6+2+3 3+1+7 7+4
1-7: 8 unique solutions
 
A B C D E F G sum A+B B+C+D D+E+F F+G
------------- --- -------------------
7 8 3 4 5 6 9 15 7+8 8+3+4 4+5+6 6+9
8 7 3 5 4 6 9 15 8+7 7+3+5 5+4+6 6+9
9 6 4 5 3 7 8 15 9+6 6+4+5 5+3+7 7+8
9 6 5 4 3 8 7 15 9+6 6+5+4 4+3+8 8+7
3-9: 4 unique solutions
 
0-9: 2860 non-unique solutions
 
0-6: 4 unique solutions
 
2-8: 8 unique solutions
</pre>
 
=={{header|BASIC256}}==
<syntaxhighlight lang="vb">call four_square(1, 7, TRUE, TRUE)
call four_square(3, 9, TRUE, TRUE)
call four_square(0, 9, FALSE, FALSE)
end
 
subroutine four_square(low, high, unique, show)
total = 0
 
if show then print " a b c d e f g" + chr(10) + " ============="
 
for a = low to high
for b = low to high
if unique and b = a then continue for
t = a + b
for c = low to high
if unique then
if c = a or c = b then continue for
end if
for d = low to high
if unique then
if d = a or d = b or d = c then continue for
end if
if b + c + d = t then
for e = low to high
if unique then
if e = a or e = b or e = c or e = d then continue for
end if
for f = low to high
if unique then
if f = a or f = b or f = c or f = d or f = e then continue for
end if
if d + e + f = t then
for g = low to high
if unique then
if g = a or g = b or g = c or g = d or g = e or g = f then continue for
end if
if f + g = t then
total += 1
if show then print " ";a;" ";b;" ";c;" ";d;" ";e;" ";f;" ";g
end if
next g
end if
next f
next e
end if
next d
next c
next b
next a
 
print
if unique then
print "There are ";total;" unique solutions in [";string(low);", ";string(high);"]"
else
print "There are ";total;" non-unique solutions in [";string(low);", ";string(high);"]"
end if
print
end subroutine</syntaxhighlight>
 
=={{header|Befunge}}==
This is loosely based on the [[4-rings_or_4-squares_puzzle#C|C]] algorithm, although many of the conditions have been combined to minimize branching. There is no option to choose whether the results are displayed or not - unique solutions are always displayed, and non-unique solutions just return the solution count.
 
<syntaxhighlight lang="befunge">550" :woL">:#,_&>00p" :hgiH">:#,_&>1+10p" :)n/y( euqinU">:#,_>~>:4v
v!g03!:\*`\g01\!`\g00:p05:+g03:p04:_$30g1+:10g\`v1g<,+$p02%2_|#`*8<
>>+\30g-!+20g*!*00g\#v_$40g1+:10g\`^<<1g00p03<<<_$55+:,\."snoitul"v
v!`\g00::p07:+g04p06:<^<`\g01:+1g06$<_v#!\g00*!*g02++!-g05< v"so"<
>\10g\`*\:::30g-!\40g-!+\50g-!+\60g-! +60g::30g-!\40g-!+\^ >:#,_@
>0g50g.......55+,0vg02+1_80g1+:10g\`!^>>:80p60g+30g-:90p::00g\`!>>v
^9g03g04g06g08g07<_>>0>>^<<*!*g02++!-g07\+!-g06\+!-g05\+!-g04\!-<<\
>>10g\`*\:::::30g-!\40g-!+\50g-!+\60g-!+\70g-!+\80g-!+80g::::30g^^></syntaxhighlight>
 
{{out}}
 
<pre>Low: 1
High: 7
Unique (y/n): y
 
4 7 1 3 2 6 5
6 4 1 5 2 3 7
3 7 2 1 5 4 6
5 6 2 3 1 7 4
7 3 2 5 1 4 6
4 5 3 1 6 2 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
 
8 solutions</pre>
 
<pre>Low: 3
High: 9
Unique (y/n): y
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
4 solutions</pre>
 
<pre>Low: 0
High: 9
Unique (y/n): n
 
 
2860 solutions</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
 
Line 568 ⟶ 2,159:
foursquares(0,9,FALSE,FALSE);
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 593 ⟶ 2,184:
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
using System.Linq;
 
namespace Four_Squares_Puzzle {
class Program {
static void Main(string[] args) {
fourSquare(1, 7, true, true);
fourSquare(3, 9, true, true);
fourSquare(0, 9, false, false);
}
 
private static void fourSquare(int low, int high, bool unique, bool print) {
int count = 0;
 
if (print) {
Console.WriteLine("a b c d e f g");
}
for (int a = low; a <= high; ++a) {
for (int b = low; b <= high; ++b) {
if (notValid(unique, b, a)) continue;
 
int fp = a + b;
for (int c = low; c <= high; ++c) {
if (notValid(unique, c, b, a)) continue;
for (int d = low; d <= high; ++d) {
if (notValid(unique, d, c, b, a)) continue;
if (fp != b + c + d) continue;
 
for (int e = low; e <= high; ++e) {
if (notValid(unique, e, d, c, b, a)) continue;
for (int f = low; f <= high; ++f) {
if (notValid(unique, f, e, d, c, b, a)) continue;
if (fp != d + e + f) continue;
 
for (int g = low; g <= high; ++g) {
if (notValid(unique, g, f, e, d, c, b, a)) continue;
if (fp != f + g) continue;
 
++count;
if (print) {
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6}", a, b, c, d, e, f, g);
}
}
}
}
}
}
}
}
if (unique) {
Console.WriteLine("There are {0} unique solutions in [{1}, {2}]", count, low, high);
}
else {
Console.WriteLine("There are {0} non-unique solutions in [{1}, {2}]", count, low, high);
}
}
 
private static bool notValid(bool unique, int needle, params int[] haystack) {
return unique && haystack.Any(p => p == needle);
}
}
}</syntaxhighlight>
{{out}}
<pre>a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
There are 8 unique solutions in [1, 7]
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
There are 4 unique solutions in [3, 9]
There are 2860 non-unique solutions in [0, 9]</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
//C++14/17
#include <algorithm>//std::for_each
Line 689 ⟶ 2,363:
return 0;
}
</syntaxhighlight>
</lang>
Output
<pre>
Line 710 ⟶ 2,384:
Number of combinations in range 0-9: 2860.
 
</pre>
 
=={{header|Chipmunk Basic}}==
{{works with|Chipmunk Basic|3.6.4}}
{{trans|Applesoft BASIC}}
<syntaxhighlight lang="qbasic">10 plo = 1 : phi = 7 : punique = true : pshow = true : gosub 50 : rem "FOURSQUARES"
20 plo = 3 : phi = 9 : punique = true : pshow = true : gosub 50 : rem "FOURSQUARES"
30 plo = 0 : phi = 9 : punique = false : pshow = false : gosub 50 : rem "FOURSQUARES"
40 end
50 lo = plo
60 hi = phi
70 unique = punique
80 show = pshow
90 s = 0 : rem SOLUTIONS
100 print
110 gosub 170 : rem "ACD"
120 print
130 print s " ";
140 if not unique then print "NON-";
150 print "UNIQUE SOLUTIONS IN " lo " TO " hi
160 return
170 for c = lo to hi
180 for d = lo to hi
190 if ( not unique) or (c <> d) then
200 a = c+d
210 if (a >= lo) and (a <= hi) and (( not unique) or ((c <> 0) and (d <> 0))) then gosub 250 : rem "GE"
220 endif
230 next d,c
240 return
250 for e = lo to hi
260 if ( not unique) or ((e <> a) and (e <> c) and (e <> d)) then
270 g = d+e
280 if (g >= lo) and (g <= hi) and (( not unique) or ((g <> a) and (g <> c) and (g <> d) and (g <> e))) then gosub 320 : rem "BF"
290 endif
300 next e
310 return
320 for f = lo to hi
330 if (( not unique) or ((f <> a) and (f <> c) and (f <> d) and (f <> g) and (f <> e))) then gosub 360
340 next f
350 return
360 b = e+f-c
370 if ((b >= lo) and (b <= hi) and (( not unique) or ((b <> a) and (b <> c) and (b <> d) and (b <> g) and (b <> e) and (b <> f)))) then
380 s = s+1
390 if (show) then print a " " b " " c " " d " " e " " f " " g
400 endif
410 return</syntaxhighlight>
{{out}}
<pre>>run
 
4 7 1 3 2 6 5
6 4 1 5 2 3 7
3 7 2 1 5 4 6
5 6 2 3 1 7 4
7 3 2 5 1 4 6
4 5 3 1 6 2 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
 
8 UNIQUE SOLUTIONS IN 1 TO 7
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
4 UNIQUE SOLUTIONS IN 3 TO 9
 
 
2860 NON-UNIQUE SOLUTIONS IN 0 TO 9</pre>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(use '[clojure.math.combinatorics]
 
(defn rings [r & {:keys [unique] :or {unique true}}]
(if unique
(apply concat (map permutations (combinations r 7)))
(selections r 7)))
 
(defn four-rings [low high & {:keys [unique] :or {unique true}}]
(for [[a b c d e f g] (rings (range low (inc high)) :unique unique)
:when (= (+ a b) (+ b c d) (+ d e f) (+ f g))] [a b c d e f g]))
</syntaxhighlight>
 
{{out}}
<pre>
=> (pprint (four-rings 1 7))
([3 7 2 1 5 4 6]
[4 5 3 1 6 2 7]
[4 7 1 3 2 6 5]
[5 6 2 3 1 7 4]
[6 4 1 5 2 3 7]
[6 4 5 1 2 7 3]
[7 2 6 1 3 5 4]
[7 3 2 5 1 4 6])
nil
 
=> (pprint (four-rings 3 9))
([7 8 3 4 5 6 9] [8 7 3 5 4 6 9] [9 6 4 5 3 7 8] [9 6 5 4 3 8 7])
nil
 
=> (count (four-rings 0 9 :unique false))
2860
</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defpackage four-rings
(:use common-lisp)
Line 749 ⟶ 2,525:
(format t "Number of solutions for Low 0, High 9 non-unique:~%~A~%"
(length (four-rings-solutions 0 9 nil)))))
</syntaxhighlight>
</lang>
Output:
<pre>
Line 775 ⟶ 2,551:
NIL
</pre>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">def check(list)
a, b, c, d, e, f, g = list
first = a + b
{b + c + d, d + e + f, f + g}.all? &.==(first)
end
 
def four_squares(low, high, unique = true, show = unique)
solutions = [] of Array(Int32)
if unique
uniq = "unique"
(low..high).to_a.each_permutation(7, true) { |ary| solutions << ary.clone if check(ary) }
else
uniq = "non-unique"
(low..high).to_a.each_repeated_permutation(7, true) { |ary| solutions << ary.clone if check(ary) }
end
if show
puts " " + ("a".."g").join(" ")
solutions.each { |ary| p ary }
end
puts "#{solutions.size} #{uniq} solutions in #{low} to #{high}"
puts
end
 
{ {1, 7}, {3, 9} }.each do |(low, high)|
four_squares(low, high)
end
four_squares(0, 9, false)</syntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">import std.stdio;
 
void main() {
Line 839 ⟶ 2,645:
}
return true;
}</langsyntaxhighlight>
 
{{out}}
Line 859 ⟶ 2,665:
There are 4 unique solutions in [3,9]
There are 2860 non-unique solutions in [0,9]</pre>
=={{header|Delphi}}==
See [[#Pascal]]
=={{header|EasyLang}}==
{{trans|AWK}}
 
<syntaxhighlight lang=easylang>
func ok v t[] .
for h in t[]
if v = h
return 0
.
.
return 1
.
proc four lo hi uni show . .
#
subr bf
for f = lo to hi
if uni = 0 or ok f [ a c d g e ] = 1
b = e + f - c
if b >= lo and b <= hi and (uni = 0 or ok b [ a c d g e f ] = 1)
solutions += 1
if show = 1
for h in [ a b c d e f g ]
write h & " "
.
print ""
.
.
.
.
.
subr ge
for e = lo to hi
if uni = 0 or ok e [ a c d ] = 1
g = d + e
if g >= lo and g <= hi and (uni = 0 or ok g [ a c d e ] = 1)
bf
.
.
.
.
subr acd
for c = lo to hi
for d = lo to hi
if uni = 0 or c <> d
a = c + d
if a >= lo and a <= hi and (uni = 0 or c <> 0 and d <> 0)
ge
.
.
.
.
.
print "low:" & lo & " hi:" & hi & " unique:" & uni
acd
print solutions & " solutions"
print ""
.
four 1 7 1 1
four 3 9 1 1
four 0 9 0 0
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
(* A simple function to generate the sequence
Nigel Galloway: January 31st., 2017 *)
Line 869 ⟶ 2,738:
seq{for a in n .. g do for b in n .. g do if (a+b) = x then for c in n .. g do if (b+c+d) = x then yield b} |> Seq.collect(fun b ->
seq{for f in n .. g do for G in n .. g do if (f+G) = x then for e in n .. g do if (f+e+d) = x then yield f} |> Seq.map(fun f -> {d=d;x=x;b=b;f=f}))))
</syntaxhighlight>
</lang>
Then:
<langsyntaxhighlight lang="fsharp">
printfn "%d" (Seq.length (N 0 9))
</syntaxhighlight>
</lang>
{{out}}
<pre>
2860
</pre>
<langsyntaxhighlight lang="fsharp">
(* A simple function to generate the sequence with unique values
Nigel Galloway: January 31st., 2017 *)
Line 886 ⟶ 2,755:
seq{for a in n .. g do if a <> d then for b in n .. g do if (a+b) = x && b <> a && b <> d then for c in n .. g do if (b+c+d) = x && c <> d && c <> a && c <> b then yield b} |> Seq.collect(fun b ->
seq{for f in n .. g do if f <> d && f <> b && f <> (x-b) && f <> (x-d-b) then for G in n .. g do if (f+G) = x && G <> d && G <> b && G <> f && G <> (x-b) && G <> (x-d-b) then for e in n .. g do if (f+e+d) = x && e <> d && e <> b && e <> f && e <> G && e <> (x-b) && e <> (x-d-b) then yield f} |> Seq.map(fun f -> {d=d;x=x;b=b;f=f}))))
</syntaxhighlight>
</lang>
Then:
<langsyntaxhighlight lang="fsharp">
for n in N 1 7 do printfn "%d,%d,%d,%d,%d,%d,%d" (n.x-n.b) n.b (n.x-n.d-n.b) n.d (n.x-n.d-n.f) n.f (n.x-n.f)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 903 ⟶ 2,772:
</pre>
and:
<langsyntaxhighlight lang="fsharp">
for n in N 3 9 do printfn "%d,%d,%d,%d,%d,%d,%d" (n.x-n.b) n.b (n.x-n.d-n.b) n.d (n.x-n.d-n.f) n.f (n.x-n.f)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 913 ⟶ 2,782:
9,6,4,5,3,7,8
</pre>
 
=={{header|Factor}}==
This solution uses the <code>backtrack</code> vocabulary — Factor's implementation of John McCarthy's ''[http://www.rosettacode.org/wiki/Amb ambiguous operator]''. In short, we define 7 integers that can take up any value within the range that we give it, such as [3,9], and assign them names a-g. We then test whether the four sums from the puzzle are equal, and if applicable, whether a-g are unique. We send this boolean value to <code>must-be-true</code> and if it's false, then the other possibilities will be explored through the power of continuations.
 
<code>bag-of</code> is a combinator (higher-order function) that yields <i>every</i> solution in a collection. If we had written <code>4-rings</code> without using <code>bag-of</code>, it would have returned only the first solution it found.
<syntaxhighlight lang="factor">USING: arrays backtrack formatting grouping kernel locals math
math.ranges prettyprint sequences sequences.generalizations
sets ;
IN: rosetta-code.4-rings
 
:: 4-rings ( lo hi unique? -- seq ) [
7 [ lo hi [a,b] amb-lazy ] replicate
7 firstn :> ( a b c d e f g )
{ a b c d e f g } :> p
a b +
b c d + +
d e f + +
f g +
4array all-equal?
unique? [ p all-unique? and ] when
must-be-true p
] bag-of ;
: report ( lo hi unique? -- )
3dup 4-rings over [ dup . ] when length swap "" "non-" ?
"In [%d, %d] there are %d %sunique solutions.\n" printf ;
1 7 t report
3 9 t report
0 9 f report</syntaxhighlight>
{{out}}
<pre>
V{
{ 3 7 2 1 5 4 6 }
{ 4 5 3 1 6 2 7 }
{ 4 7 1 3 2 6 5 }
{ 5 6 2 3 1 7 4 }
{ 6 4 1 5 2 3 7 }
{ 6 4 5 1 2 7 3 }
{ 7 2 6 1 3 5 4 }
{ 7 3 2 5 1 4 6 }
}
In [1, 7] there are 8 unique solutions.
V{
{ 7 8 3 4 5 6 9 }
{ 8 7 3 5 4 6 9 }
{ 9 6 4 5 3 7 8 }
{ 9 6 5 4 3 8 7 }
}
In [3, 9] there are 4 unique solutions.
In [0, 9] there are 2860 non-unique solutions.
</pre>
 
=={{header|Fortran}}==
This uses the facility standardised in F90 whereby DO-loops can have text labels attached (not in the usual label area) so that the END DO statement can have the corresponding label, and any CYCLE statements can use it also. Similarly, the subroutine's END statement bears the name of the subroutine. This is just syntactic decoration. Rather more useful is extended syntax for dealing with arrays and especially the function ANY for making multiple tests without having to enumerate them in the code. To gain this convenience, the EQUIVALENCE statement makes variables A, B, C, D, E, F, and G occupy the same storage as <code>INTEGER V(7)</code>, an array.
Line 918 ⟶ 2,840:
One could abandon the use of the named variables in favour of manipulating the array equivalent, and indeed develop code which performs the nested loops via messing with the array, but for simplicity, the individual variables are used. However, tempting though it is to write a systematic sequence of seven nested DO-loops, the variables are not in fact all independent: some are fixed once others are chosen. Just cycling through all the notional possibilities when one only is in fact possible is a bit too much brute-force-and-ignorance, though other problems with other constraints, may encourage such exhaustive stepping. As a result, the code is more tightly bound to the specific features of the problem.
 
Also standardised in F90 is the $ format code, which specifies that the output line is not to end with the WRITE statement. The problem here is that Fortran does not offer an IF ...FI bracketing construction inside an expression, that would allow something like <langsyntaxhighlight Fortranlang="fortran">WRITE(...) FIRST,LAST,IF (UNIQUE) THEN "Distinct values only" ELSE "Repeated values allowed" FI // "."</langsyntaxhighlight> so that the correct alternative will be selected. Further, an array (that would hold those two texts) can't be indexed by a LOGICAL variable, and playing with EQUIVALENCE won't help, because the numerical values revealed thereby for .TRUE. and .FALSE. may not be 1 and 0. And anyway, parameters are not allowed to be accessed via EQUIVALENCE to another variable.
 
So, a two-part output, and to reduce the blather, two IF-statements. <langsyntaxhighlight Fortranlang="fortran"> SUBROUTINE FOURSHOW(FIRST,LAST,UNIQUE) !The "Four Rings" or "Four Squares" puzzle.
Choose values such that A+B = B+C+D = D+E+F = F+G, all being integers in FIRST:LAST...
INTEGER FIRST,LAST !The range of allowed values.
Line 967 ⟶ 2,889:
CALL FOURSHOW(0,9,.FALSE.)
 
END </langsyntaxhighlight>
Output: not in a neat order because the first variable is not determined first.
<pre>
Line 1,043 ⟶ 2,965:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 18-03-2017
' compile with: fbc -s console
 
Line 1,133 ⟶ 3,055:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> a b c d e f g
Line 1,161 ⟶ 3,083:
2860 Non unique solutions for 0 to 9
----------------------------------------</pre>
 
 
 
=={{header|FutureBasic}}==
This simple example uses old-style, length-limited Pascal strings for formatting to make it easier to compare with similar code posted here for this task. However, FB more commonly uses Apple's modern and superior Core Foundation strings.
<syntaxhighlight lang="futurebasic">
local fn FourRings( low as long, high as long, unique as BOOL, show as BOOL )
long a, b, c, d, e, f, g
unsigned long t, total = 0
unsigned long l = len$( str$(high) )
if l < len$( str$(low) ) then l = len$( str$( low) )
if ( show == YES )
for a = 97 to 103
print space$(l); chr$(a);
next
print
print " "; string$( ( l + 1 ) * 7, "-" );
print
end if
for a = low to high
for b = low to high
if ( unique == YES )
if b == a then continue
end if
t = a + b
for c = low to high
if unique == YES
if c == a or c == b then continue
end if
for d = low to high
if unique == YES
if d == a or d == b or d == c then continue
end if
if b + c + d == t
for e = low to high
if unique == YES
if e == a or e == b or e == c or e == d then continue
end if
for f = low to high
if unique == YES
if f == a or f == b or f == c or f == d or f == e then continue
end if
if ( d + e + f == t )
for g = low to high
if unique == YES
if g == a or g == b or g == c or g == d or g == e or g == f then continue
end if
if ( f + g == t )
total += 1
if( show == YES )
printf @"%3d%3d%3d%3d%3d%3d%3d", a, b, c, d, e, f, g
end if
end if
next
end if
next
next
end if
next
next
next
next
if ( unique == YES )
print
print total; " unique solutions for"; str$(low); " to"; str$(high)
print string$(30, "-") : print
else
print total; " non-unique solutions for"; str$(low); " to"; str$(high)
print string$(36, "-") : print
end if
end fn
 
window 1, @"4 Rings", ( 0, 0, 350, 400 )
 
fn FourRings( 1, 7, YES, YES )
fn FourRings( 3, 9, YES, YES )
fn FourRings( 0, 9, NO, NO )
 
HandleEvents
</syntaxhighlight>
 
For interest, the following solution uses CoreFoundation (CF) strings.
<syntaxhighlight lang="futurebasic">local fn FourRings( low as long, high as long, unique as BOOL, show as BOOL )
long a, b, c, d, e, f, g
long t, total = 0
long l = len(str(high))
if ( l < len(str(low)) ) then l = len(str(low))
if ( show )
for a = 97 to 103
print space(l);fn StringWithCharacters( @a, 1 );
next
print
print @" ";fn StringByPaddingToLength( @"", ( l + 1 ) * 7, @"-", 0 )
end if
for a = low to high
for b = low to high
if ( unique )
if ( b == a ) then continue
end if
t = a + b
for c = low to high
if ( unique )
if ( c == a or c == b ) then continue
end if
for d = low to high
if ( unique )
if ( d == a or d == b or d == c ) then continue
end if
if ( b + c + d == t )
for e = low to high
if ( unique )
if ( e == a or e == b or e == c or e == d ) then continue
end if
for f = low to high
if ( unique )
if ( f == a or f == b or f == c or f == d or f == e ) then continue
end if
if ( d + e + f == t )
for g = low to high
if ( unique )
if ( g == a or g == b or g == c or g == d or g == e or g == f ) then continue
end if
if ( f + g == t )
total += 1
if ( show )
printf @"%3d%3d%3d%3d%3d%3d%3d", a, b, c, d, e, f, g
end if
end if
next
end if
next
next
end if
next
next
next
next
if ( unique )
print
print total;@" unique solutions for ";low;@" to ";high
print fn StringByPaddingToLength( @"", 30, @"-", 0 )
print
else
print total;@" non-unique solutions for ";low;@" to ";high
print fn StringByPaddingToLength( @"", 37, @"-", 0 )
print
end if
end fn
 
window 1, @"4 Rings", ( 0, 0, 350, 400 )
 
fn FourRings( 1, 7, YES, YES )
fn FourRings( 3, 9, YES, YES )
fn FourRings( 0, 9, NO, NO )
 
HandleEvents</syntaxhighlight>
 
{{output}}
<pre style="font-size: 13px">
a b c d e f g
---------------------
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
 
8 unique solutions for 1 to 7
------------------------------
 
a b c d e f g
---------------------
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
4 unique solutions for 3 to 9
------------------------------
 
2860 non-unique solutions for 0 to 9
------------------------------------
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,187 ⟶ 3,304:
for g := low; g <= high; g++ {
if validComb(a,b,c,d,e,f,g) {
if !unique || isUnique(a,b,c,d,e,f,g) {
if isUnique(a,b,c,d,e,f,g) {
num++
validCombs = append(validCombs,[]int{a,b,c,d,e,f,g})
}
}else{
num++
validCombs = append(validCombs,[]int{a,b,c,d,e,f,g})
Line 1,215 ⟶ 3,327:
data[f]++
data[g]++
ifreturn len(data) == 7 {
return true
}else {
return false
}
}
func validComb(a,b,c,d,e,f,g int) bool{
Line 1,228 ⟶ 3,336:
return square1 == square2 && square2 == square3 && square3 == square4
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,237 ⟶ 3,345:
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class FourRings {
static void main(String[] args) {
fourSquare(1, 7, true, true)
fourSquare(3, 9, true, true)
fourSquare(0, 9, false, false)
}
 
private static void fourSquare(int low, int high, boolean unique, boolean print) {
int count = 0
 
if (print) {
println("a b c d e f g")
}
for (int a = low; a <= high; ++a) {
for (int b = low; b <= high; ++b) {
if (notValid(unique, a, b)) continue
 
int fp = a + b
for (int c = low; c <= high; ++c) {
if (notValid(unique, c, a, b)) continue
for (int d = low; d <= high; ++d) {
if (notValid(unique, d, a, b, c)) continue
if (fp != b + c + d) continue
 
for (int e = low; e <= high; ++e) {
if (notValid(unique, e, a, b, c, d)) continue
for (int f = low; f <= high; ++f) {
if (notValid(unique, f, a, b, c, d, e)) continue
if (fp != d + e + f) continue
 
for (int g = low; g <= high; ++g) {
if (notValid(unique, g, a, b, c, d, e, f)) continue
if (fp != f + g) continue
 
++count
if (print) {
printf("%d %d %d %d %d %d %d%n", a, b, c, d, e, f, g)
}
}
}
}
}
}
}
}
if (unique) {
printf("There are %d unique solutions in [%d, %d]%n", count, low, high)
} else {
printf("There are %d non-unique solutions in [%d, %d]%n", count, low, high)
}
}
 
private static boolean notValid(boolean unique, int needle, int ... haystack) {
return unique && Arrays.stream(haystack).anyMatch({ p -> p == needle })
}
}</syntaxhighlight>
{{out}}
<pre>a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
There are 8 unique solutions in [1, 7]
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
There are 4 unique solutions in [3, 9]
There are 2860 non-unique solutions in [0, 9]</pre>
 
=={{header|Haskell}}==
====By exhaustive search====
<langsyntaxhighlight lang="haskell">import Data.List
import Control.Monad
 
Line 1,279 ⟶ 3,465:
fourRings 1 7 False True
fourRings 3 9 False True
fourRings 0 9 True False</langsyntaxhighlight>
 
{{out}}
Line 1,309 ⟶ 3,495:
 
 
Nesting four bind operators (>>=), we can then build the set of solutions in the order: Queensqueens, Leftleft Bishops,bishops Leftand Rooksrooks, Rightright Bishops,bishops Rightand Rooksrooks, Knightsknights.
Probably less readable, but already fast, and could be further optimised.
<langsyntaxhighlight lang="haskell">import Data.List (delete, sortBy, (\\))
 
--------------- 4 RINGS OR 4 SQUARES PUZZLE --------------
rings :: Bool -> [Int] -> [(Int, Int, Int, Int, Int, Int, Int)]
 
type Rings = [(Int, Int, Int, Int, Int, Int, Int)]
 
rings :: Bool -> [Int] -> Rings
rings u digits =
((>>=) <*> (queen u =<< head))
let ds = sortBy (flip compare) digits
(sortBy (flip compare) digits)
h = head ds
 
in ds >>=
queen :: Bool -> Int -> [Int] -> Int -> Rings
-- QUEEN ------------------------------------------------------------------
queen u h ds q = xs >>= leftBishop u q h ts ds
(\q ->
where
let ts = filter ((<= h) . (q +)) ds
ts = filter ((<= h) . (q +)) bs =ds
if uxs
| u then= delete q ts
| otherwise else= ds
 
in bs >>=
leftBishop ::
-- LEFT BISHOP AND ROOK --------------------------------------------
(\lbBool ->
Int ->
let lRook = lb + q
Int ->
in if lRook <= h
[Int] ->
then let rbs =
[Int] ->
if u
Int ->
then ts \\ [q, lb, lRook]
Rings
else ds
leftBishop u q h ts ds lb
in rbs >>=
| lRook <= h = xs >>= rightBishop u q h lb ds lRook
-- RIGHT BISHOP AND ROOK --------------------------
| otherwise = []
(\rb ->
where
let rRook = q + rb
lRook = lb + q
in if (rRook <= h) && (not u || (rRook /= lb))
xs
then let ks =
| u = ts \\ [q, lb, lRook]
if u
| otherwise = ds
then ds \\
 
[ q
rightBishop ::
, lb
Bool ->
, rb
Int ->
, rRook
Int ->
, lRook
Int ->
]
[Int] ->
else ds
Int ->
rookDelta = lRook - rRook
Int ->
in ks >>=
Rings
-- SOLUTION WITH KNIGHTS ---------
rightBishop u q h lb ds lRook rb
(\k ->
| (rRook <= h) && (not u || (rRook /= lb)) =
let k2 = k + rookDelta
let ks
in [ ( lRook
| u = (ds \\ [q, lb, rb, rRook, klRook])
| otherwise = ds
, lb
in ks
, q
>>= knights
, rb
, k2u
(lRook ,- rRook)
lRook
| (k2 `elem` ks) &&
lb
(not u ||
q
notElem
rb
k2
rRook
[ lRook
ks
, k
| otherwise = []
, lb
where
, q
rRook = q + rb
, rb
 
, rRook
knights ::
]) ])
Bool ->
else [])
Int ->
else []))
Int ->
Int ->
Int ->
Int ->
Int ->
[Int] ->
Int ->
Rings
knights u rookDelta lRook lb q rb rRook ks k =
[ (lRook, k, lb, q, rb, k2, rRook)
| (k2 `elem` ks)
&& ( not u
|| notElem
k2
[lRook, k, lb, q, rb, rRook]
)
]
where
k2 = k + rookDelta
 
-- TEST ----------------------------------------------- TEST -------------------------
main :: IO ()
main = do
let f (k, xs) = putStrLn "ringsk >> nl >> mapM_ Trueprint [1xs ..>> 7]\n"nl
mapM_ print $ rings Truenl [1= ..putStrLn 7[]
mapM_
putStrLn "\nrings True [3 .. 9]\n"
f
mapM_ print $ rings True [3 .. 9]
putStrLn "\nlength [ ("rings FalseTrue [01 .. 97])\n", rings True [1 .. 7]),
print $ length ("rings True [3 .. 9]", rings FalseTrue [03 .. 9])</lang>
]
f
( "length (rings False [0 .. 9])",
[length (rings False [0 .. 9])]
)</syntaxhighlight>
{{Out}}
<pre>rings True [1 .. 7]
Line 1,411 ⟶ 3,625:
Implementation for the unique version of the puzzle:
 
<langsyntaxhighlight Jlang="j">fspuz=:dyad define
range=: x+i.1+y-x
lo=. 6+3*x
Line 1,429 ⟶ 3,643:
end.
end.
)</langsyntaxhighlight>
 
Implementation for the non-unique version of the puzzle:
 
<langsyntaxhighlight Jlang="j">fspuz2=:dyad define
range=: x+i.1+y-x
lo=. 3*x
Line 1,449 ⟶ 3,663:
end.
end.
)</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight Jlang="j"> 1 fspuz 7
4 5 3 1 6 2 7
7 2 6 1 3 5 4
Line 1,468 ⟶ 3,682:
9 6 5 4 3 8 7
#0 fspuz2 9
2860</langsyntaxhighlight>
 
=={{header|Java}}==
Uses java 8 features.
<langsyntaxhighlight Javalang="java">import java.util.Arrays;
 
public class FourSquares {
Line 1,529 ⟶ 3,743:
return unique && Arrays.stream(haystack).anyMatch(p -> p == needle);
}
}</langsyntaxhighlight>
{{out}}
<pre>a b c d e f g
Line 1,552 ⟶ 3,766:
===ES6===
{{Trans|Haskell}} (Structured search version)
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
// RINGS ----------- 4-RINGS OR 4--------------------------------------------SQUARES PUZZLE -----------
 
// rings :: noRepeatedDigits -> DigitList -> Lists of solutions
// rings :: Bool -> [Int] -> [[Int]]
const rings = (u, digits)uniq => {
digits => Boolean(digits.length) ? (
const
ds() => sortBy(flip(compare), digits),{
h const ns = headdigits.sort(flip(dscompare));
 
// CENTRAL DIGIT :: d
// QUEEN (i.e. middle digit of 7)---------------------------------------
return concatMapns.flatMap(
q => { ringTriage(uniq)(ns)
const);
ts = filter})(x => (x + q) <=: h, ds),[];
bs = u ? delete_(q, ts) : ds;
 
// LEFT BISHOP (next to queen) AND ITS ROOK (leftmost digit)----
return concatMap(
lb => {
const lRook = lb + q;
return lRook > h ? [] : (() => {
const rbs = u ? difference(ts, [q, lb, lRook]) : ds;
 
const ringTriage = uniq => ns => d => {
// RIGHT BISHOP AND ITS ROOK -----------------------
const
return concatMap(rb => {
const rRookh = q + rb;head(ns),
ts = return (ns.filter(rRookx => h) || (ux &&+ (rRookd) ==<= lbh))) ? (;
[]
) : (() => {
const
rookDelta = lRook - rRook,
ks = u ? difference(
ds, [q, lb, rb, rRook, lRook]
) : ds;
 
// LEFT OF CENTRE :: c and a
// KNIGHTS LEFT AND RIGHT ------------------
return concatMap(k => {
uniq ? (delete_(d)(ts)) : ns
const k2 = k + rookDelta;
)
return (elem(k2, ks) &&
.flatMap(c => {
(!u || notElem(k2, [
const a = c + lRook, k, lb, q, rb, rRookd;
 
]))) ? (
// RIGHT OF CENTRE :: e and [g
return a > h ? (
[lRook, k, lb, q, rb, k2, rRook]
[]
) : [];(
uniq ? }, ks);(
difference(ts)([d, c, })(a]);
) : }, rbs);ns
})();
.flatMap(subTriage(uniq)([ns, h, a, c, },d]));
bs});
);
},
ds
);
};
 
 
// GENERIC FUNCTIONS ------------------------------------------------------
const subTriage = uniq =>
([ns, h, a, c, d]) => e => {
const g = d + e;
 
return ((g > h) || (
uniq && (g === c))
) ? (
[]
) : (() => {
const
agDelta = a - g,
bfs = uniq ? (
difference(ns)([
d, c, e, g, a
])
) : ns;
 
// MID LEFT, MID RIGHT :: b and f
return bfs.flatMap(b => {
const f = b + agDelta;
 
return (bfs).includes(f) && (
!uniq || ![
a, b, c, d, e, g
].includes(f)
) ? ([
[a, b, c, d, e, f, g]
]) : [];
});
})();
};
 
// ---------------------- TEST -----------------------
const main = () => unlines([
"rings(true, enumFromTo(1,7))\n",
unlines(
rings(true)(
enumFromTo(1)(7)
).map(show)
),
 
"\nrings(true, enumFromTo(3, 9))\n",
unlines(
rings(true)(
enumFromTo(3)(9)
).map(show)
),
 
"\nlength(rings(false, enumFromTo(0, 9)))\n",
rings(false)(
enumFromTo(0)(9)
)
.length
.toString(),
""
]);
 
 
// ---------------- GENERIC FUNCTIONS ----------------
 
// compare :: a -> a -> Ordering
const compare = (a, b) => a < b ? -1 : (a > b ? 1 : 0);
a < b ? -1 : (a > b ? 1 : 0);
 
 
// delete :: Eq a => a -> [a] -> [a]
const delete_ = x => {
// xs with first instance of x (if any) removed.
const go = xs =>
Boolean(xs.length) ? (
(x === xs[0]) ? (
xs.slice(1)
) : [xs[0]].concat(go(xs.slice(1)))
) : [];
 
return go;
};
 
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = (f, xs) => [].concat.apply([], xs.map(f));
 
// delete_difference :: Eq a => [a] -> [a] -> [a]
const delete_difference = (x, xs) =>
xs.lengthys => 0 ? ({
(xconst s === xs[0]) ?new Set(ys);
xs.slice(1)
) : [xs[0]].concat(delete_(x, xs.slice(1)))
) : [];
 
// (\\) :: (Eq a) => [a]return ->xs.filter(x [a] -=> [a]!s.has(x));
const difference = (xs, ys) =>};
ys.reduce((a, x) => delete_(x, a), xs);
 
// elem :: Eq a => a -> [a] -> Bool
const elem = (x, xs) => xs.indexOf(x) !== -1;
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
n => Array.from({
length: Math.floor(1 + n - m) + 1
}, (_, i) => m + i);
 
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
 
// flip :: (a -> b -> c) -> b -> a -> c
const flip = fop => (a, b) => f.apply(null, [b, a]);
// The binary function op with
// its arguments reversed.
1 !== op.length ? (
(a, b) => op(b, a)
) : (a => b => op(b)(a));
 
 
// head :: [a] -> a
const head = xs => xs.length ? xs[0] : undefined;
// The first item (if any) in a list.
 
Boolean(xs.length) ? (
// length :: [a] -> Int
const length = xs => xs.length;[0]
) : null;
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
 
// notElem :: Eq a => a -> [a] -> Bool
const notElem = (x, xs) => xs.indexOf(x) === -1;
 
// show :: a -> String
const show = x => JSON.stringify(x); //, null, 2);
JSON.stringify(x);
 
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
const sortBy = (f, xs) => xs.sort(f);
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// A single string formed by the intercalation
// of a list of strings with the newline character.
xs.join("\n");
 
 
// MAIN ---
// TEST --------------------------------------------------------------------
return unlinesmain([);
})();</syntaxhighlight>
'rings(true, enumFromTo(1,7))\n',
unlines(map(show, rings(true, enumFromTo(1, 7)))),
'\nrings(true, enumFromTo(3, 9))\n',
unlines(map(show, rings(true, enumFromTo(3, 9)))),
'\nlength(rings(false, enumFromTo(0, 9)))\n',
length(rings(false, enumFromTo(0, 9)))
.toString(),
''
]);
})();</lang>
{{Out}}
<pre>rings(true, enumFromTo(1,7))
Line 1,703 ⟶ 3,959:
 
2860</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
Since jq is built on back-tracking and optimizes the tail-recursion involved here,
this entry will focus on generic solutiond for problems of this sort.
Specifically, the number of boxes is unrestricted.
 
====N boxes with arbitrary overlaps====
In this section, an arbitrary pattern of overlaps can be specified as follows.
 
We will associate the letters "a", "b", ... with the integers 0, 1,...
so that each box can be represented as an array of integers; the
puzzle configuration can then be characterized by an array of such arrays.
For the particular puzzle under consideration, the characteristic array is:
 
[[0,1], [1,2,3], [3,4,5], [5,6]]
 
The solution in this subsection is quite efficient for the family of problems based on permutations, but as is shown, can also be used without the permutation constraint.
<syntaxhighlight lang="jq"># Generate a stream of all the permutations of the input array
def permutations:
if length == 0 then []
else
range(0;length) as $i
| [.[$i]] + (del(.[$i])|permutations)
end ;
 
# Permutations of a ... n inclusive
def permutations(a;n):
[range(a;n+1)] | permutations;
 
# value of a box
# Input: the table of values
def valueOfBox($box):
[ .[ $box[] ]] | add;
 
def allEqual($boxes):
. as $values
| valueOfBox($boxes[0]) as $sum
| all($boxes[1:][]; . as $box | $values | valueOfBox($box) == $sum);
 
def combinations($m; $n; $size):
[range(0; $size) | [range($m; $n)]] | combinations;
 
def count(s): reduce s as $x (null; .+1);
 
# a=0, b=1, etc
def boxes: [[0,1], [1,2,3], [3,4,5], [5,6]];
 
def tasks:
"1 to 7:",
(permutations(1;7) | select(allEqual(boxes))),
"\n3 to 9:",
(permutations(3;9) | select(allEqual(boxes))),
"\n0 to 9:\n\(count(permutations(0;9) | select(allEqual(boxes))))",
"\nThere are \(count(combinations(0;10;7) | select(allEqual(boxes)))) solutions for 0 to 9 with replacement."
;
 
tasks</syntaxhighlight>
{{out}}
<pre>
1 to 7:
[3,7,2,1,5,4,6]
[4,5,3,1,6,2,7]
[4,7,1,3,2,6,5]
[5,6,2,3,1,7,4]
[6,4,1,5,2,3,7]
[6,4,5,1,2,7,3]
[7,2,6,1,3,5,4]
[7,3,2,5,1,4,6]
 
3 to 9:
[7,8,3,4,5,6,9]
[8,7,3,5,4,6,9]
[9,6,4,5,3,7,8]
[9,6,5,4,3,8,7]
 
There are 1152 distinct solutions for 0 to 9.
 
There are 2860 solutions for 0 to 9 with replacement.
</pre>
====N boxes with one overlap between adjacent boxes and no uniqueness constraint====
 
In this subsection, an efficient solution for the N-boxes puzzle in the case of non-uniqueness
(i.e. unrestricted choice of values within the specified range) is given. It is assumed, however,
that each box (except for the last) has exactly one overlap with its successor.
 
For consistency with the prior section, the pattern can be specified in the same way,
i.e. as a characteristic array, which for the specific problem at hand could be:
[[0,1], [1,2,3], [3,4,5], [5,6]].
 
<syntaxhighlight lang="jq"># rings/3 assumes that each box (except for the last) has exactly one overlap with its successor.
# Input: ignored.
# Output: a stream of solutions, i.e. a stream of arrays.
# $boxes is an array of boxes, each box being a flat array.
# $min and $max define the range of permissible values of items in the boxes (inclusive)
def rings($boxes; $min; $max):
 
def inrange: $min <= . and . <= $max;
# The following helper function deals with the case when the global per-box sum ($sum) is known.
# Input: an array representing the solution so far, or null.
# Output: the input plus the solution corresponding to the first argument.
# $this is the sum of the previous items in the first box, or 0.
def solve($boxes; $this; $sum):
# The following is a helper function for handling the case when:
# * $sum is known
# * $boxes[0] | length == 1, and
# * $boxes|length>1
def lastInBox($boxes; $this):
. as $in
| ($boxes[1:] | (.[0] |= .[1:])) as $bx
# the first entry in the next box must be the same:
| ($sum - $this) as $next
| select($next | inrange)
| (. + [$next]) | solve( $bx; $next; $sum) ;
 
. as $in
| if $boxes|length == 0 then $in
else $boxes[0] as $box
| if $box|length == 0
then solve( $boxes[1:]; 0; $sum )
elif $box|length == 1
# is this the last box?
then if $boxes|length == 1
then ($sum - $this) as $next
| select($next | inrange)
| $in + [$next]
else lastInBox($boxes; $this)
end
else # $box|length > 1
range($min; $max + 1) as $first
| select( ($this + $first) <= $sum)
| ($in + [$first]) | solve( [$box[1:]] + $boxes[1:]; $this + $first; $sum)
end
end ;
. as $in
| $boxes[0] as $box
| ($boxes[1:] | .[0] |= .[1:]) as $bx
| [range(0; $box|length) | [range($min; $max + 1)]]
| combinations
| solve($bx; .[-1]; add) ;
 
def count(s): reduce s as $x (null; .+1);</syntaxhighlight>
'''The specific task'''
<syntaxhighlight lang="jq"># a=0, b=1, etc
def boxes: [[0,1], [1,2,3], [3,4,5], [5,6]];
 
count(rings(boxes; 0; 9))</syntaxhighlight>
{{out}}
<pre>
2860
</pre>
 
=={{header|Julia}}==
{{Trans|Python}}
<langsyntaxhighlight lang="julia">
using Combinatorics
 
Line 1,729 ⟶ 4,141:
foursquares(3, 9, true, true)
foursquares(0, 9, false, false)
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 1,747 ⟶ 4,159:
Total unique solutions for HIGH 9, LOW 3: 4
Total solutions for HIGH 9, LOW 0: 2860
</pre>
 
=={{header|Koka}}==
{{trans|Rust}}
<syntaxhighlight lang="koka">
fun is_unique(a: int, b: int, c: int, d: int, e: int, f: int, g: int)
a != b && a != c && a != d && a != e && a != f && a != g &&
b != c && b != d && b != e && b != f && b != g &&
c != d && c != e && c != f && c != g &&
d != e && d != f && d != g &&
e != f && e != g &&
f != g
 
fun is_solution(a: int, b: int, c: int, d: int, e: int, f: int, g: int)
val bcd = b + c + d
val ab = a + b
if ab != bcd then return False
val def = d + e + f
if bcd != def then return False
val fg = f + g
return def == fg
 
fun four_squares(low: int, high: int, unique:bool=True)
var count := 0
for(low, high) fn(a)
for(low, high) fn(b)
for(low, high) fn(c)
for(low, high) fn(d)
for(low, high) fn(e)
for(low, high) fn(f)
for(low, high) fn(g)
if (!unique || is_unique(a, b, c, d, e, f, g)) && is_solution(a, b, c, d, e, f, g) then
count := count + 1
if unique then
println([a, b, c, d, e, f, g].show)
else
()
val uniquestr = if unique then "unique" else "non-unique"
println(count.show ++ " " ++ uniquestr ++ " solutions in " ++ low.show ++ " to " ++ high.show ++ " range\n")
 
fun main()
four_squares(1, 7)
four_squares(3, 9)
four_squares(0, 9, False)
</syntaxhighlight>
 
{{out}}
<pre>
[3,7,2,1,5,4,6]
[4,5,3,1,6,2,7]
[4,7,1,3,2,6,5]
[5,6,2,3,1,7,4]
[6,4,1,5,2,3,7]
[6,4,5,1,2,7,3]
[7,2,6,1,3,5,4]
[7,3,2,5,1,4,6]
8 unique solutions in 1 to 7 range
 
[7,8,3,4,5,6,9]
[8,7,3,5,4,6,9]
[9,6,4,5,3,7,8]
[9,6,5,4,3,8,7]
4 unique solutions in 3 to 9 range
 
2860 non-unique solutions in 0 to 9 range
</pre>
 
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class FourSquares(
Line 1,823 ⟶ 4,300:
FourSquares(3, 9, true, true)
FourSquares(0, 9, false, false)
}</langsyntaxhighlight>
 
{{out}}
Line 1,852 ⟶ 4,329:
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|Lua}}==
{{trans|D}}
<syntaxhighlight lang="lua">function valid(unique,needle,haystack)
if unique then
for _,value in pairs(haystack) do
if needle == value then
return false
end
end
end
return true
end
 
function fourSquare(low,high,unique,prnt)
count = 0
if prnt then
print("a", "b", "c", "d", "e", "f", "g")
end
for a=low,high do
for b=low,high do
if valid(unique, a, {b}) then
fp = a + b
for c=low,high do
if valid(unique, c, {a, b}) then
for d=low,high do
if valid(unique, d, {a, b, c}) and fp == b + c + d then
for e=low,high do
if valid(unique, e, {a, b, c, d}) then
for f=low,high do
if valid(unique, f, {a, b, c, d, e}) and fp == d + e + f then
for g=low,high do
if valid(unique, g, {a, b, c, d, e, f}) and fp == f + g then
count = count + 1
if prnt then
print(a, b, c, d, e, f, g)
end
end
end
end
end
end
end
end
end
end
end
end
end
end
if unique then
print(string.format("There are %d unique solutions in [%d, %d]", count, low, high))
else
print(string.format("There are %d non-unique solutions in [%d, %d]", count, low, high))
end
end
 
fourSquare(1,7,true,true)
fourSquare(3,9,true,true)
fourSquare(0,9,false,false)</syntaxhighlight>
{{out}}
<pre>a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
There are 8 unique solutions in [1, 7]
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
There are 4 unique solutions in [3, 9]
There are 2860 non-unique solutions in [0, 9]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">{low, high} = {1, 7};
SolveValues[{a + b == b + c + d == d + e + f == f + g, low <= a <= high,
low <= b <= high, low <= c <= high, low <= d <= high,
low <= e <= high, low <= f <= high, low <= g <= high,
a != b != c != d != e != f != g}, {a, b, c, d, e, f, g}, Integers]
 
{low, high} = {3, 9};
SolveValues[{a + b == b + c + d == d + e + f == f + g, low <= a <= high,
low <= b <= high, low <= c <= high, low <= d <= high,
low <= e <= high, low <= f <= high, low <= g <= high,
a != b != c != d != e != f != g}, {a, b, c, d, e, f, g}, Integers]
 
{low, high} = {0, 9};
SolveValues[{a + b == b + c + d == d + e + f == f + g, low <= a <= high,
low <= b <= high, low <= c <= high, low <= d <= high,
low <= e <= high, low <= f <= high, low <= g <= high}, {a, b, c, d,
e, f, g}, Integers] // Length</syntaxhighlight>
{{out}}
<pre>{{3, 7, 2, 1, 5, 4, 6}, {4, 5, 3, 1, 6, 2, 7}, {4, 7, 1, 3, 2, 6,
5}, {5, 6, 2, 3, 1, 7, 4}, {6, 4, 1, 5, 2, 3, 7}, {6, 4, 5, 1, 2, 7,
3}, {7, 2, 6, 1, 3, 5, 4}, {7, 3, 2, 5, 1, 4, 6}}
 
{{7, 8, 3, 4, 5, 6, 9}, {8, 7, 3, 5, 4, 6, 9}, {9, 6, 4, 5, 3, 7,
8}, {9, 6, 5, 4, 3, 8, 7}}
 
2860</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">combinations = function(elements, comboLength, unique=true)
n = elements.len
if comboLength > n then return []
allCombos = []
genCombos=function(start, currCombo)
if currCombo.len == comboLength then
allCombos.push(currCombo)
return
end if
if start == n then return
for i in range(start, n - 1)
newCombo = currCombo + [elements[i]]
genCombos(i + unique, newCombo)
end for
end function
genCombos(0, [])
return allCombos
end function
 
permutations = function(elements, permLength=null)
n = elements.len
elements.sort
if permLength == null then permLength = n
allPerms = []
genPerms = function(prefix, remainingElements)
if prefix.len == permLength then
allPerms.push(prefix)
return
end if
for i in range(0, remainingElements.len - 1)
if i > 0 and remainingElements[i] == remainingElements[i-1] then continue
newPrefix = prefix + [remainingElements[i]]
newRemains = remainingElements[:i] + remainingElements[i+1:]
genPerms(newPrefix, newRemains)
end for
end function
genPerms([],elements)
return allPerms
end function
 
ringsEqual = function(a)
if a.len != 7 then return false
return a[0]+a[1] == a[1]+a[2]+a[3] == a[3]+a[4]+a[5] == a[5] + a[6]
end function
 
fourRings = function(lo, hi, unique, show)
rng = range(lo, hi)
combos = combinations(rng, 7, unique)
cnt = 0
for c in combos
for p in permutations(c)
if ringsEqual(p) then
cnt += 1
if show then print p.join(", ")
end if
end for
end for
uniStr = [" nonunique", " unique"]
print cnt + uniStr[unique] + " solutions for " + lo + " to " + hi
print
end function
 
fourRings(1, 7, true, true)
fourRings(3, 9, true, true)
fourRings(0, 9, false, false)
</syntaxhighlight>
{{out}}
<pre>
3, 7, 2, 1, 5, 4, 6
4, 5, 3, 1, 6, 2, 7
4, 7, 1, 3, 2, 6, 5
5, 6, 2, 3, 1, 7, 4
6, 4, 1, 5, 2, 3, 7
6, 4, 5, 1, 2, 7, 3
7, 2, 6, 1, 3, 5, 4
7, 3, 2, 5, 1, 4, 6
8 unique solutions for 1 to 7
 
7, 8, 3, 4, 5, 6, 9
8, 7, 3, 5, 4, 6, 9
9, 6, 4, 5, 3, 7, 8
9, 6, 5, 4, 3, 8, 7
4 unique solutions for 3 to 9
 
2860 nonunique solutions for 0 to 9</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE FourSquare;
FROM Conversions IMPORT IntToStr;
FROM Terminal IMPORT *;
 
PROCEDURE WriteInt(num : INTEGER);
VAR str : ARRAY[0..16] OF CHAR;
BEGIN
IntToStr(num,str);
WriteString(str);
END WriteInt;
 
PROCEDURE four_square(low, high : INTEGER; unique, print : BOOLEAN);
VAR count : INTEGER;
VAR a, b, c, d, e, f, g : INTEGER;
VAR fp : INTEGER;
BEGIN
count:=0;
 
IF print THEN
WriteString('a b c d e f g');
WriteLn;
END;
FOR a:=low TO high DO
FOR b:=low TO high DO
IF unique AND (b=a) THEN CONTINUE; END;
 
fp:=a+b;
FOR c:=low TO high DO
IF unique AND ((c=a) OR (c=b)) THEN CONTINUE; END;
FOR d:=low TO high DO
IF unique AND ((d=a) OR (d=b) OR (d=c)) THEN CONTINUE; END;
IF fp # b+c+d THEN CONTINUE; END;
 
FOR e:=low TO high DO
IF unique AND ((e=a) OR (e=b) OR (e=c) OR (e=d)) THEN CONTINUE; END;
FOR f:=low TO high DO
IF unique AND ((f=a) OR (f=b) OR (f=c) OR (f=d) OR (f=e)) THEN CONTINUE; END;
IF fp # d+e+f THEN CONTINUE; END;
 
FOR g:=low TO high DO
IF unique AND ((g=a) OR (g=b) OR (g=c) OR (g=d) OR (g=e) OR (g=f)) THEN CONTINUE; END;
IF fp # f+g THEN CONTINUE; END;
 
INC(count);
IF print THEN
WriteInt(a);
WriteString(' ');
WriteInt(b);
WriteString(' ');
WriteInt(c);
WriteString(' ');
WriteInt(d);
WriteString(' ');
WriteInt(e);
WriteString(' ');
WriteInt(f);
WriteString(' ');
WriteInt(g);
WriteLn;
END;
END;
END;
END;
END;
END;
END;
END;
IF unique THEN
WriteString('There are ');
WriteInt(count);
WriteString(' unique solutions in [');
WriteInt(low);
WriteString(', ');
WriteInt(high);
WriteString(']');
WriteLn;
ELSE
WriteString('There are ');
WriteInt(count);
WriteString(' non-unique solutions in [');
WriteInt(low);
WriteString(', ');
WriteInt(high);
WriteString(']');
WriteLn;
END;
END four_square;
 
BEGIN
four_square(1,7,TRUE,TRUE);
four_square(3,9,TRUE,TRUE);
four_square(0,9,FALSE,FALSE);
ReadChar; (* Wait so results can be viewed. *)
END FourSquare.</syntaxhighlight>
 
=={{header|Nim}}==
Adapted from Rust version.
<syntaxhighlight lang="nim">func isUnique(a, b, c, d, e, f, g: uint8): bool =
a != b and a != c and a != d and a != e and a != f and a != g and
b != c and b != d and b != e and b != f and b != g and
c != d and c != e and c != f and c != g and
d != e and d != f and d != f and
e != f and e != g and
f != g
 
func isSolution(a, b, c, d, e, f, g: uint8): bool =
let sum = a + b
sum == b + c + d and sum == d + e + f and sum == f + g
 
func fourSquares(l, h: uint8, unique: bool): seq[array[7, uint8]] =
for a in l..h:
for b in l..h:
for c in l..h:
for d in l..h:
for e in l..h:
for f in l..h:
for g in l..h:
if (not unique or isUnique(a, b, c, d, e, f, g)) and
isSolution(a, b, c, d, e, f, g):
result &= [a, b, c, d, e, f, g]
 
proc printFourSquares(l, h: uint8, unique = true) =
let solutions = fourSquares(l, h, unique)
 
if unique:
for s in solutions:
echo s
 
echo solutions.len, (if unique: " " else: " non-"), "unique solutions in ",
l, " to ", h, " range\n"
 
when isMainModule:
printFourSquares(1, 7)
printFourSquares(3, 9)
printFourSquares(0, 9, unique = false)</syntaxhighlight>
{{out}}
<pre>[3, 7, 2, 1, 5, 4, 6]
[4, 5, 3, 1, 6, 2, 7]
[4, 7, 1, 3, 2, 6, 5]
[5, 6, 2, 3, 1, 7, 4]
[6, 4, 1, 5, 2, 3, 7]
[6, 4, 5, 1, 2, 7, 3]
[7, 2, 6, 1, 3, 5, 4]
[7, 3, 2, 5, 1, 4, 6]
8 unique solutions in 1 to 7 range
 
[7, 8, 3, 4, 5, 6, 9]
[8, 7, 3, 5, 4, 6, 9]
[9, 6, 4, 5, 3, 7, 8]
[9, 6, 5, 4, 3, 8, 7]
4 unique solutions in 3 to 9 range
 
2860 non-unique solutions in 0 to 9 range</pre>
 
=={{header|OCaml}}==
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
<syntaxhighlight lang="OCaml">
 
(* Task : 4-rings_or_4-squares_puzzle *)
 
(*
Replace a, b, c, d, e, f, and g with the decimal digits LOW ───► HIGH
such that the sum of the letters inside of each of the four large squares add up to the same sum.
 
Squares are: ab; bcd; def; fg
Solution: brute force from generating a, b, d, g from possible range
*)
 
(*** Helpers ***)
 
type assignment = {
a: int;
b: int;
c: int;
d: int;
e: int;
f: int;
g: int;
}
 
let generate ((a, b), (d, g)) =
let s = a + b in
let c = s - b - d in
let f = s - g in
let e = s - f - d in
{a; b; c; d; e; f; g}
 
let list_of_assign assign =
[assign.a; assign.b; assign.c; assign.d; assign.e; assign.f; assign.g]
 
let test unique low high assign =
let l = list_of_assign assign in
let test_el e =
e >= low && e <= high &&
(not unique || (l |> List.filter ((=) e) |> List.length) == 1)
in
List.for_all test_el l
 
let generator low high =
let single () = Seq.ints low |> Seq.take_while (fun x -> x <= high) in
let first_two = Seq.product (single ()) (single ()) in
let second_two = Seq.product (single ()) (single ()) in
let final = Seq.product first_two second_two in
Seq.map generate final
 
let print_assign a =
Printf.printf "a: %d, b: %d, c: %d, d: %d, e: %d, f: %d, g: %d\n"
a.a a.b a.c a.d a.e a.f a.g
 
(*** Actual task at hand ***)
 
let evaluate low high unique log =
let seqs = generator low high |> Seq.filter (test unique low high) in
let unique_str = if unique then "unique" else "non-unique" in
if log then Seq.iter print_assign seqs;
Printf.printf "%d %s sequences found between %d and %d\n\n" (Seq.length seqs) unique_str low high
 
(*** Output ***)
 
let () =
evaluate 1 7 true true;
evaluate 3 9 true true;
evaluate 0 9 false false
;;
 
</syntaxhighlight>
{{out}}
<pre>
a: 7, b: 2, c: 6, d: 1, e: 3, f: 5, g: 4
a: 6, b: 4, c: 5, d: 1, e: 2, f: 7, g: 3
a: 3, b: 7, c: 2, d: 1, e: 5, f: 4, g: 6
a: 4, b: 5, c: 3, d: 1, e: 6, f: 2, g: 7
a: 5, b: 6, c: 2, d: 3, e: 1, f: 7, g: 4
a: 4, b: 7, c: 1, d: 3, e: 2, f: 6, g: 5
a: 7, b: 3, c: 2, d: 5, e: 1, f: 4, g: 6
a: 6, b: 4, c: 1, d: 5, e: 2, f: 3, g: 7
8 unique sequences found between 1 and 7
 
a: 9, b: 6, c: 5, d: 4, e: 3, f: 8, g: 7
a: 9, b: 6, c: 4, d: 5, e: 3, f: 7, g: 8
a: 7, b: 8, c: 3, d: 4, e: 5, f: 6, g: 9
a: 8, b: 7, c: 3, d: 5, e: 4, f: 6, g: 9
4 unique sequences found between 3 and 9
 
2860 non-unique sequences found between 0 and 9
</pre>
 
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
There are so few solutions of 7 consecutive numbers, so I used a modified version, to get all the expected solutions at once.
<langsyntaxhighlight lang="pascal">program square4;
{$MODE DELPHI}
{$R+,O+}
Line 1,970 ⟶ 4,895:
writeln(' solution count for ',loDgt,' to ',HiDgt,' = ',cnt);
writeln('unique solution count for ',loDgt,' to ',HiDgt,' = ',uniqueCount);
end.</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,008 ⟶ 4,933:
unique solution count for 0 to 9 = 192</pre>
 
=={{header|Perl 6}}==
Relying on the modules <code>ntheory</code> and <code>Set::CrossProduct</code> to generate the tuples needed. Both are supply results via iterators, particularly important in the latter case, to avoid gobbling too much memory.
{{works with|Rakudo|2016.12}}
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use ntheory qw/forperm/;
use Set::CrossProduct;
 
sub four_sq_permute {
<lang perl6>sub four-squares ( @list, :$unique=1, :$show=1 ) {
my($list) = @_;
my @solutions;
forperm {
@c = @$list[@_];
push @solutions, [@c] if check(@c);
} @$list;
print +@solutions . " unique solutions found using: " . join(', ', @$list) . "\n";
return @solutions;
}
 
sub four_sq_cartesian {
my(@list) = @_;
my @solutions;
my $iterator = Set::CrossProduct->new( [(@list) x 7] );
 
forwhile( my $unique.&combosc = $iterator->get @c) {
push @solutions.push:, [@$c] if [==]check(@$c);
@c[0] + @c[1],
@c[1] + @c[2] + @c[3],
@c[3] + @c[4] + @c[5],
@c[5] + @c[6];
}
print +@solutions . " non-unique solutions found using: " . join(', ', @{@list[0]}) . "\n";
return @solutions;
}
 
sub check {
say +@solutions, ($unique ?? ' ' !! ' non-'), "unique solutions found using {join(', ', @list)}.\n";
my(@c) = @_;
$a = $c[0] + $c[1];
$b = $c[1] + $c[2] + $c[3];
$c = $c[3] + $c[4] + $c[5];
$d = $c[5] + $c[6];
$a == $b and $a == $c and $a == $d;
}
 
sub display {
my $f = "%{@list.max.chars}s";
my(@solutions) = @_;
my $fmt = "%2s " x 7 . "\n";
printf $fmt, ('a'..'g');
printf $fmt, @$_ for @solutions;
print "\n";
}
 
display four_sq_permute( [1..7] );
say join "\n", (('a'..'g').fmt: $f), @solutions».fmt($f), "\n" if $show;
display four_sq_permute( [3..9] );
display four_sq_permute( [8, 9, 11, 12, 17, 18, 20, 21] );
four_sq_cartesian( [0..9] );</syntaxhighlight>
{{out}}
<pre>8 unique solutions found using: 1, 2, 3, 4, 5, 6, 7
a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
 
4 unique solutions found using: 3, 4, 5, 6, 7, 8, 9
multi combos ( $ where so * ) { @list.combinations(7).map: |*.permutations }
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
8 unique solutions found using: 8, 9, 11, 12, 17, 18, 20, 21
multi combos ( $ where not * ) { [X] @list xx 7 }
a b c d e f g
}
17 21 8 9 11 18 20
17 21 9 8 12 18 20
20 18 8 12 9 17 21
20 18 11 9 8 21 17
20 18 11 9 12 17 21
20 18 12 8 9 21 17
21 17 9 12 8 18 20
21 17 12 9 11 18 20
 
2860 non-unique solutions found using: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9</pre>
# TASK
===With Recursion===
four-squares( [1..7] );
<syntaxhighlight lang="perl">#!/usr/bin/perl
four-squares( [3..9] );
four-squares( [8, 9, 11, 12, 17, 18, 20, 21] );
four-squares( [0..9], :unique(0), :show(0) );</lang>
 
use strict; # https://rosettacode.org/wiki/4-rings_or_4-squares_puzzle
{{out}}
use warnings;
 
for ( [1 .. 7], [3 .. 9] )
<pre>8 unique solutions found using 1, 2, 3, 4, 5, 6, 7.
{
print "for @$_\n\n";
findunique( $_ );
print "\n";
}
my $count = 0;
findcount();
print "count of non-unique 0-9: $count\n";
 
sub findunique
{
my @allowed = @{ shift @_ };
if( @_ == 4 ) { $_[0] == $_[2] + $_[3] or return }
elsif( @_ == 6 ) { $_[1] + $_[2] == $_[4] + $_[5] or return }
elsif( @_ == 7 ) { $_[3] + $_[4] == $_[6] and print "@_\n"; return }
for my $n ( @allowed )
{
findunique( [ grep $n != $_, @allowed ], @_, $n );
}
}
 
sub findcount
{
if( @_ == 4 ) { $_[0] == $_[2] + $_[3] or return }
elsif( @_ == 6 ) { $_[1] + $_[2] == $_[4] + $_[5] or return }
elsif( @_ == 7 ) { $_[3] + $_[4] == $_[6] and $count++; return }
findcount( @_, $_ ) for 0 .. 9;
}</syntaxhighlight>
{{out}}
<pre>
for 1 2 3 4 5 6 7
 
a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
Line 2,054 ⟶ 5,061:
7 3 2 5 1 4 6
 
for 3 4 5 6 7 8 9
 
4 unique solutions found using 3, 4, 5, 6, 7, 8, 9.
 
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
Line 2,063 ⟶ 5,068:
9 6 5 4 3 8 7
 
count of non-unique 0-9: 2860
 
8 unique solutions found using 8, 9, 11, 12, 17, 18, 20, 21.
 
a b c d e f g
17 21 8 9 11 18 20
20 18 11 9 8 21 17
17 21 9 8 12 18 20
20 18 8 12 9 17 21
20 18 12 8 9 21 17
21 17 9 12 8 18 20
20 18 11 9 12 17 21
21 17 12 9 11 18 20
 
 
2860 non-unique solutions found using 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
</pre>
=={{header|Phix}}==
<lang Phix>integer solutions
 
=={{header|Phix}}==
procedure check(sequence set, bool show)
<!--<syntaxhighlight lang="phix">(phixonline)-->
integer {a,b,c,d,e,f,g} = set, ab = a+b
<span style="color: #000080;font-style:italic;">-- demo/rosetta/4_rings_or_4_squares_puzzle.exw</span>
if ab=b+d+c and ab=d+e+f and ab=f+g then
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
solutions += 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">solutions</span>
if show then
?set
<span style="color: #008080;">procedure</span> <span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ab</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">ab</span><span style="color: #0000FF;">=</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">c</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ab</span><span style="color: #0000FF;">=</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">e</span><span style="color: #0000FF;">+</span><span style="color: #000000;">f</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ab</span><span style="color: #0000FF;">=</span><span style="color: #000000;">f</span><span style="color: #0000FF;">+</span><span style="color: #000000;">g</span> <span style="color: #008080;">then</span>
end procedure
<span style="color: #000000;">solutions</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
 
<span style="color: #008080;">if</span> <span style="color: #000000;">show</span> <span style="color: #008080;">then</span>
procedure foursquares(integer lo, integer hi, bool uniq, bool show)
<span style="color: #0000FF;">?</span><span style="color: #000000;">set</span>
sequence set = repeat(lo,7)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
solutions = 0
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if uniq then
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
for i=1 to 7 do
set[i] = lo+i-1
<span style="color: #008080;">procedure</span> <span style="color: #000000;">foursquares</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hi</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">uniq</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
for i=1 to factorial(7) do
<span style="color: #000000;">solutions</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
check(permute(i,set),show)
<span style="color: #008080;">if</span> <span style="color: #000000;">uniq</span> <span style="color: #008080;">then</span>
end for
<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;">7</span> <span style="color: #008080;">do</span>
else
<span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">+</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
integer done = 0
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
while not done 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: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
check(set,show)
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set</span><span style="color: #0000FF;">),</span><span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
for i=1 to 7 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
set[i] += 1
<span style="color: #008080;">else</span>
if set[i]<=hi then exit end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
if i=7 then
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
done = 1
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show</span><span style="color: #0000FF;">)</span>
exit
<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;">7</span> <span style="color: #008080;">do</span>
end if
<span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
set[i] = lo
<span style="color: #008080;">if</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]<=</span><span style="color: #000000;">hi</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 for
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">7</span> <span style="color: #008080;">then</span>
end while
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #008080;">exit</span>
printf(1,"%d solutions\n",solutions)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lo</span>
foursquares(1,7,uniq:=True,show:=True)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
foursquares(3,9,True,True)
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
foursquares(0,9,False,False)</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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;">"%d solutions\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solutions</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">foursquares</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">uniq</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">show</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">foursquares</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">foursquares</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,139 ⟶ 5,135:
4 solutions
2860 solutions
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">import cp.
 
main =>
puzzle_all(1, 7, true, Sol1),
foreach(Sol in Sol1) println(Sol) end,
nl,
puzzle_all(3, 9, true, Sol2),
foreach(Sol in Sol2) println(Sol) end,
nl,
puzzle_all(0, 9, false, Sol3),
println(len=Sol3.len),
nl.
 
puzzle_all(Min, Max, Distinct, LL) =>
L = [A,B,C,D,E,F,G],
L :: Min..Max,
if Distinct then
all_different(L)
else
true
end,
T #= A+B,
T #= B+C+D,
T #= D+E+F,
T #= F+G,
% Another approach:
% Sums = $[A+B,B+C+D,D+E+F,F+G],
% foreach(I in 2..Sums.len) Sums[I] #= Sums[I-1] end,
LL = solve_all(L).</syntaxhighlight>
 
{{out}}
<pre>
Picat> main
[3,7,2,1,5,4,6]
[4,5,3,1,6,2,7]
[4,7,1,3,2,6,5]
[5,6,2,3,1,7,4]
[6,4,1,5,2,3,7]
[6,4,5,1,2,7,3]
[7,2,6,1,3,5,4]
[7,3,2,5,1,4,6]
 
[7,8,3,4,5,6,9]
[8,7,3,5,4,6,9]
[9,6,4,5,3,7,8]
[9,6,5,4,3,8,7]
 
len = 2860</pre>
 
=={{header|PL/M}}==
{{Trans|ALGOL 68}}
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="pli">100H: /* SOLVE THE 4 RINGS OR 4 SQUARES PUZZLE */
 
DECLARE FALSE LITERALLY '0';
DECLARE TRUE LITERALLY '0FFH';
 
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* FIND SOLUTIONS TO THE EQUATIONS: */
/* A + B = B + C + D = D + E + F = F + G */
/* WHERE A, B, C, D, E, F, G IN LO : HI ( NOT NECESSARILY UNIQUE ) */
/* DEPENDING ON SHOW, THE SOLUTIONS WILL BE PRINTED OR NOT */
FOUR$RINGS: PROCEDURE( LO, HI, ALLOW$DUPLICATES, SHOW );
DECLARE ( LO, HI ) ADDRESS;
DECLARE ( ALLOW$DUPLICATES, SHOW ) BYTE;
DECLARE ( SOLUTIONS, A, B, C, D, E, F, G, T ) ADDRESS;
SOLUTIONS = 0;
DO A = LO TO HI;
DO B = LO TO HI;
IF ALLOWDUPLICATES OR A <> B THEN DO;
T = A + B;
DO C = LO TO HI;
IF ALLOWDUPLICATES OR ( A <> C AND B <> C ) THEN DO;
D = T - ( B + C );
IF D >= LO AND D <= HI
AND ( ALLOW$DUPLICATES
OR ( A <> D AND B <> D AND C <> D )
)
THEN DO;
DO E = LO TO HI;
IF ALLOWDUPLICATES
OR ( A <> E AND B <> E
AND C <> E AND D <> E
)
THEN DO;
G = D + E;
F = T - G;
IF F >= LO AND F <= HI
AND G >= LO AND G <= HI
AND ( ALLOWDUPLICATES
OR ( A <> F AND B <> F AND C <> F
AND D <> F AND E <> F
AND A <> G AND B <> G AND C <> G
AND D <> G AND E <> G AND F <> G
)
)
THEN DO;
SOLUTIONS = SOLUTIONS + 1;
IF SHOW THEN DO;
CALL PR$CHAR( ' ' ); CALL PR$NUMBER( A );
CALL PR$CHAR( ' ' ); CALL PR$NUMBER( B );
CALL PR$CHAR( ' ' ); CALL PR$NUMBER( C );
CALL PR$CHAR( ' ' ); CALL PR$NUMBER( D );
CALL PR$CHAR( ' ' ); CALL PR$NUMBER( E );
CALL PR$CHAR( ' ' ); CALL PR$NUMBER( F );
CALL PR$CHAR( ' ' ); CALL PR$NUMBER( G );
CALL PR$NL;
END;
END;
END;
END;
END;
END;
END;
END;
END;
END;
CALL PR$NUMBER( SOLUTIONS );
IF ALLOW$DUPLICATES THEN CALL PR$STRING( .' NON-UNIQUE$' );
ELSE CALL PR$STRING( .' UNIQUE$' );
CALL PR$STRING( .' SOLUTIONS IN $' );
CALL PR$NUMBER( LO );
CALL PR$STRING( .' TO $' );
CALL PR$NUMBER( HI );
CALL PR$NL;
CALL PR$NL;
END FOUR$RINGS;
 
/* FIND THE SOLUTIONS AS REQUIRED FOR THE TASK */
CALL FOUR$RINGS( 1, 7, FALSE, TRUE );
CALL FOUR$RINGS( 3, 9, FALSE, TRUE );
CALL FOUR$RINGS( 0, 9, TRUE, FALSE );
EOF</syntaxhighlight>
{{out}}
<pre>
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
8 UNIQUE SOLUTIONS IN 1 TO 7
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
4 UNIQUE SOLUTIONS IN 3 TO 9
 
2860 NON-UNIQUE SOLUTIONS IN 0 TO 9
 
</pre>
 
=={{header|PL/SQL}}==
{{works with|Oracle}}
<langsyntaxhighlight lang="plsql">
create table allints (v number);
create table results
Line 2,275 ⟶ 5,446:
end;
/
</syntaxhighlight>
</lang>
Output
<pre>
Line 2,308 ⟶ 5,479:
=={{header|Prolog}}==
Works with SWI-Prolog 7.5.8
<syntaxhighlight lang="prolog">
<lang Prolog>
:- use_module(library(clpfd)).
 
Line 2,332 ⟶ 5,503:
my_sum(Min, Max, 1, LL),
length(LL, Len).
</syntaxhighlight>
</lang>
Output
<pre>
Line 2,356 ⟶ 5,527:
N = 2860.
</pre>
 
=={{header|Python}}==
===Procedural===
<lang Python>
====Itertools====
import itertools
<syntaxhighlight lang="python">import itertools
 
def all_equal(a,b,c,d,e,f,g):
Line 2,380 ⟶ 5,553:
 
print str(solutions)+" "+uorn+" solutions in "+str(lo)+" to "+str(hi)
print</syntaxhighlight>
</lang>
Output
<pre>foursquares(1,7,True,True)
<pre>
foursquares(1,7,True,True)
4, 5, 3, 1, 6, 2, 7
3, 7, 2, 1, 5, 4, 6
Line 2,405 ⟶ 5,576:
 
foursquares(0,9,False,False)
2860 non-unique solutions in 0 to 9</pre>
 
====Generators====
</pre>
Faster solution without itertools
<syntaxhighlight lang="python">
<lang Python>
def foursquares(lo,hi,unique,show):
 
Line 2,474 ⟶ 5,645:
print str(solutions)+" "+uorn+" solutions in "+str(lo)+" to "+str(hi)
print</syntaxhighlight>
Output<pre>
 
</lang>
Output
<pre>
foursquares(1,7,True,True)
4, 7, 1, 3, 2, 6, 5
Line 2,500 ⟶ 5,668:
 
foursquares(0,9,False,False)
2860 non-unique solutions in 0 to 9</pre>
 
===Functional===
{{Trans|Haskell}}
{{Trans|JavaScript}}
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''4-rings or 4-squares puzzle'''
 
from itertools import chain
 
 
# rings :: noRepeatedDigits -> DigitList -> Lists of solutions
# rings :: Bool -> [Int] -> [[Int]]
def rings(uniq):
'''Sets of unique or non-unique integer values
(drawn from the `digits` argument)
for each of the seven names [a..g] such that:
(a + b) == (b + c + d) == (d + e + f) == (f + g)
'''
def go(digits):
ns = sorted(digits, reverse=True)
h = ns[0]
 
# CENTRAL DIGIT :: d
def central(d):
xs = list(filter(lambda x: h >= (d + x), ns))
 
# LEFT NEIGHBOUR AND LEFTMOST :: c and a
def left(c):
a = c + d
if a > h:
return []
else:
# RIGHT NEIGHBOUR AND RIGHTMOST :: e and g
def right(e):
g = d + e
if ((g > h) or (uniq and (g == c))):
return []
else:
agDelta = a - g
bfs = difference(ns)(
[d, c, e, g, a]
) if uniq else ns
 
# MID LEFT AND RIGHT :: b and f
def midLeftRight(b):
f = b + agDelta
return [[a, b, c, d, e, f, g]] if (
(f in bfs) and (
(not uniq) or (
f not in [a, b, c, d, e, g]
)
)
) else []
 
# CANDIDATE DIGITS BOUND TO POSITIONS [a .. g] --------
 
return concatMap(midLeftRight)(bfs)
 
return concatMap(right)(
difference(xs)([d, c, a]) if uniq else ns
)
 
return concatMap(left)(
delete(d)(xs) if uniq else ns
)
 
return concatMap(central)(ns)
 
return lambda digits: go(digits) if digits else []
 
 
# TEST ----------------------------------------------------
# main :: IO ()
def main():
'''Testing unique digits [1..7], [3..9] and unrestricted digits'''
 
print(main.__doc__ + ':\n')
print(unlines(map(
lambda tpl: '\nrings' + repr(tpl) + ':\n\n' + unlines(
map(repr, uncurry(rings)(*tpl))
), [
(True, enumFromTo(1)(7)),
(True, enumFromTo(3)(9))
]
)))
tpl = (False, enumFromTo(0)(9))
print(
'\n\nlen(rings' + repr(tpl) + '):\n\n' +
str(len(uncurry(rings)(*tpl)))
)
 
 
# GENERIC -------------------------------------------------
 
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''A concatenated list over which a function has been mapped.
The list monad can be derived by using a function f which
wraps its output in a list,
(using an empty list to represent computational failure).
'''
return lambda xs: list(
chain.from_iterable(map(f, xs))
)
 
 
# delete :: Eq a => a -> [a] -> [a]
def delete(x):
'''xs with the first of any instances of x removed.'''
def go(xs):
xs.remove(x)
return xs
return lambda xs: go(list(xs)) if (
x in xs
) else list(xs)
 
 
# difference :: Eq a => [a] -> [a] -> [a]
def difference(xs):
'''All elements of ys except any also found in xs'''
def go(ys):
s = set(ys)
return [x for x in xs if x not in s]
return lambda ys: go(ys)
 
 
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
 
 
# uncurry :: (a -> b -> c) -> ((a, b) -> c)
def uncurry(f):
'''A function over a pair of arguments,
derived from a vanilla or curried function.
'''
return lambda x, y: f(x)(y)
 
 
# unlines :: [String] -> String
def unlines(xs):
'''A single string formed by the intercalation
of a list of strings with the newline character.
'''
return '\n'.join(xs)
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>Testing unique digits [1..7], [3..9] and unrestricted digits:
 
rings(True, [1, 2, 3, 4, 5, 6, 7]):
 
[7, 3, 2, 5, 1, 4, 6]
[6, 4, 1, 5, 2, 3, 7]
[5, 6, 2, 3, 1, 7, 4]
[4, 7, 1, 3, 2, 6, 5]
[7, 2, 6, 1, 3, 5, 4]
[6, 4, 5, 1, 2, 7, 3]
[4, 5, 3, 1, 6, 2, 7]
[3, 7, 2, 1, 5, 4, 6]
 
rings(True, [3, 4, 5, 6, 7, 8, 9]):
 
[9, 6, 4, 5, 3, 7, 8]
[8, 7, 3, 5, 4, 6, 9]
[9, 6, 5, 4, 3, 8, 7]
[7, 8, 3, 4, 5, 6, 9]
 
 
len(rings(False, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])):
 
2860</pre>
 
=={{header|R}}==
Function "perms" is a modified version of the "permutations" function from the "gtools" R package.
<syntaxhighlight lang="r"># 4 rings or 4 squares puzzle
 
perms <- function (n, r, v = 1:n, repeats.allowed = FALSE) {
if (repeats.allowed)
sub <- function(n, r, v) {
if (r == 1)
matrix(v, n, 1)
else if (n == 1)
matrix(v, 1, r)
else {
inner <- Recall(n, r - 1, v)
cbind(rep(v, rep(nrow(inner), n)), matrix(t(inner),
ncol = ncol(inner), nrow = nrow(inner) * n,
byrow = TRUE))
}
}
else sub <- function(n, r, v) {
if (r == 1)
matrix(v, n, 1)
else if (n == 1)
matrix(v, 1, r)
else {
X <- NULL
for (i in 1:n) X <- rbind(X, cbind(v[i], Recall(n - 1, r - 1, v[-i])))
X
}
}
X <- sub(n, r, v[1:n])
result <- vector(mode = "numeric")
 
for(i in 1:nrow(X)){
y <- X[i, ]
x1 <- y[1] + y[2]
x2 <- y[2] + y[3] + y[4]
x3 <- y[4] + y[5] + y[6]
x4 <- y[6] + y[7]
if(x1 == x2 & x2 == x3 & x3 == x4) result <- rbind(result, y)
}
return(result)
}
 
print_perms <- function(n, r, v = 1:n, repeats.allowed = FALSE, table.out = FALSE) {
a <- perms(n, r, v, repeats.allowed)
colnames(a) <- rep("", ncol(a))
rownames(a) <- rep("", nrow(a))
if(!repeats.allowed){
print(a)
cat(paste('\n', nrow(a), 'unique solutions from', min(v), 'to', max(v)))
} else {
cat(paste('\n', nrow(a), 'non-unique solutions from', min(v), 'to', max(v)))
}
}
 
registerS3method("print_perms", "data.frame", print_perms)
 
print_perms(7, 7, repeats.allowed = FALSE, table.out = TRUE)
print_perms(7, 7, v = 3:9, repeats.allowed = FALSE, table.out = TRUE)
print_perms(10, 7, v = 0:9, repeats.allowed = TRUE, table.out = FALSE)
 
</syntaxhighlight>
{{Out}}
<pre>
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
 
8 unique solutions from 1 to 7
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
4 unique solutions from 3 to 9
 
2860 non-unique solutions from 0 to 9
</pre>
 
=={{header|Racket}}==
 
Using a folder, so we can count as well as produce lists of results
 
<syntaxhighlight lang="racket">#lang racket
 
(define solution? (match-lambda [(list a b c d e f g) (= (+ a b) (+ b c d) (+ d e f) (+ f g))]))
 
(define (fold-4-rings-or-4-squares-puzzle lo hi kons k0)
(for*/fold ((k k0))
((combination (in-combinations (range lo (add1 hi)) 7))
(permutation (in-permutations combination))
#:when (solution? permutation))
(kons permutation k)))
 
(fold-4-rings-or-4-squares-puzzle 1 7 cons null)
(fold-4-rings-or-4-squares-puzzle 3 9 cons null)
(fold-4-rings-or-4-squares-puzzle 0 9 (λ (ignored-solution count) (add1 count)) 0)</syntaxhighlight>
 
{{out}}
 
<pre>'((6 4 1 5 2 3 7) (4 5 3 1 6 2 7) (3 7 2 1 5 4 6) (7 3 2 5 1 4 6) (4 7 1 3 2 6 5) (5 6 2 3 1 7 4) (7 2 6 1 3 5 4) (6 4 5 1 2 7 3))
'((7 8 3 4 5 6 9) (8 7 3 5 4 6 9) (9 6 4 5 3 7 8) (9 6 5 4 3 8 7))
192</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2016.12}}
 
<syntaxhighlight lang="raku" line>sub four-squares ( @list, :$unique=1, :$show=1 ) {
 
my @solutions;
 
for $unique.&combos -> @c {
@solutions.push: @c if [==]
@c[0] + @c[1],
@c[1] + @c[2] + @c[3],
@c[3] + @c[4] + @c[5],
@c[5] + @c[6];
}
 
say +@solutions, ($unique ?? ' ' !! ' non-'), "unique solutions found using {join(', ', @list)}.\n";
 
my $f = "%{@list.max.chars}s";
 
say join "\n", (('a'..'g').fmt: $f), @solutions».fmt($f), "\n" if $show;
 
multi combos ( $ where so * ) { @list.combinations(7).map: |*.permutations }
 
multi combos ( $ where not * ) { [X] @list xx 7 }
}
 
# TASK
four-squares( [1..7] );
four-squares( [3..9] );
four-squares( [8, 9, 11, 12, 17, 18, 20, 21] );
four-squares( [0..9], :unique(0), :show(0) );</syntaxhighlight>
 
{{out}}
 
<pre>8 unique solutions found using 1, 2, 3, 4, 5, 6, 7.
 
a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
 
 
4 unique solutions found using 3, 4, 5, 6, 7, 8, 9.
 
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
 
8 unique solutions found using 8, 9, 11, 12, 17, 18, 20, 21.
 
a b c d e f g
17 21 8 9 11 18 20
20 18 11 9 8 21 17
17 21 9 8 12 18 20
20 18 8 12 9 17 21
20 18 12 8 9 21 17
21 17 9 12 8 18 20
20 18 11 9 12 17 21
21 17 12 9 11 18 20
 
 
2860 non-unique solutions found using 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
</pre>
 
Line 2,508 ⟶ 6,034:
This REXX version is faster than the more idiomatic version, but is longer (statement-wise) and
<br>a bit easier to read (visualize).
<langsyntaxhighlight lang="rexx">/*REXX pgm solves the 4-rings puzzle, where letters represent unique (or not) digits). */
arg LO HI unique show . /*the ARG statement capitalizes args.*/
if LO=='' | LO=="," then LO=1 /*Not specified? Then use the default.*/
Line 2,577 ⟶ 6,103:
if show then say left('',9) center(a1,w) center(a2,w) center(a3,w) center(a4,w),
center(a5,w) center(a6,w) center(a7,w)
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs: &nbsp; <tt> &nbsp; 1 &nbsp; 7 </tt>}}
<pre>
Line 2,614 ⟶ 6,140:
Note that the REXX language doesn't have short-circuits &nbsp; (when executing multiple clauses
in &nbsp; <big> '''if''' </big> &nbsp; (and other) &nbsp; statements.
<langsyntaxhighlight lang="rexx">/*REXX pgm solves the 4-rings puzzle, where letters represent unique (or not) digits). */
arg LO HI unique show . /*the ARG statement capitalizes args.*/
if LO=='' | LO=="," then LO=1 /*Not specified? Then use the default.*/
Line 2,655 ⟶ 6,181:
if show then say left('',9) center(a1,w) center(a2,w) center(a3,w) center(a4,w),
center(a5,w) center(a6,w) center(a7,w)
return</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the faster REXX version.}} <br><br>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def four_squares(low, high, unique=true, show=unique)
f = -> (a,b,c,d,e,f,g) {[a+b, b+c+d, d+e+f, f+g].uniq.size == 1}
if unique
Line 2,679 ⟶ 6,205:
four_squares(low, high)
end
four_squares(0, 9, false)</langsyntaxhighlight>
 
{{out}}
Line 2,705 ⟶ 6,231:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
#![feature(inclusive_range_syntax)]
 
Line 2,780 ⟶ 6,306:
nonuniques(0, 9);
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,801 ⟶ 6,327:
2860 non-unique solutions in 0 to 9 range
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="scala">object FourRings {
def fourSquare(low: Int, high: Int, unique: Boolean, print: Boolean): Unit = {
def isValid(needle: Integer, haystack: Integer*) = !unique || !haystack.contains(needle)
 
if (print) println("a b c d e f g")
 
var count = 0
for {
a <- low to high
b <- low to high if isValid(a, b)
fp = a + b
c <- low to high if isValid(c, a, b)
d <- low to high if isValid(d, a, b, c) && fp == b + c + d
e <- low to high if isValid(e, a, b, c, d)
f <- low to high if isValid(f, a, b, c, d, e) && fp == d + e + f
g <- low to high if isValid(g, a, b, c, d, e, f) && fp == f + g
} {
count += 1
if (print) println(s"$a $b $c $d $e $f $g")
}
println(s"There are $count ${if(unique) "unique" else "non-unique"} solutions in [$low, $high]")
}
def main(args: Array[String]): Unit = {
fourSquare(1, 7, unique = true, print = true)
fourSquare(3, 9, unique = true, print = true)
fourSquare(0, 9, unique = false, print = false)
}
}</syntaxhighlight>
{{out}}
<pre>a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
There are 8 unique solutions in [1, 7]
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
There are 4 unique solutions in [3, 9]
There are 2860 non-unique solutions in [0, 9]</pre>
 
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
Line 2,852 ⟶ 6,430:
(display (count (lambda (combination) (apply solution? combination))
(combinations 7 (iota 10 0) #f))) (newline)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,863 ⟶ 6,441:
2860
</pre>
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">func four_squares (list, unique=true, show=true) {
 
var solutions = []
Line 2,907 ⟶ 6,486:
four_squares(@(3..9))
four_squares([8, 9, 11, 12, 17, 18, 20, 21])
four_squares(@(0..9), unique: false, show: false)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,946 ⟶ 6,525:
 
=={{header|Simula}}==
<langsyntaxhighlight simulalang="modula2">BEGIN
 
INTEGER PROCEDURE GETCOMBS(LOW, HIGH, UNIQUE, COMBS);
Line 3,060 ⟶ 6,639:
 
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>8 UNIQUE SOLUTIONS IN 1 TO 7
Line 3,077 ⟶ 6,656:
[ 9 6 5 4 3 8 7 ]
2860 SOLUTIONS IN 0 TO 9
</pre>
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<syntaxhighlight lang="sql pl">
--#SET TERMINATOR @
 
SET SERVEROUTPUT ON @
 
CREATE TABLE ALL_INTS (
V INTEGER
)@
 
CREATE TABLE RESULTS (
A INTEGER,
B INTEGER,
C INTEGER,
D INTEGER,
E INTEGER,
F INTEGER,
G INTEGER
)@
CREATE OR REPLACE PROCEDURE FOUR_SQUARES(
IN LO INTEGER,
IN HI INTEGER,
IN UNIQ SMALLINT,
--IN UNIQ BOOLEAN,
IN SHOW SMALLINT)
--IN SHOW BOOLEAN)
BEGIN
DECLARE A INTEGER;
DECLARE B INTEGER;
DECLARE C INTEGER;
DECLARE D INTEGER;
DECLARE E INTEGER;
DECLARE F INTEGER;
DECLARE G INTEGER;
DECLARE OUT_LINE VARCHAR(2000);
DECLARE I SMALLINT;
DECLARE SOLUTIONS INTEGER;
DECLARE UORN VARCHAR(2000);
 
SET SOLUTIONS = 0;
DELETE FROM ALL_INTS;
DELETE FROM RESULTS;
SET I = LO;
WHILE (I <= HI) DO
INSERT INTO ALL_INTS VALUES (I);
SET I = I + 1;
END WHILE;
COMMIT;
-- Computes unique solutions.
IF (UNIQ = 0) THEN
--IF (UNIQ = TRUE) THEN
INSERT INTO RESULTS
SELECT
A.V A, B.V B, C.V C, D.V D, E.V E, F.V F, G.V G
FROM
ALL_INTS A, ALL_INTS B, ALL_INTS C, ALL_INTS D, ALL_INTS E, ALL_INTS F,
ALL_INTS G
WHERE
A.V NOT IN (B.V, C.V, D.V, E.V, F.V, G.V)
AND B.V NOT IN (C.V, D.V, E.V, F.V, G.V)
AND C.V NOT IN (D.V, E.V, F.V, G.V)
AND D.V NOT IN (E.V, F.V, G.V)
AND E.V NOT IN (F.V, G.V)
AND F.V NOT IN (G.V)
AND A.V = C.V + D.V
AND G.V = D.V + E.V
AND B.V = E.V + F.V - C.V
ORDER BY
A, B, C, D, E, F, G;
SET UORN = ' unique solutions in ';
ELSE
-- Compute non-unique solutions.
INSERT INTO RESULTS
SELECT
A.V A, B.V B, C.V C, D.V D, E.V E, F.V F, G.V G
FROM
ALL_INTS A, ALL_INTS B, ALL_INTS C, ALL_INTS D, ALL_INTS E, ALL_INTS F,
ALL_INTS G
WHERE
A.V = C.V + D.V
AND G.V = D.V + E.V
AND B.V = E.V + F.V - C.V
ORDER BY
A, B, C, D, E, F, G;
SET UORN = ' non-unique solutions in ';
END IF;
COMMIT;
-- Counts the possible solutions.
FOR v AS c CURSOR FOR
SELECT
A, B, C, D, E, F, G
FROM RESULTS
ORDER BY
A, B, C, D, E, F, G
DO
SET SOLUTIONS = SOLUTIONS + 1;
-- Shows the results.
IF (SHOW = 0) THEN
--IF (SHOW = TRUE) THEN
SET OUT_LINE = A || ' ' || B || ' ' || C || ' ' || D || ' ' || E || ' '
|| F ||' ' || G;
CALL DBMS_OUTPUT.PUT_LINE(OUT_LINE);
END IF;
END FOR;
 
SET OUT_LINE = SOLUTIONS || UORN || LO || ' to ' || HI;
CALL DBMS_OUTPUT.PUT_LINE(OUT_LINE);
END
@
 
CALL FOUR_SQUARES(1, 7, 0, 0)@
CALL FOUR_SQUARES(3, 9, 0, 0)@
CALL FOUR_SQUARES(0, 9, 1, 1)@
</syntaxhighlight>
Output:
<pre>
db2 -td@
db2 => CREATE TABLE ALL_INTS ( V INTEGER )
DB20000I The SQL command completed successfully.
 
db2 => CREATE TABLE RESULTS ( A INTEGER, B INTEGER, C INTEGER, D INTEGER, E INTEGER, F INTEGER, G INTEGER )
DB20000I The SQL command completed successfully.
 
db2 => CREATE OR REPLACE PROCEDURE FOUR_SQUARES(
...
db2 (cont.) => END @
DB20000I The SQL command completed successfully.
 
db2 => CALL FOUR_SQUARES(1, 7, 0, 0)
 
Return Status = 0
 
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
8 unique solutions in 1 TO 7
 
db2 => CALL FOUR_SQUARES(3, 9, 0, 0)
 
Return Status = 0
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
4 unique solutions in 3 TO 9
 
CALL FOUR_SQUARES(0, 9, 1, 1)
 
Return Status = 0
 
2860 non-unique solutions in 0 TO 9
</pre>
 
Line 3,082 ⟶ 6,826:
Use the program '''perm''' in the [[Permutations]] task for the first two questions, as it's fast enough. Use '''joinby''' for the third.
 
<langsyntaxhighlight lang="stata">perm 7
rename * (a b c d e f g)
list if a==c+d & b+c==e+f & d+e==g, noobs sep(50)
Line 3,133 ⟶ 6,877:
erase temp.dta
count
2,860</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 3,142 ⟶ 6,886:
The puzzle can be varied freely by changing the values of <tt>$vars</tt> and <tt>$exprs</tt> specified at the top of the script.
 
<langsyntaxhighlight Tcllang="tcl">set vars {a b c d e f g}
set exprs {
{$a+$b}
Line 3,216 ⟶ 6,960:
solve_4rings $vars $exprs [range 3 9]
puts "# Number of solutions, free over 0..9:"
puts [solve_4rings_hard $vars $exprs [range 0 9]]</langsyntaxhighlight>
 
{{out}}
Line 3,235 ⟶ 6,979:
# Number of solutions, free over 0..9:
2860</pre>
 
=={{header|VBA}}==
{{trans|C}}
<syntaxhighlight lang="vb">Dim a As Integer, b As Integer, c As Integer, d As Integer
Dim e As Integer, f As Integer, g As Integer
Dim lo As Integer, hi As Integer, unique As Boolean, show As Boolean
Dim solutions As Integer
Private Sub bf()
For f = lo To hi
If ((Not unique) Or _
((f <> a And f <> c And f <> d And f <> g And f <> e))) Then
b = e + f - c
If ((b >= lo) And (b <= hi) And _
((Not unique) Or ((b <> a) And (b <> c) And _
(b <> d) And (b <> g) And (b <> e) And (b <> f)))) Then
solutions = solutions + 1
If show Then Debug.Print a; b; c; d; e; f; g
End If
End If
Next
End Sub
Private Sub ge()
For e = lo To hi
If ((Not unique) Or ((e <> a) And (e <> c) And (e <> d))) Then
g = d + e
If ((g >= lo) And (g <= hi) And _
((Not unique) Or ((g <> a) And (g <> c) And _
(g <> d) And (g <> e)))) Then
bf
End If
End If
Next
End Sub
Private Sub acd()
For c = lo To hi
For d = lo To hi
If ((Not unique) Or (c <> d)) Then
a = c + d
If ((a >= lo) And (a <= hi) And _
((Not unique) Or ((c <> 0) And (d <> 0)))) Then
ge
End If
End If
Next d
Next c
End Sub
Private Sub foursquares(plo As Integer, phi As Integer, punique As Boolean, pshow As Boolean)
lo = plo
hi = phi
unique = punique
show = pshow
solutions = 0
acd
Debug.Print
If unique Then
Debug.Print solutions; " unique solutions in"; lo; "to"; hi
Else
Debug.Print solutions; " non-unique solutions in"; lo; "to"; hi
End If
End Sub
Public Sub program()
Call foursquares(1, 7, True, True)
Debug.Print
Call foursquares(3, 9, True, True)
Call foursquares(0, 9, False, False)
End Sub
</syntaxhighlight>
{{out}}
<pre>
4 7 1 3 2 6 5
6 4 1 5 2 3 7
3 7 2 1 5 4 6
5 6 2 3 1 7 4
7 3 2 5 1 4 6
4 5 3 1 6 2 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
 
8 unique solutions in 1 to 7
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
4 unique solutions in 3 to 9
 
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|Visual Basic .NET}}==
Similar to the other brute-force algorithims, but with a couple of enhancements. A "used" list is maintained to simplify checking of the nested variables overlap. Also the ''d'', ''f'' and ''g'' '''For Each''' loops are constrained by the other variables instead of blindly going through all combinations.
<syntaxhighlight lang="vbnet">Module Module1
 
Dim CA As Char() = "0123456789ABC".ToCharArray()
 
Sub FourSquare(lo As Integer, hi As Integer, uni As Boolean, sy As Char())
If sy IsNot Nothing Then Console.WriteLine("a b c d e f g" & vbLf & "-------------")
Dim r = Enumerable.Range(lo, hi - lo + 1).ToList(), u As New List(Of Integer),
t As Integer, cn As Integer = 0
For Each a In r
u.Add(a)
For Each b In r
If uni AndAlso u.Contains(b) Then Continue For
u.Add(b)
t = a + b
For Each c In r : If uni AndAlso u.Contains(c) Then Continue For
u.Add(c)
For d = a - c To a - c
If d < lo OrElse d > hi OrElse uni AndAlso u.Contains(d) OrElse
t <> b + c + d Then Continue For
u.Add(d)
For Each e In r
If uni AndAlso u.Contains(e) Then Continue For
u.Add(e)
For f = b + c - e To b + c - e
If f < lo OrElse f > hi OrElse uni AndAlso u.Contains(f) OrElse
t <> d + e + f Then Continue For
u.Add(f)
For g = t - f To t - f : If g < lo OrElse g > hi OrElse
uni AndAlso u.Contains(g) Then Continue For
cn += 1 : If sy IsNot Nothing Then _
Console.WriteLine("{0} {1} {2} {3} {4} {5} {6}",
sy(a), sy(b), sy(c), sy(d), sy(e), sy(f), sy(g))
Next : u.Remove(f) : Next : u.Remove(e) : Next : u.Remove(d)
Next : u.Remove(c) : Next : u.Remove(b) : Next : u.Remove(a)
Next : Console.WriteLine("{0} {1}unique solutions for [{2},{3}]{4}",
cn, If(uni, "", "non-"), lo, hi, vbLf)
End Sub
 
Sub main()
fourSquare(1, 7, True, CA)
fourSquare(3, 9, True, CA)
fourSquare(0, 9, False, Nothing)
fourSquare(5, 12, True, CA)
End Sub
 
End Module</syntaxhighlight>
{{out}}
Added the zkl example for [5,12]<pre>a b c d e f g
-------------
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
8 unique solutions for [1,7]
 
a b c d e f g
-------------
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
4 unique solutions for [3,9]
 
2860 non-unique solutions for [0,9]
 
a b c d e f g
-------------
B 9 6 5 7 8 C
B A 6 5 7 9 C
C 8 7 5 6 9 B
C 9 7 5 6 A B
4 unique solutions for [5,12]</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn main(){
mut n, mut c := get_combs(1,7,true)
println("$n unique solutions in 1 to 7")
println(c)
n, c = get_combs(3,9,true)
println("$n unique solutions in 3 to 9")
println(c)
n, _ = get_combs(0,9,false)
println("$n non-unique solutions in 0 to 9")
}
fn get_combs(low int,high int,unique bool) (int, [][]int) {
mut num := 0
mut valid_combs := [][]int{}
for a := low; a <= high; a++ {
for b := low; b <= high; b++ {
for c := low; c <= high; c++ {
for d := low; d <= high; d++ {
for e := low; e <= high; e++ {
for f := low; f <= high; f++ {
for g := low; g <= high; g++ {
if valid_comb(a,b,c,d,e,f,g) {
if !unique || is_unique(a,b,c,d,e,f,g) {
num++
valid_combs << [a,b,c,d,e,f,g]
}
}
}
}
}
}
}
}
}
return num, valid_combs
}
fn is_unique(a int,b int,c int,d int,e int,f int,g int) bool {
mut data := map[int]int{}
data[a]++
data[b]++
data[c]++
data[d]++
data[e]++
data[f]++
data[g]++
return data.len == 7
}
fn valid_comb(a int,b int,c int,d int,e int,f int,g int) bool {
square1 := a + b
square2 := b + c + d
square3 := d + e + f
square4 := f + g
return square1 == square2 && square2 == square3 && square3 == square4
}</syntaxhighlight>
 
{{out}}
<pre>
8 unique solutions in 1 to 7
[[3, 7, 2, 1, 5, 4, 6], [4, 5, 3, 1, 6, 2, 7], [4, 7, 1, 3, 2, 6, 5], [5, 6, 2, 3, 1, 7, 4], [6, 4, 1, 5, 2, 3, 7], [6, 4, 5, 1, 2, 7, 3], [7, 2, 6, 1, 3, 5, 4], [7, 3, 2, 5, 1, 4, 6]]
4 unique solutions in 3 to 9
[[7, 8, 3, 4, 5, 6, 9], [8, 7, 3, 5, 4, 6, 9], [9, 6, 4, 5, 3, 7, 8], [9, 6, 5, 4, 3, 8, 7]]
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|Wren}}==
{{trans|C}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var a = 0
var b = 0
var c = 0
var d = 0
var e = 0
var f = 0
var g = 0
 
var lo
var hi
var unique
var show
var solutions
 
var bf = Fn.new {
f = lo
while (f <= hi) {
if (!unique || (f != a && f != c && f != d && f != e && f != g)) {
b = e + f - c
if (b >= lo && b <= hi &&
(!unique || (b != a && b != c && b != d && b != e && b != f && b != g))) {
solutions = solutions + 1
if (show) Fmt.lprint("$d $d $d $d $d $d $d", [a, b, c, d, e, f, g])
}
}
f = f + 1
}
}
 
var ge = Fn.new {
e = lo
while (e <= hi) {
if (!unique || (e != a && e != c && e != d)) {
g = d + e
if (g >= lo && g <= hi &&
(!unique || (g != a && g != c && g != d && g != e))) bf.call()
}
e = e + 1
}
}
 
var acd = Fn.new {
c = lo
while (c <= hi) {
d = lo
while (d <= hi) {
if (!unique || c != d) {
a = c + d
if (a >= lo && a <= hi && (!unique || (c != 0 && d != 0))) ge.call()
}
d = d + 1
}
c = c + 1
}
}
 
var foursquares = Fn.new { |plo, phi, punique, pshow|
lo = plo
hi = phi
unique = punique
show = pshow
solutions = 0
if (show) {
System.print("\na b c d e f g")
System.print("-------------")
}
acd.call()
if (unique) {
Fmt.print("\n$d unique solutions in $d to $d", solutions, lo, hi)
} else {
Fmt.print("\n$d non-unique solutions in $d to $d\n", solutions, lo, hi)
}
}
 
foursquares.call(1, 7, true, true)
foursquares.call(3, 9, true, true)
foursquares.call(0, 9, false, false)</syntaxhighlight>
 
{{out}}
<pre>
a b c d e f g
-------------
4 7 1 3 2 6 5
6 4 1 5 2 3 7
3 7 2 1 5 4 6
5 6 2 3 1 7 4
7 3 2 5 1 4 6
4 5 3 1 6 2 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
 
8 unique solutions in 1 to 7
 
a b c d e f g
-------------
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
4 unique solutions in 3 to 9
 
2860 non-unique solutions in 0 to 9
</pre>
 
=={{header|X86 Assembly}}==
See [[4-rings_or_4-squares_puzzle/X86 Assembly]]
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">int Show, Low, High, Digit(7\a..g\), Count;
proc Rings(Level);
int Level; \of recursion
int D, Temp, I, Set;
[for D:= Low to High do
[Digit(Level):= D;
if Level < 7-1 then Rings(Level+1)
else [ Temp:= Digit(0) + Digit(1); \solution?
if Temp = Digit(1) + Digit(2) + Digit(3) and
Temp = Digit(3) + Digit(4) + Digit(5) and
Temp = Digit(5) + Digit(6) then
[Count:= Count+1;
if Show then
[Set:= 0; \digits must be unique
for I:= 0 to 7-1 do
Set:= Set ! 1<<Digit(I);
if Set = %111_1111 << Low then
[for I:= 0 to 7-1 do
[IntOut(0, Digit(I)); ChOut(0, ^ )];
CrLf(0);
];
];
];
];
];
];
 
[Show:= true;
Low:= 1; High:= 7;
Rings(0);
CrLf(0);
Low:= 3; High:= 9;
Rings(0);
CrLf(0);
Show:= false;
Low:= 0; High:= 9; Count:= 0;
Rings(0);
IntOut(0, Count);
CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
 
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
 
2860
</pre>
 
=={{header|Yabasic}}==
{{trans|D}}
<syntaxhighlight lang="yabasic">fourSquare(1,7,true,true)
fourSquare(3,9,true,true)
fourSquare(0,9,false,false)
 
sub fourSquare(low, high, unique, prin)
local count, a, b, c, d, e, f, g, fp
if (prin) print "a b c d e f g"
 
for a = low to high
for b = low to high
if (not valid(unique, a, b)) continue
fp = a+b
for c = low to high
if (not valid(unique, c, a, b)) continue
for d = low to high
if (not valid(unique, d, a, b, c)) continue
if (fp <> b+c+d) continue
for e = low to high
if (not valid(unique, e, a, b, c, d)) continue
for f = low to high
if (not valid(unique, f, a, b, c, d, e)) continue
if (fp <> d+e+f) continue
for g = low to high
if (not valid(unique, g, a, b, c, d, e, f)) continue
if (fp <> f+g) continue
count = count + 1
if (prin) print a," ",b," ",c," ",d," ",e," ",f," ",g
next
next
next
next
next
next
next
if (unique) then
print "There are ", count, " unique solutions in [",low,",",high,"]"
else
print "There are ", count, " non-unique solutions in [",low,",",high,"]"
end if
end sub
sub valid(unique, needle, n1, n2, n3, n4, n5, n6)
local i
if (unique) then
for i = 1 to numparams - 2
switch i
case 1: if needle = n1 return false : break
case 2: if needle = n2 return false : break
case 3: if needle = n3 return false : break
case 4: if needle = n4 return false : break
case 5: if needle = n5 return false : break
case 6: if needle = n6 return false : break
end switch
next
end if
return true
end sub</syntaxhighlight>
{{out}}
<pre>a b c d e f g
3 7 2 1 5 4 6
4 5 3 1 6 2 7
4 7 1 3 2 6 5
5 6 2 3 1 7 4
6 4 1 5 2 3 7
6 4 5 1 2 7 3
7 2 6 1 3 5 4
7 3 2 5 1 4 6
There are 8 unique solutions in [1,7]
a b c d e f g
7 8 3 4 5 6 9
8 7 3 5 4 6 9
9 6 4 5 3 7 8
9 6 5 4 3 8 7
There are 4 unique solutions in [3,9]
There are 2860 non-unique solutions in [0,9]</pre>
 
=={{header|Zig}}==
{{trans|Go}}
This is a direct translation of the Go solution - the Zig implementation
having manual memory management and Zig not ignoring errors or return values.
 
<syntaxhighlight lang="zig">const std = @import("std");
const Allocator = std.mem.Allocator;
</syntaxhighlight><syntaxhighlight lang="zig">
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
 
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const ok = gpa.deinit();
std.debug.assert(ok == .ok);
}
const allocator = gpa.allocator();
 
{
const nc = try getCombs(allocator, 1, 7, true);
defer allocator.free(nc.combinations);
try stdout.print("{d} unique solutions in 1 to 7\n", .{nc.num});
try stdout.print("{any}\n", .{nc.combinations});
}
{
const nc = try getCombs(allocator, 3, 9, true);
defer allocator.free(nc.combinations);
try stdout.print("{d} unique solutions in 3 to 9\n", .{nc.num});
try stdout.print("{any}\n", .{nc.combinations});
}
{
const nc = try getCombs(allocator, 0, 9, false);
defer allocator.free(nc.combinations);
try stdout.print("{d} non-unique solutions in 0 to 9\n", .{nc.num});
}
}
</syntaxhighlight><syntaxhighlight lang="zig">
/// Caller owns combinations slice memory.
fn getCombs(allocator: Allocator, low: u16, high: u16, unique: bool) !struct { num: usize, combinations: [][7]usize } {
var num: usize = 0;
var valid_combinations = std.ArrayList([7]usize).init(allocator);
for (low..high + 1) |a|
for (low..high + 1) |b|
for (low..high + 1) |c|
for (low..high + 1) |d|
for (low..high + 1) |e|
for (low..high + 1) |f|
for (low..high + 1) |g|
if (validComb(a, b, c, d, e, f, g))
if (!unique or try isUnique(allocator, a, b, c, d, e, f, g)) {
num += 1;
try valid_combinations.append([7]usize{ a, b, c, d, e, f, g });
};
return .{ .num = num, .combinations = try valid_combinations.toOwnedSlice() };
}
</syntaxhighlight><syntaxhighlight lang="zig">
fn isUnique(allocator: Allocator, a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize) !bool {
var data = std.AutoArrayHashMap(usize, void).init(allocator);
defer data.deinit();
try data.put(a, {});
try data.put(b, {});
try data.put(c, {});
try data.put(d, {});
try data.put(e, {});
try data.put(f, {});
try data.put(g, {});
return data.count() == 7;
}
</syntaxhighlight><syntaxhighlight lang="zig">
fn validComb(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize) bool {
const square1 = a + b;
const square2 = b + c + d;
const square3 = d + e + f;
const square4 = f + g;
return square1 == square2 and square2 == square3 and square3 == square4;
}</syntaxhighlight>
{{out}}
<pre>8 unique solutions in 1 to 7
{ { 3, 7, 2, 1, 5, 4, 6 }, { 4, 5, 3, 1, 6, 2, 7 }, { 4, 7, 1, 3, 2, 6, 5 }, { 5, 6, 2, 3, 1, 7, 4 }, { 6, 4, 1, 5, 2, 3, 7 }, { 6, 4, 5, 1, 2, 7, 3 }, { 7, 2, 6, 1, 3, 5, 4 }, { 7, 3, 2, 5, 1, 4, 6 } }
4 unique solutions in 3 to 9
{ { 7, 8, 3, 4, 5, 6, 9 }, { 8, 7, 3, 5, 4, 6, 9 }, { 9, 6, 4, 5, 3, 7, 8 }, { 9, 6, 5, 4, 3, 8, 7 } }
2860 non-unique solutions in 0 to 9</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl"> // unique: No repeated numbers in solution
fcn fourSquaresPuzzle(lo=1,hi=7,unique=True){ //-->list of solutions
_assert_(0<=lo and hi<36);
Line 3,259 ⟶ 7,576:
}
s
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn show(solutions,msg){
if(not solutions){ println("No solutions for",msg); return(); }
 
Line 3,274 ⟶ 7,591:
fourSquaresPuzzle(5,12) : show(_," unique (5-12)"); println();
println(fourSquaresPuzzle(0,9,False).len(), // 10^7 possibilities
" non-unique (0-9) solutions found.");</langsyntaxhighlight>
{{out}}
<pre>
Line 3,307 ⟶ 7,624:
2860 non-unique (0-9) solutions found.
</pre>
[[Category:Games]]
[[Category:Puzzles]]
Anonymous user