4-rings or 4-squares puzzle: Difference between revisions
Content added Content deleted
Recombinant (talk | contribs) m (→{{header|Zig}}: writer.print() is void (no return value)) |
|||
Line 4,097: | Line 4,097: | ||
Total unique solutions for HIGH 9, LOW 3: 4 |
Total unique solutions for HIGH 9, LOW 3: 4 |
||
Total solutions for HIGH 9, LOW 0: 2860 |
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> |
</pre> |
||