Using integer constraints, we obtain a short, versatile and efficient Prolog solution of this combinatorial task.
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.