7

I have 5 numbers 1, 2, 3, 4, and 5, and I would like to get all of the possible combinations of those numbers to arrive at a given total of 10.

Example:

1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + = 10
1 + 2 + 2 + 3 + 2 = 10
7 + 3 = 10
4 + 5 + 1 = 10
2 + 2 + 2 + 1 + 3 = 10
and so on...

I will appreciate it if anyone here could give a good solution on how to solve this problem?

RickyBelmont
  • 619
  • 4
  • 11
  • 9
    This is easy. You merely need to write an *algorithm*. – Andreas Rejbrand Mar 12 '21 at 14:37
  • Yes, you are right! It should be an algorithm but I've been breaking my head for hours already on how to do this in Delphi. – RickyBelmont Mar 12 '21 at 14:39
  • 1
    Don't want to sound rude, but do you know how to do it *any* programming language? If not, this is actually a question about pure mathematics, and so off-topic at Stack Overflow. – Andreas Rejbrand Mar 12 '21 at 14:47
  • 1
    @AndreasRejbrand It's not off-topic, there are plenty of similar questions under the algorithm tag (for example [this one](https://stackoverflow.com/questions/2070359/finding-three-elements-in-an-array-whose-sum-is-closest-to-a-given-number)). – Olivier Mar 12 '21 at 14:55
  • I know. I am just trying my luck here if someone has the goods and willing to share. I think I am tired already, I will continue the algorithm tomorrow. – RickyBelmont Mar 12 '21 at 14:56
  • 1
    @Olivier: Well, that Q doesn't include a language tag and was written quite some time ago, when SO was a bit more relaxed. But personally I don't mind questions like this. – Andreas Rejbrand Mar 12 '21 at 14:57
  • @AndreasRejbrand [this one](https://stackoverflow.com/questions/58042000/subarray-sum-solution) is more recent and has no language tag either. Why do you think a language tag is required? – Olivier Mar 12 '21 at 15:01
  • 3
    @Olivier: Actually, it is the opposite: A *lack* of language tag IMHO makes the Q a better fit at SO. – Andreas Rejbrand Mar 12 '21 at 15:02
  • 2
    You can check here https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum – Bosshoss Mar 12 '21 at 15:37
  • 2
    Is summand order important? – MBo Mar 12 '21 at 15:45
  • @MBo summand order not really important. – RickyBelmont Mar 13 '21 at 00:55
  • @MartynA Thanks. I am amazed by the answers given here. I will try all the answers and see which one fits my requirements. Actually, from this algorithm, I will be incorporating this with my clientdatssets variable data. So any of the answers should fit-in. Let's see. – RickyBelmont Mar 13 '21 at 01:00
  • @LURD Agree and edited my question. Thanks. – RickyBelmont Mar 13 '21 at 01:03
  • 2
    May I ask why you need this, by the way? – Olivier Mar 13 '21 at 08:58
  • 1
    @Olivier I am working on a manufacturing app and one of the requirements is to find a combination of products (different sizes) to maximize the use and minimize waste of raw materials. Scenario: I have variable sizes of products planned to produc by the user and the app should be able to identify the appropriate raw material to be used given one requirement `minimize the waste`. So, I have to find the best combination of the products of different sizes to fit with a fixed size of raw material. – RickyBelmont Mar 14 '21 at 00:01

5 Answers5

8

Although this is arguably not a Delphi question but a question about pure mathematics, I can give you a few hints.

First, notice that you clearly cannot have more than 10 terms in the sum, because if you have more than ten terms, then you have at least eleven terms and so the sum becomes at least

11 × Lowest allowed summand = 11 × 1 = 11

which is already greater than 10.

Therefore, a single solution to this problem can naturally be represented as an array of exactly 10 integers from 0 to 5.

type
  TTerm = 0..5;
  TCandidate = array[0..9] of TTerm;

Please note, however, that two distinct TCandidate values might represent the same solution:

5, 3, 2, 0, 0, 0, 0, 0, 0, 0
3, 2, 5, 0, 0, 0, 0, 0, 0, 0
5, 3, 0, 0, 0, 0, 0, 0, 2, 0

Since each summand is chosen from a set of cardinality 6, there are 610 = 60466176 possible TCandidate values. For a modern computer, this is a "small" number, so even a very naïve algorithm which tries every such candidate (by computing its sum!) will give you the answer almost immediately.

Furthermore, since 10 is not a huge number, you could use ten nestled for loops and that approach is almost trivial (right?). However, that approach is so ugly that I refuse to use it. Instead, I'll use a more elegant approach which would also work for other values than fixed small ones like 10.

const
  FirstCandidate: TCandidate = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

function GetNextCandidate(var ANext: TCandidate): Boolean;
begin
  for var p := High(ANext) downto Low(ANext) do
    if ANext[p] < High(TTerm) then
    begin
      Inc(ANext[p]);
      for var p2 := Succ(p) to High(ANext) do
        ANext[p2] := 0;
      Exit(True);
    end;
  Result := False;
end;

The GetNextCandidate function is used to enumerate the candidates in the order you get if you consider them to be base-6 numbers. It accepts a candidate, like (2, 1, 3, 0, 5, 2, 1, 3, 2, 0) and replaces it with the next one, like (2, 1, 3, 0, 5, 2, 1, 3, 2, 1), unless you are at the last one: (5, 5, 5, 5, 5, 5, 5, 5, 5, 5).

Let's try this enumeration:

var CurrentCandidate := FirstCandidate;
while GetNextCandidate(CurrentCandidate) do
  OutputCandidateVector(CurrentCandidate);

(implementing OutputCandidateVector is left as an exercise) produces

0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 2
0, 0, 0, 0, 0, 0, 0, 0, 0, 3
0, 0, 0, 0, 0, 0, 0, 0, 0, 4
0, 0, 0, 0, 0, 0, 0, 0, 0, 5
0, 0, 0, 0, 0, 0, 0, 0, 1, 0
0, 0, 0, 0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 1, 2
0, 0, 0, 0, 0, 0, 0, 0, 1, 3
0, 0, 0, 0, 0, 0, 0, 0, 1, 4
0, 0, 0, 0, 0, 0, 0, 0, 1, 5
0, 0, 0, 0, 0, 0, 0, 0, 2, 0
0, 0, 0, 0, 0, 0, 0, 0, 2, 1
0, 0, 0, 0, 0, 0, 0, 0, 2, 2
0, 0, 0, 0, 0, 0, 0, 0, 2, 3
0, 0, 0, 0, 0, 0, 0, 0, 2, 4
0, 0, 0, 0, 0, 0, 0, 0, 2, 5
0, 0, 0, 0, 0, 0, 0, 0, 3, 0
0, 0, 0, 0, 0, 0, 0, 0, 3, 1
0, 0, 0, 0, 0, 0, 0, 0, 3, 2
0, 0, 0, 0, 0, 0, 0, 0, 3, 3
0, 0, 0, 0, 0, 0, 0, 0, 3, 4
0, 0, 0, 0, 0, 0, 0, 0, 3, 5
...

Now we are "done":

var CurrentCandidate := FirstCandidate;
while GetNextCandidate(CurrentCandidate) do
  if Sum(CurrentCandidate) = 10 then
    Display(CurrentCandidate);

using two more trivial helper routines.

Output:

...
0+3+3+0+2+0+0+1+0+1
0+3+3+0+2+0+0+1+1+0
0+3+3+0+2+0+0+2+0+0
0+3+3+0+2+0+1+0+0+1
0+3+3+0+2+0+1+0+1+0
0+3+3+0+2+0+1+1+0+0
0+3+3+0+2+0+2+0+0+0
0+3+3+0+2+1+0+0+0+1
0+3+3+0+2+1+0+0+1+0
0+3+3+0+2+1+0+1+0+0
0+3+3+0+2+1+1+0+0+0
0+3+3+0+2+2+0+0+0+0
0+3+3+0+3+0+0+0+0+1
0+3+3+0+3+0+0+0+1+0
0+3+3+0+3+0+0+1+0+0
0+3+3+0+3+0+1+0+0+0
0+3+3+0+3+1+0+0+0+0
0+3+3+0+4+0+0+0+0+0
0+3+3+1+0+0+0+0+0+3
0+3+3+1+0+0+0+0+1+2
0+3+3+1+0+0+0+0+2+1
0+3+3+1+0+0+0+0+3+0
0+3+3+1+0+0+0+1+0+2
0+3+3+1+0+0+0+1+1+1
0+3+3+1+0+0+0+1+2+0
...

But how do we get rid of duplicates? Notice that there are two sources of duplicates:

  • First, we have the positions of the zeros. 0+3+3+1+0+0+0+1+1+1 and 0+3+3+1+0+0+1+0+1+1 are both more naturally written 3+3+1+1+1+1.

  • Second, we have ordering: 3+3+1+1+1+1 versus 3+1+3+1+1+1.

It's not clear from your question if you consider order important, but I'll assume you don't, so that 3+3+1+1+1+1 versus 3+1+3+1+1+1 represent the same solution.

How, then, to get rid of duplicates? One solution is to sort each candidate vector and then remove strict duplicates. Now I am really lazy and use a string dictionary:

begin
  var SolutionStringsDict := TDictionary<string, Pointer>.Create;
  var SolutionStringsList := TList<string>.Create;
  try

    var CurrentCandidate := FirstCandidate;
    while GetNextCandidate(CurrentCandidate) do
      if Sum(CurrentCandidate) = 10 then
      begin
        var CandidateSorted := SortCandidateVector(CurrentCandidate);
        var CandidateString := PrettySumString(CandidateSorted);
        if not SolutionStringsDict.ContainsKey(CandidateString) then
        begin
          SolutionStringsDict.Add(CandidateString, nil);
          SolutionStringsList.Add(CandidateString);
        end;
      end;

    for var SolutionString in SolutionStringsList do
      Writeln(SolutionString);

  finally
    SolutionStringsList.Free;
    SolutionStringsDict.Free;
  end;
end.

This yields

5+5
5+4+1
5+3+2
4+4+2
4+3+3
5+3+1+1
4+4+1+1
5+2+2+1
4+3+2+1
3+3+3+1
4+2+2+2
3+3+2+2
5+2+1+1+1
4+3+1+1+1
4+2+2+1+1
3+3+2+1+1
3+2+2+2+1
2+2+2+2+2
5+1+1+1+1+1
4+2+1+1+1+1
3+3+1+1+1+1
3+2+2+1+1+1
2+2+2+2+1+1
4+1+1+1+1+1+1
3+2+1+1+1+1+1
2+2+2+1+1+1+1
3+1+1+1+1+1+1+1
2+2+1+1+1+1+1+1
2+1+1+1+1+1+1+1+1
1+1+1+1+1+1+1+1+1+1

after two or three seconds, even though this approach is very inefficient!

This highlights two general rules:

  • Given a well-specified problem, it is often easy to create a correct algorithm that solves it. However, creating an efficient algorithm requires more work.

  • Computers are really fast these days.

Appendix A: Full source code

program EnumSums;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils,
  Math,
  Generics.Defaults,
  Generics.Collections;

type
  TTerm = 0..5;
  TCandidate = array[0..9] of TTerm;

const
  FirstCandidate: TCandidate = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

function GetNextCandidate(var ANext: TCandidate): Boolean;
begin
  for var p := High(ANext) downto Low(ANext) do
    if ANext[p] < High(TTerm) then
    begin
      Inc(ANext[p]);
      for var p2 := Succ(p) to High(ANext) do
        ANext[p2] := 0;
      Exit(True);
    end;
  Result := False;
end;

function Sum(const ACandidate: TCandidate): Integer;
begin
  Result := 0;
  for var Term in ACandidate do
    Inc(Result, Term);
end;

procedure Display(const ACandidate: TCandidate);
begin
  var S := '';
  for var i := Low(ACandidate) to High(ACandidate) do
    if S.IsEmpty then
      S := IntToStr(ACandidate[i])
    else
      S := S + '+' + IntToStr(ACandidate[i]);
  Writeln(S);
end;

function SortCandidateVector(const ACandidate: TCandidate): TCandidate;
begin
  var L: TArray<Integer>;
  SetLength(L, Length(ACandidate));
  for var i := 0 to High(L) do
    L[i] := ACandidate[i];
  TArray.Sort<Integer>(L);
  for var i := 0 to High(L) do
    Result[i] := L[High(L) - i];
end;

function PrettySumString(const ACandidate: TCandidate): string;
begin
  Result := '';
  for var i := Low(ACandidate) to High(ACandidate) do
    if ACandidate[i] = 0 then
      Exit
    else if Result.IsEmpty then
      Result := IntToStr(ACandidate[i])
    else
      Result := Result + '+' + IntToStr(ACandidate[i]);
end;


begin

  var SolutionStringsDict := TDictionary<string, Pointer>.Create;
  var SolutionStringsList := TList<string>.Create;
  try

    var CurrentCandidate := FirstCandidate;
    while GetNextCandidate(CurrentCandidate) do
      if Sum(CurrentCandidate) = 10 then
      begin
        var CandidateSorted := SortCandidateVector(CurrentCandidate);
        var CandidateString := PrettySumString(CandidateSorted);
        if not SolutionStringsDict.ContainsKey(CandidateString) then
        begin
          SolutionStringsDict.Add(CandidateString, nil);
          SolutionStringsList.Add(CandidateString);
        end;
      end;

    for var SolutionString in SolutionStringsList do
      Writeln(SolutionString);

  finally
    SolutionStringsList.Free;
    SolutionStringsDict.Free;
  end;

  Readln;

end.
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • Thanks, Andreas for some time to craft this solution. It is well explained and I like the way you did it. Somehow, it did not cover all the possible combinations like `5` + `5` = `10`. I am not sure if my question is not so clear. However, I can already work with this to cover all combinations. However, if you can further make some tweaks, please do, so I can mark it as an answer so anyone here in the future can make use of this. – RickyBelmont Mar 13 '21 at 03:44
  • @RickyBelmont: Isn't `5+5` [the very first item](https://privat.rejbrand.se/EnumSums.png) in the output above? – Andreas Rejbrand Mar 13 '21 at 10:18
  • 2
    Also, let my reiterate the fact that this answer is mostly educational, displaying the very simplest way to find the result using brute-force. It's very slow, but requires almost no pre-implementation analysis; you barely need to think at all. If you compare this with some of the other answers, you'll see that they require more initial analysis (like determining individual bounds 10, 5, 3, 2, 2) and hard-coding these and offer a bit more room for source-code typos (which can be very hard to find!), but in return are *much* faster. So my main point is "Given a well-specified problem ..." above. – Andreas Rejbrand Mar 13 '21 at 10:23
  • 1
    Your Q don't say exactly what you need the solution for (hence inviting my more "educational" discussion), but if some real-world app needs this particular list of solutions, the best way is IMHO to hardcode this list in a constant source-code array! And then it might be good to use a naïve method like mine to compute this list, so you know there is absolutely no room for errors. – Andreas Rejbrand Mar 13 '21 at 10:29
  • I am very very sorry. Yes there is `5 + 5`. It was actually hidden at the very top when I ran it. And I can confirm it works! Exactly, what I need. – RickyBelmont Mar 14 '21 at 00:33
4

Another approach would be to convert to a linear equation where A,B,C,D and E are the number of 1,2,3,4 or 5's.

A + B*2 + C*3 + D*4 + E*5 = 10

Determine the range of each variable.

A = (0..10)   // can be 0 to 10 1's
B = (0..5)    // can be 0 to 5 2's
C = (0..3)    // etc
D = (0..2)
E = (0..2)

Try all combinations. Total combinations to check: 11 * 6 * 4 * 3 * 3 = 2,376.

  for var A : integer := 0 to 10 do
    for var B : integer := 0 to 5 do
      for var C : integer := 0 to 3 do
        for var D : integer := 0 to 2 do
          for var E : integer := 0 to 2 do
            if A * 1 + B * 2 + C * 3 + D * 4 + E * 5 = 10 then
            begin
              // output a solution
            end;

Full Source Solution

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.StrUtils;

begin
  for var A : integer := 0 to 10 do
    for var B : integer := 0 to 5 do
      for var C : integer := 0 to 3 do
        for var D : integer := 0 to 2 do
          for var E : integer := 0 to 2 do
            if A * 1 + B * 2 + C * 3 + D * 4 + E * 5 = 10 then
            begin
              Var AResult : string := '';
              for Var I :integer := 1 to E do AResult := AResult + ' + 5';
              for Var I :integer := 1 to D do AResult := AResult + ' + 4';
              for Var I :integer := 1 to C do AResult := AResult + ' + 3';
              for Var I :integer := 1 to B do AResult := AResult + ' + 2';
              for Var I :integer := 1 to A do AResult := AResult + ' + 1';
              writeln(RightStr( AResult,length(AResult) -3) + ' = 10');
            end;
  readln;
end.
Brian
  • 6,717
  • 2
  • 23
  • 31
  • Thanks for taking some time to draft this answer but it appears to me when I tried it does not exhaust all the combinations. Like for example `5` + `5` = `10` is not generated. I got only 29 combinations. – RickyBelmont Mar 13 '21 at 02:56
  • You have actually the same result as Andreas above. But yours is shorter. I am trying to modify something here so it could cover the remaining combinations. However, if you can do so more changes to fit my requirements, please do so and I will mark it as an answer so anyone in the future could use your answer. Many thanks. – RickyBelmont Mar 13 '21 at 03:48
  • @RickyBelmont, this solution gives 30 combinations, including 5 + 5. – LU RD Mar 13 '21 at 07:15
  • I missed sorry. Something wrong with my cmd window. It was hidden at the very top. I confirmed to it is exactly what I need. It works! – RickyBelmont Mar 14 '21 at 00:34
3

Build a rooted tree where the paths from the root are the elements that sum to 10.

Say each node stores its value and the sum from the root to it (with the root having both zeroed).

def update(node):
    max_child = min(5, 10 - node.sum_from_root, node.value)
    for i in range(1, max_child):
        child = node.new(i, sum_from_root + i)
        node.add_child(child)
        update(child) if child.sum_from_root < 10

E.g.,

root has children (value, sum_from_root): (1,1), (2,2), (3,3), (4,4), (5,5)

root-(4,4) has children (1,5), (2,6), (3,7), (4,8)

root-(4,4)-(3,7) has children(1,8), (2,9), (3,10)

root-(4,4)-(3,7)-(2,9) has children(1,10)

...

whereas root-(4,4)-(4,8) has children (1,9), (2,10)

This is linear in the output (the number of paths).

I insist on children being <= parents (other than for the root) to avoid permutations of the same answer. If you want permutations then remove this restriction.

Dave
  • 7,460
  • 3
  • 26
  • 39
  • Your approach is totally different. I'd like to go with it but for now, I am not really familiar yet with nodes. I will just try other solutions first and if I need to figure out yours, I will. Thanks by the way. – RickyBelmont Mar 13 '21 at 03:52
  • 1
    @RickyBelmont I don't know Delphi; it may have another name there. Use whatever data structure you'd normally use for trees, (and graphs, and linked lists). I'm just describing a way of making a tree where the paths from the root to the leaves describe the paths you want. – Dave Mar 13 '21 at 05:00
  • 1
    @RickyBelmont It's not actually necessary to build the tree. See my answer. – Olivier Mar 13 '21 at 10:41
2

Is 9ms fast enough? In spite of using an interpretive language (Perl)? (I don't know Delphi.) There is very little wasted effort in this algorithm. No dups; the algorithm prevents them.

use strict;
for my $a (1..5) {
for my $b ($a..5) {
if ($a + $b == 10) { print "$a + $b\n"; next }
for my $c ($b..10-$b) {
if ($a + $b + $c == 10) { print "$a + $b + $c\n"; next }
for my $d ($c..10-$c) {
if ($a + $b + $c + $d == 10) { print "$a + $b + $c + $d\n"; next }
for my $e ($d..10-$d) {
if ($a + $b + $c + $d + $e == 10) { print "$a + $b + $c + $e + $e\n"; next }
for my $f ($e..10-$e) {
if ($a + $b + $c + $d + $e + $f == 10) { print "$a + $b + $c + $d + $e + $f\n"; next }
for my $g ($f..10-$f) {
if ($a + $b + $c + $d + $e + $f + $g == 10) { print "$a + $b + $c + $d + $e + $f + $g\n"; next }
for my $h ($g..10-$g) {
if ($a + $b + $c + $d + $e + $f + $g + $h == 10) { print "$a + $b + $c + $d + $e + $f + $g + $h\n"; next }
for my $i ($h..10-$f) {
if ($a + $b + $c + $d + $e + $f + $g + $h + $i == 10) { print "$a + $b + $c + $d + $e + $f + $g + $h + $i\n"; next }
for my $j ($i..10-$g) {
if ($a + $b + $c + $d + $e + $f + $g + $h + $i + $j == 10) { print "$a + $b + $c + $d + $e + $f + $g + $h + $i + $j\n"; next }
}}}}}}}}}}

Output:

1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2
1 + 1 + 1 + 1 + 1 + 1 + 1 + 3
1 + 1 + 1 + 1 + 1 + 1 + 2 + 2
1 + 1 + 1 + 1 + 1 + 1 + 4
1 + 1 + 1 + 1 + 1 + 2 + 3
1 + 1 + 1 + 1 + 1 + 5
1 + 1 + 1 + 1 + 2 + 2 + 2
1 + 1 + 1 + 1 + 2 + 4
1 + 1 + 1 + 1 + 3 + 3
1 + 1 + 1 + 6 + 6
1 + 1 + 1 + 2 + 2 + 3
1 + 1 + 1 + 5 + 5
1 + 1 + 1 + 4 + 4
1 + 1 + 1 + 7
1 + 1 + 2 + 2 + 2 + 2
1 + 1 + 2 + 4 + 4
1 + 1 + 2 + 3 + 3
1 + 1 + 2 + 6
1 + 1 + 3 + 5
1 + 1 + 4 + 4
1 + 1 + 8
1 + 2 + 2 + 3 + 3
1 + 2 + 2 + 5
1 + 2 + 3 + 4
1 + 2 + 7
1 + 3 + 3 + 3
1 + 3 + 6
1 + 4 + 5
2 + 2 + 2 + 2 + 2
2 + 2 + 2 + 4
2 + 2 + 3 + 3
2 + 2 + 6
2 + 3 + 5
2 + 4 + 4
3 + 3 + 4
5 + 5

(37 lines)

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • 1
    Only numbers up to 5 are allowed. – Olivier Mar 13 '21 at 10:42
  • 1
    @Olivier - OK, keep the first 5 "for" loops; lose the rest. Simpler. Faster. – Rick James Mar 13 '21 at 15:07
  • Thanks for this answer but all the rest of the answers here gives 30 lines result. Anyway, I am looking at Delphi but I appreciate the effort though. I am sure someone will be looking for Perl language in the future—they could use this. – RickyBelmont Mar 14 '21 at 10:09
1

Here is a recursive solution inspired by Dave's answer. It doesn't build a tree though:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils, Math;

type
  TSolution = array[1..10] of integer;

procedure PrintSolution(var Solution:TSolution; Size:integer);
var
  s: string;
  i: integer;
begin
  s := '';
  for i:=1 to Size do
    s := s + IntToStr(Solution[i]) + ' ';
  Writeln(s);
end;

procedure Search(var Solution:TSolution; Size, Sum, Target:integer);
var
  i, j, k, Sum2:integer;
begin
  if Size = 0 then
     j := 1
  else
    j := Solution[Size];
  k := Min(Target - Sum, 5);
  Inc(Size);
  for i:=j to k do
  begin
    Solution[Size] := i;
    Sum2 := Sum + i;
    if Sum2<Target then
      Search(Solution, Size, Sum2, Target)
    else
      PrintSolution(Solution, Size);
  end;
end;

var
  Solution:TSolution;
begin
  Search(Solution, 0, 0, 10);
  Readln;
end.

Output:

1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 3
1 1 1 1 1 1 2 2
1 1 1 1 1 1 4
1 1 1 1 1 2 3
1 1 1 1 1 5
1 1 1 1 2 2 2
1 1 1 1 2 4
1 1 1 1 3 3
1 1 1 2 2 3
1 1 1 2 5
1 1 1 3 4
1 1 2 2 2 2
1 1 2 2 4
1 1 2 3 3
1 1 3 5
1 1 4 4
1 2 2 2 3
1 2 2 5
1 2 3 4
1 3 3 3
1 4 5
2 2 2 2 2
2 2 2 4
2 2 3 3
2 3 5
2 4 4
3 3 4
5 5
Olivier
  • 13,283
  • 1
  • 8
  • 24
  • Just a quick one. I am getting an error at the last line `Readln();` it says `[dcc32 Error] Project1.dpr(50): E2029 'END' expected but ')' found` – RickyBelmont Mar 14 '21 at 00:44