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

Added Wren
mNo edit summary
(Added Wren)
Line 5,705:
C 9 7 5 6 A B
4 unique solutions for [5,12]</pre>
 
=={{header|Wren}}==
{{trans|C}}
{{libheader|Wren-fmt}}
<lang ecmascript>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)</lang>
 
{{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}}==
9,482

edits