N-queens minimum and knights and bishops: Difference between revisions

m
syntax highlighting fixup automation
(Added Perl)
m (syntax highlighting fixup automation)
Line 11:
<br><br>
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Minimum knights to attack all squares not occupied on an NxN chess board. Nigel Galloway: May 12th., 2022
type att={n:uint64; g:uint64}
Line 38:
printfn "Solution found using %d knights in %A:" rn (System.DateTime.UtcNow-t); tc rb.att
for n in 1..10 do solveK n
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 131:
 
Timing is for an Intel Core i7-8565U machine running Ubuntu 20.04.
<langsyntaxhighlight lang="go">package main
 
import (
Line 336:
elapsed := time.Now().Sub(start)
fmt.Printf("Took %s\n", elapsed)
}</langsyntaxhighlight>
 
{{out}}
Line 428:
This is a crude attempt -- brute force search with some minor state space pruning. I am not patient enough to run this for boards larger than 7x7 for knights:
 
<syntaxhighlight lang="j">
<lang J>
genboard=: {{
safelen=:2*len=: {.y
Line 528:
end.
end.
}}</langsyntaxhighlight>
 
Task output:
 
<langsyntaxhighlight Jlang="j"> task 10
+--+--+--+ B: 1
|B |Q |K | Q: 1
Line 608:
|. . . . . . . . . . |. . . . . . . . . . |
+--------------------+--------------------+
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
Uses the Cbc optimizer (github.com/coin-or/Cbc) for its complementarity support.
<langsyntaxhighlight lang="ruby">""" Rosetta code task N-queens_minimum_and_knights_and_bishops """
 
import Cbc
Line 725:
"\nKnights:\n", examples[3])
 
</langsyntaxhighlight> {{out}}
<pre>
Squares Queens Bishops Knights
Line 784:
The first Q,B in the first row is only placed lmt..mid because of symmetry reasons.<br>
14 Queens takes 2 min @home ~2.5x faster than TIO.RUN
<langsyntaxhighlight lang="pascal">program TestMinimalQueen;
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}
 
Line 1,050:
pG_Out(@Bsol,'_.B',max-1);
END.
</syntaxhighlight>
</lang>
{{out|@TIO.RUN}}
<pre>
Line 1,085:
Shows how the latest release of Perl can now use booleans.
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use v5.36;
use builtin 'true', 'false';
no warnings 'experimental::for_list', 'experimental::builtin';
Line 1,194:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Queens
Line 1,279:
You can run this online [http://phix.x10.mx/p2js/minqbn.htm here], with cheat mode on so it should all be done in 22s (8.3 on the desktop).
Cheat mode drops the final tricky 10N search from 8,163,658 positions to just 183,937. Without cheat mode it takes 1 min 20s to completely finish on the desktop (would be ~7mins in a browser), but it always remains fully interactive throughout.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\minQBN.exw
Line 1,672:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
{{trans|Julia}}
<langsyntaxhighlight lang="python">""" For Rosetta Code task N-queens_minimum_and_knights_and_bishops """
 
from mip import Model, BINARY, xsum, minimize
Line 1,777:
print()
print()
</langsyntaxhighlight>{{out}}
<pre>
Squares Queens Bishops Knights
Line 1,835:
Due to the time it's taking only a subset of the task are attempted.
{{trans|Go}}
<syntaxhighlight lang="raku" perl6line># 20220705 Raku programming solution
 
my (@board, @diag1, @diag2, @diag1Lookup, @diag2Lookup, $n, $minCount, $layout);
Line 1,945:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,013:
 
Although far quicker than it was originally (it now gets to 7 knights in less than a minute), it struggles after that and needs north of 21 minutes to get to 10.
<langsyntaxhighlight lang="ecmascript">import "./fmt" for Fmt
 
var board
Line 2,156:
}
}
System.print("Took %(System.clock - start) seconds.")</langsyntaxhighlight>
 
{{out}}
Line 2,253:
 
I have borrowed one or two tricks from the Julia/Python versions in formulating the constraints.
<langsyntaxhighlight lang="ecmascript">import "./linear" for Prob, Glp, Tran, File
import "./fmt" for Fmt
 
Line 2,362:
}
File.remove(fname)
System.print("Took %(System.clock - start) seconds.")</langsyntaxhighlight>
 
{{out}}
10,327

edits