Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
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.
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 13059 times
Loop.gif (68.9 KiB) Viewed 13059 times
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.8, iMac Intel. The locale for any menus or Calc formulas in my posts is English (USA).
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%.)
On Windows 10: LibreOffice 25.8.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München