Ejemplo n.º 1
0
        /// <summary>
        /// Computes each unique set of values of length SquareRoot(values.Length) that can be
        /// formed from the given values.
        /// </summary>
        /// <param name="values">
        /// All the possible values for the magic square. Each value must be unique.
        /// </param>
        public static HashSet <BitVector> ComputeSets(ReadOnlySpan <int> values)
        {
            int       boxSize           = Boxes.IntSquareRoot(values.Length);
            BitVector allPossibleValues = new BitVector();

            for (int i = 0; i < values.Length; ++i)
            {
                if (allPossibleValues.IsBitSet(values[i]))
                {
                    throw new ArgumentException("Values must be unique.");
                }
                allPossibleValues.SetBit(values[i]);
            }
            return(ComputeSets(values, boxSize, allPossibleValues));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Computes the "sum" that each row/column/diagonal must add up to.
        /// </summary>
        /// <param name="values">
        /// All the possible values for the magic square. Each value must be unique.
        /// </param>.
        /// <exception cref="ArgumentException">
        /// Thrown if no sum is possible.
        /// </exception>
        public static int ComputeSum(ReadOnlySpan <int> values)
        {
            int boxSize = Boxes.IntSquareRoot(values.Length);

            return(_ComputeSum(values, boxSize));
        }