4-rings or 4-squares puzzle: Difference between revisions
m
syntax highlighting fixup automation
(Added PL/M) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 47:
{{trans|Python}}
<
V solutions = 0
L(c) lo .. hi
Line 76:
foursquares(1, 7, 1B, 1B)
foursquares(3, 9, 1B, 1B)
foursquares(0, 9, 0B, 0B)</
{{out}}
Line 101:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program square4_64.s */
Line 504:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{out}}
<pre>
Line 541:
=={{header|Ada}}==
<
procedure Puzzle_Square_4 is
Line 608:
Four_Rings (Low => 3, High => 9, Unique => True, Show => True);
Four_Rings (Low => 0, High => 9, Unique => False, Show => False);
end Puzzle_Square_4;</
{{out}}
Line 632:
=={{header|ALGOL 68}}==
As with the REXX solution, we use explicit loops to generate the permutations.
<
# 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 #
Line 710:
four rings( 3, 9, FALSE, TRUE );
four rings( 0, 9, TRUE, FALSE )
END</
{{out}}
<pre>
Line 734:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<
% -- 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 ) %
Line 802:
fourRings( 3, 9, false, true );
fourRings( 0, 9, true, false )
end.</
{{out}}
<pre>
Line 829:
{{Trans|Haskell}}
(Structured search example)
<
on run
Line 1,135:
on unlines(xs)
intercalate(linefeed, xs)
end unlines</
{{Out}}
<pre>rings(true, enumFromTo(1, 7))
Line 1,161:
=={{header|Applesoft BASIC}}==
{{trans|C}}
<
110 PLO = 1:PHI = 7:PUNIQUE = TRUE:PSHOW = TRUE: GOSUB 150"FOURSQUARES"
120 PLO = 3:PHI = 9:PUNIQUE = TRUE:PSHOW = TRUE: GOSUB 150"FOURSQUARES"
Line 1,192:
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</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<
/* ARM assembly Raspberry PI */
Line 1,634:
iMagicNumber: .int 0xCCCCCCCD
</syntaxhighlight>
{{out}}
Line 1,683:
=={{header|AWK}}==
<
# syntax: GAWK -f 4-RINGS_OR_4-SQUARES_PUZZLE.AWK
# converted from C
Line 1,750:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,784:
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.
<
v!g03!:\*`\g01\!`\g00:p05:+g03:p04:_$30g1+:10g\`v1g<,+$p02%2_|#`*8<
>>+\30g-!+20g*!*00g\#v_$40g1+:10g\`^<<1g00p03<<<_$55+:,\."snoitul"v
Line 1,791:
>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^^></
{{out}}
Line 1,829:
=={{header|C}}==
<syntaxhighlight lang=C>
#include <stdio.h>
Line 1,913:
foursquares(0,9,FALSE,FALSE);
}
</syntaxhighlight>
Output
<pre>
Line 1,941:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Linq;
Line 2,002:
}
}
}</
{{out}}
<pre>a b c d e f g
Line 2,023:
=={{header|C++}}==
<
//C++14/17
#include <algorithm>//std::for_each
Line 2,117:
return 0;
}
</syntaxhighlight>
Output
<pre>
Line 2,141:
=={{header|Clojure}}==
<
(defn rings [r & {:keys [unique] :or {unique true}}]
Line 2,151:
(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}}
Line 2,175:
=={{header|Common Lisp}}==
<
(defpackage four-rings
(:use common-lisp)
Line 2,211:
(format t "Number of solutions for Low 0, High 9 non-unique:~%~A~%"
(length (four-rings-solutions 0 9 nil)))))
</syntaxhighlight>
Output:
<pre>
Line 2,240:
=={{header|Crystal}}==
{{trans|Ruby}}
<
a, b, c, d, e, f, g = list
first = a + b
Line 2,266:
four_squares(low, high)
end
four_squares(0, 9, false)</
=={{header|D}}==
<
void main() {
Line 2,331:
}
return true;
}</
{{out}}
Line 2,354:
See [[#Pascal]]
=={{header|F_Sharp|F#}}==
<
(* A simple function to generate the sequence
Nigel Galloway: January 31st., 2017 *)
Line 2,362:
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>
Then:
<
printfn "%d" (Seq.length (N 0 9))
</syntaxhighlight>
{{out}}
<pre>
2860
</pre>
<
(* A simple function to generate the sequence with unique values
Nigel Galloway: January 31st., 2017 *)
Line 2,379:
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>
Then:
<
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>
{{out}}
<pre>
Line 2,396:
</pre>
and:
<
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>
{{out}}
<pre>
Line 2,411:
<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.
<
math.ranges prettyprint sequences sequences.generalizations
sets ;
Line 2,435:
1 7 t report
3 9 t report
0 9 f report</
{{out}}
<pre>
Line 2,464:
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 <
So, a two-part output, and to reduce the blather, two IF-statements. <
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 2,513:
CALL FOURSHOW(0,9,.FALSE.)
END </
Output: not in a neat order because the first variable is not determined first.
<pre>
Line 2,589:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 2,679:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> a b c d e f g
Line 2,712:
=={{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.
<
local fn FourRings( low as long, high as long, unique as BOOL, show as BOOL )
long a, b, c, d, e, f, g
Line 2,790:
HandleEvents
</syntaxhighlight>
{{output}}
<pre style="font-size: 13px">
Line 2,822:
=={{header|Go}}==
<
import "fmt"
Line 2,878:
return square1 == square2 && square2 == square3 && square3 == square4
}
</syntaxhighlight>
{{Out}}
<pre>
Line 2,890:
=={{header|Groovy}}==
{{trans|Java}}
<
static void main(String[] args) {
fourSquare(1, 7, true, true)
Line 2,945:
return unique && Arrays.stream(haystack).anyMatch({ p -> p == needle })
}
}</
{{out}}
<pre>a b c d e f g
Line 2,967:
=={{header|Haskell}}==
====By exhaustive search====
<
import Control.Monad
Line 3,007:
fourRings 1 7 False True
fourRings 3 9 False True
fourRings 0 9 True False</
{{out}}
Line 3,039:
Nesting four bind operators (>>=), we can then build the set of solutions in the order: queens, left bishops and rooks, right bishops and rooks, knights.
Probably less readable, but already fast, and could be further optimised.
<
--------------- 4 RINGS OR 4 SQUARES PUZZLE --------------
Line 3,139:
( "length (rings False [0 .. 9])",
[length (rings False [0 .. 9])]
)</
{{Out}}
<pre>rings True [1 .. 7]
Line 3,167:
Implementation for the unique version of the puzzle:
<
range=: x+i.1+y-x
lo=. 6+3*x
Line 3,185:
end.
end.
)</
Implementation for the non-unique version of the puzzle:
<
range=: x+i.1+y-x
lo=. 3*x
Line 3,205:
end.
end.
)</
Task examples:
<
4 5 3 1 6 2 7
7 2 6 1 3 5 4
Line 3,224:
9 6 5 4 3 8 7
#0 fspuz2 9
2860</
=={{header|Java}}==
Uses java 8 features.
<
public class FourSquares {
Line 3,285:
return unique && Arrays.stream(haystack).anyMatch(p -> p == needle);
}
}</
{{out}}
<pre>a b c d e f g
Line 3,308:
===ES6===
{{Trans|Haskell}} (Structured search version)
<
"use strict";
Line 3,478:
// MAIN ---
return main();
})();</
{{Out}}
<pre>rings(true, enumFromTo(1,7))
Line 3,521:
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.
<
def permutations:
if length == 0 then []
Line 3,560:
;
tasks</
{{out}}
<pre>
Line 3,593:
[[0,1], [1,2,3], [3,4,5], [5,6]].
<
# Input: ignored.
# Output: a stream of solutions, i.e. a stream of arrays.
Line 3,647:
| solve($bx; .[-1]; add) ;
def count(s): reduce s as $x (null; .+1);</
'''The specific task'''
<
def boxes: [[0,1], [1,2,3], [3,4,5], [5,6]];
count(rings(boxes; 0; 9))</
{{out}}
<pre>
Line 3,660:
=={{header|Julia}}==
{{Trans|Python}}
<
using Combinatorics
Line 3,683:
foursquares(3, 9, true, true)
foursquares(0, 9, false, false)
</syntaxhighlight>
{{output}}
<pre>
Line 3,705:
=={{header|Kotlin}}==
{{trans|C}}
<
class FourSquares(
Line 3,777:
FourSquares(3, 9, true, true)
FourSquares(0, 9, false, false)
}</
{{out}}
Line 3,809:
=={{header|Lua}}==
{{trans|D}}
<
if unique then
for _,value in pairs(haystack) do
Line 3,865:
fourSquare(1,7,true,true)
fourSquare(3,9,true,true)
fourSquare(0,9,false,false)</
{{out}}
<pre>a b c d e f g
Line 3,886:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
SolveValues[{a + b == b + c + d == d + e + f == f + g, low <= a <= high,
low <= b <= high, low <= c <= high, low <= d <= high,
Line 3,902:
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</
{{out}}
<pre>{{3, 7, 2, 1, 5, 4, 6}, {4, 5, 3, 1, 6, 2, 7}, {4, 7, 1, 3, 2, 6,
Line 3,914:
=={{header|Modula-2}}==
<
FROM Conversions IMPORT IntToStr;
FROM Terminal IMPORT *;
Line 4,007:
four_square(0,9,FALSE,FALSE);
ReadChar; (* Wait so results can be viewed. *)
END FourSquare.</
=={{header|Nim}}==
Adapted from Rust version.
<
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
Line 4,048:
printFourSquares(1, 7)
printFourSquares(3, 9)
printFourSquares(0, 9, unique = false)</
{{out}}
<pre>[3, 7, 2, 1, 5, 4, 6]
Line 4,071:
{{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.
<
{$MODE DELPHI}
{$R+,O+}
Line 4,185:
writeln(' solution count for ',loDgt,' to ',HiDgt,' = ',cnt);
writeln('unique solution count for ',loDgt,' to ',HiDgt,' = ',uniqueCount);
end.</
{{Out}}
<pre>
Line 4,226:
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.
{{libheader|ntheory}}
<
use Set::CrossProduct;
Line 4,271:
display four_sq_permute( [3..9] );
display four_sq_permute( [8, 9, 11, 12, 17, 18, 20, 21] );
four_sq_cartesian( [0..9] );</
{{out}}
<pre>8 unique solutions found using: 1, 2, 3, 4, 5, 6, 7
Line 4,304:
2860 non-unique solutions found using: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9</pre>
===With Recursion===
<
use strict; # https://rosettacode.org/wiki/4-rings_or_4-squares_puzzle
Line 4,337:
elsif( @_ == 7 ) { $_[3] + $_[4] == $_[6] and $count++; return }
findcount( @_, $_ ) for 0 .. 9;
}</
{{out}}
<pre>
Line 4,362:
=={{header|Phix}}==
<!--<
<span style="color: #000080;font-style:italic;">-- demo/rosetta/4_rings_or_4_squares_puzzle.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 4,407:
<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>
<!--</
{{out}}
<pre>
Line 4,428:
=={{header|Picat}}==
<
main =>
Line 4,458:
% 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).</
{{out}}
Line 4,482:
{{Trans|ALGOL 68}}
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<
DECLARE FALSE LITERALLY '0';
Line 4,579:
CALL FOUR$RINGS( 3, 9, FALSE, TRUE );
CALL FOUR$RINGS( 0, 9, TRUE, FALSE );
EOF</
{{out}}
<pre>
Line 4,604:
=={{header|PL/SQL}}==
{{works with|Oracle}}
<
create table allints (v number);
create table results
Line 4,736:
end;
/
</syntaxhighlight>
Output
<pre>
Line 4,769:
=={{header|Prolog}}==
Works with SWI-Prolog 7.5.8
<
:- use_module(library(clpfd)).
Line 4,793:
my_sum(Min, Max, 1, LL),
length(LL, Len).
</syntaxhighlight>
Output
<pre>
Line 4,821:
===Procedural===
====Itertools====
<
def all_equal(a,b,c,d,e,f,g):
Line 4,843:
print str(solutions)+" "+uorn+" solutions in "+str(lo)+" to "+str(hi)
print</
Output
<pre>foursquares(1,7,True,True)
Line 4,870:
====Generators====
Faster solution without itertools
<
def foursquares(lo,hi,unique,show):
Line 4,935:
print str(solutions)+" "+uorn+" solutions in "+str(lo)+" to "+str(hi)
print</
Output<pre>
foursquares(1,7,True,True)
Line 4,964:
{{Trans|JavaScript}}
{{Works with|Python|3.7}}
<
from itertools import chain
Line 5,109:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Testing unique digits [1..7], [3..9] and unrestricted digits:
Line 5,138:
=={{header|R}}==
Function "perms" is a modified version of the "permutations" function from the "gtools" R package.
<
perms <- function (n, r, v = 1:n, repeats.allowed = FALSE) {
Line 5,198:
print_perms(10, 7, v = 0:9, repeats.allowed = TRUE, table.out = FALSE)
</syntaxhighlight>
{{Out}}
<pre>
Line 5,226:
Using a folder, so we can count as well as produce lists of results
<
(define solution? (match-lambda [(list a b c d e f g) (= (+ a b) (+ b c d) (+ d e f) (+ f g))]))
Line 5,239:
(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)</
{{out}}
Line 5,251:
{{works with|Rakudo|2016.12}}
<
my @solutions;
Line 5,278:
four-squares( [3..9] );
four-squares( [8, 9, 11, 12, 17, 18, 20, 21] );
four-squares( [0..9], :unique(0), :show(0) );</
{{out}}
Line 5,324:
This REXX version is faster than the more idiomatic version, but is longer (statement-wise) and
<br>a bit easier to read (visualize).
<
arg LO HI unique show . /*the ARG statement capitalizes args.*/
if LO=='' | LO=="," then LO=1 /*Not specified? Then use the default.*/
Line 5,393:
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</
{{out|output|text= when using the default inputs: <tt> 1 7 </tt>}}
<pre>
Line 5,430:
Note that the REXX language doesn't have short-circuits (when executing multiple clauses
in <big> '''if''' </big> (and other) statements.
<
arg LO HI unique show . /*the ARG statement capitalizes args.*/
if LO=='' | LO=="," then LO=1 /*Not specified? Then use the default.*/
Line 5,471:
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</
{{out|output|text= is identical to the faster REXX version.}} <br><br>
=={{header|Ruby}}==
<
f = -> (a,b,c,d,e,f,g) {[a+b, b+c+d, d+e+f, f+g].uniq.size == 1}
if unique
Line 5,495:
four_squares(low, high)
end
four_squares(0, 9, false)</
{{out}}
Line 5,521:
=={{header|Rust}}==
<
#![feature(inclusive_range_syntax)]
Line 5,596:
nonuniques(0, 9);
}
</syntaxhighlight>
{{Out}}
<pre>
Line 5,620:
=={{header|Scala}}==
{{trans|Java}}
<
def fourSquare(low: Int, high: Int, unique: Boolean, print: Boolean): Unit = {
Line 5,650:
fourSquare(0, 9, unique = false, print = false)
}
}</
{{out}}
<pre>a b c d e f g
Line 5,672:
=={{header|Scheme}}==
<
(import (scheme base)
(scheme write)
Line 5,720:
(display (count (lambda (combination) (apply solution? combination))
(combinations 7 (iota 10 0) #f))) (newline)
</syntaxhighlight>
{{out}}
Line 5,734:
=={{header|Sidef}}==
{{trans|Raku}}
<
var solutions = []
Line 5,776:
four_squares(@(3..9))
four_squares([8, 9, 11, 12, 17, 18, 20, 21])
four_squares(@(0..9), unique: false, show: false)</
{{out}}
<pre>
Line 5,815:
=={{header|Simula}}==
<
INTEGER PROCEDURE GETCOMBS(LOW, HIGH, UNIQUE, COMBS);
Line 5,929:
END.
</syntaxhighlight>
{{out}}
<pre>8 UNIQUE SOLUTIONS IN 1 TO 7
Line 5,951:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 6,067:
CALL FOUR_SQUARES(3, 9, 0, 0)@
CALL FOUR_SQUARES(0, 9, 1, 1)@
</syntaxhighlight>
Output:
<pre>
Line 6,116:
Use the program '''perm''' in the [[Permutations]] task for the first two questions, as it's fast enough. Use '''joinby''' for the third.
<
rename * (a b c d e f g)
list if a==c+d & b+c==e+f & d+e==g, noobs sep(50)
Line 6,167:
erase temp.dta
count
2,860</
=={{header|Tcl}}==
Line 6,176:
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.
<
set exprs {
{$a+$b}
Line 6,250:
solve_4rings $vars $exprs [range 3 9]
puts "# Number of solutions, free over 0..9:"
puts [solve_4rings_hard $vars $exprs [range 0 9]]</
{{out}}
Line 6,272:
=={{header|VBA}}==
{{trans|C}}
<
Dim e As Integer, f As Integer, g As Integer
Dim lo As Integer, hi As Integer, unique As Boolean, show As Boolean
Line 6,335:
Call foursquares(0, 9, False, False)
End Sub
</syntaxhighlight>
{{out}}
<pre>
Line 6,361:
=={{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.
<
Dim CA As Char() = "0123456789ABC".ToCharArray()
Line 6,406:
End Sub
End Module</
{{out}}
Added the zkl example for [5,12]<pre>a b c d e f g
Line 6,440:
=={{header|Vlang}}==
{{trans|Go}}
<
mut n, mut c := get_combs(1,7,true)
println("$n unique solutions in 1 to 7")
Line 6,493:
square4 := f + g
return square1 == square2 && square2 == square3 && square3 == square4
}</
{{out}}
Line 6,507:
{{trans|C}}
{{libheader|Wren-fmt}}
<
var a = 0
Line 6,585:
foursquares.call(1, 7, true, true)
foursquares.call(3, 9, true, true)
foursquares.call(0, 9, false, false)</
{{out}}
Line 6,618:
=={{header|XPL0}}==
<
proc Rings(Level);
int Level; \of recursion
Line 6,657:
IntOut(0, Count);
CrLf(0);
]</
{{out}}
Line 6,680:
=={{header|Yabasic}}==
{{trans|D}}
<
fourSquare(3,9,true,true)
fourSquare(0,9,false,false)
Line 6,743:
end if
return true
end sub</
{{out}}
<pre>a b c d e f g
Line 6,764:
=={{header|zkl}}==
<
fcn fourSquaresPuzzle(lo=1,hi=7,unique=True){ //-->list of solutions
_assert_(0<=lo and hi<36);
Line 6,783:
}
s
}</
<
if(not solutions){ println("No solutions for",msg); return(); }
Line 6,798:
fourSquaresPuzzle(5,12) : show(_," unique (5-12)"); println();
println(fourSquaresPuzzle(0,9,False).len(), // 10^7 possibilities
" non-unique (0-9) solutions found.");</
{{out}}
<pre>
|