A challenge with simple rules but non-trivial algorithms. :-)
Task
Take input in the form of space-separated integers:
N A B S
Where N is the side length of a 2D square matrix filled with unique numbers (integers) between A and B inclusive. For each row and column in this matrix, the sum is always the same: S. (In other words, the matrix is a semi-magic square).
Note:
All numbers are positive. Exception is A, which can be 0.
Examples
For
3 1 10000 2015
a valid solution would be
For
8 1 300 500
a valid solution would be
Output
You output should be an ASCII table. Example for the first example above:
384 159 1472
1174 499 342
457 1357 201
Right-aligned integers padded by spaces. The width of each column is the width of the largest integer in that column.
Scoring
This is code-golf, so shortest code in bytes wins. Standard loopholes apply (especially about built-ins to solve this problem). You don't have to care about wrong or otherwise impossible inputs (including negative numbers). Please provide a sample output in your answer (mandatory) for the second example above.
A
,B
, andN
can be negative? \$\endgroup\$