On Sunday, 24 July 2022 at 08:16:41 UTC, Test123 wrote:
On Saturday, 23 July 2022 at 23:14:03 UTC, Salih Dincer wrote:
On Saturday, 23 July 2022 at 22:50:55 UTC, Salih Dincer wrote:
[...]

I guess my code didn't list all possibilities either. Moreover, the m and n parameters will be swapped:

[...]

**PS.** * marked from version(c) in the above list is from version(b).

SDB@79

Can you share your code ?


The restuls is position unrelated.

Everyone has it, the classic permutation codes.There should even be something in the standard library?

I didn't share because I didn't know what you looking for. But you know this classic recurisive function:

```d

void permut(T)(T[] x, ref int count, int start = 0) {
  auto len = cast(int)x.length;
  if(start != len)
  {
    for(int i = start; i < len; i++)
    {
      x[start].swap(x[i]);
      x.permut(count, start + 1);
      x[start].swap(x[i]);
    }
  } else if(len < 9) x.writeln(": ", ++count);
  else ++count;   // ^---a lot of output
}

void swap(T)(ref T x, ref T y) {
  auto t = y;
       y = x;
       x = t;
}

T factorial(T)(in T n) pure nothrow @nogc
in(n < 21, "The number is a very large for ulongMax") {
  return n > 1 ? n * factorial(n - 1) : 1;
} unittest { assert (factorial!int(12) == 479001600); }


import std.range, std.stdio;

void main() {
  int totalCount, n = 3;

  iota(1, n + 1).array.permut(totalCount);
  totalCount.writeln(" permutation found...");

  assert(totalCount == n.factorial);
}
```

**PS.** I added extra factorial `assert()`...

SDB@79


Reply via email to