Page 1 of 1
Sequence of all permutations of N objects
Posted: Mon Dec 15, 2025 12:49 am
by Lupp
(An alternative as of 2023-09-27. If you want to discuss the solution do as MrProgrammer already described.)
Code: Select all
Function sequenceOfPermutations(pN)
If pN=1 Then
Dim out1(1 To 1, 1 To 1) As Long
out1(1, 1) = 1
sequenceOfPermutations = out1
Else
pre = sequenceOfPermutations(pN - 1)
n = Ubound(pre, 1)
newN = n * pN
Dim out(1 To newn, 1 To pN) As Long
For j = 1 To n
For i = 1 To pN'pN To 1 Step -1
h = (j-1)*pN + i
For k = 1 To pN - i
out(h, k) = pre(j, k)
Next k
out(h, k) = pN
For k = k + 1 To pN
out(h, k) = pre(j, k - 1)
Next k
Next i
Next j
sequenceOfPermutations = out
EndIf
End Function
I didn't thoroughly compare this with the better formatted solution by MrProgrammer.
The code was written under the then current version of LibreOffice, and I tested it also with AOO 4.1.15.
An example:
Re: Sequence of all permutations of N objects
Posted: Mon Dec 15, 2025 4:42 am
by MrProgrammer
I tried
=SEQUENCEOFPERMUTATIONS(9) using OpenOffice 4.1.7. I received two
Overflow errors. The function did complete, but it only returned 40320 (8 factorial) rows and 8 columns. Perhaps all you need to do is declare some variables as
Long.

- Overflow.png (38.63 KiB) Viewed 13060 times

- Loop.gif (68.9 KiB) Viewed 13060 times
Re: Sequence of all permutations of N objects
Posted: Mon Dec 15, 2025 10:58 pm
by Lupp
@MrProgrammer
0. I can only test under Win 10.
1. I didn't successfully test with any AOO setting N=9 (except in a version using my BSM package).
2. I only have a PortableApps version of AOO (4.1.15).
3. When I now tried my version in that AOO with N=9, the first cycle (get needed range; about 60 s) seemed to work as expected, and the output range was selected. The second cycle failed with a "bad allocation" error.
4. After restarting/recovering I inserted your module into the document's Standard Basic library and used it in a newly inserted sheet.
5. I got the "bad allocation" now also with GENPERM(9). The needed time was > 2 min now. The error you got shown never occurred.
6. "bad allocation" should occur if the system gets too short in memory. I have always more than one application running, but with 8GiB the RAM should be sufficient (and actually is doing the same thing in LibO 25.8.2 as expected). There may be a bookkeeping bug in AOO.
7. In LibO my version worked as expected with n=9, and needed about 2 min for both cycles. Your GENPERM(9) needed a bit longer.
8. In LibO 25.8.2 I even tested my version in a sheet equipped with my BSM package. There I could use the SEQUENCEOFPERMUTATIONS(10). One of the BSM features is that you can create arrays by Calc formulas (using standard and/or UD functions) without the size limitations of a sheet. ROWS(), COLUMNS() and INDEX() can be applied to them using an assigned name even if ROWS > 2^20 ...
Of course the calculation needed a long time, but the resulting BSM variable had the expexted 3628800 rows (=FACT(10)), and gave access to any element. No error occurred in LibO V 25.8.2. The file to which the document was saved had only 35KiB because the BackStage variables aren't stored. (Disadvantage: Long recalc on opening.)
9. This isn't a recommendation to use BSM this way, but it may induce interest. There are more realistic use cases with less recalculation. (My AOO 4.1.15 can use BSM, but didn't open and recalculate this file correctly. It got stuck with CPU load 0%.)
Re: Sequence of all permutations of N objects
Posted: Sat Dec 27, 2025 6:50 pm
by cwolan
I tried
=GENPERM(9) and
=SEQUENCEOFPERMUTATIONS(9) on Windows 7 (8 GB RAM) and Windows 11 (16 GB) with:
- OpenOffice (4.1.7, 4.1.15, 4.1.16) — both failed (mostly "bad allocation" error)
- LibreOffice 25.8.4.2 — both work as expected.