# Maths Problem

Please understand am not sure if this is the place for my question if not then please remove.

I need to solve all possible combinations of the solution to n!

So if 5! then there are 120 possible combinations so how do I work out what they are…?

I did solve this once many years ago but my brain seems to have seized up with regard to this as have been working on it for a couple of weeks but no luck.

Thanks for any help in advance.

Hi Ian,

Could you explain a bit more what are trying to do and what you mean by 5!?

If you mean 5 factorial it is equal to 12345 (so 120) but there isn’t any “combination”, it’s a single value.

If that’s what you want (untested code):

[code]Sub Factorial(Numb as Integer) as Int64

Dim i as integer
Dim Result as Int64
Result=1

For i=1 to Numb
Result=Result*i
Next

Return Result

End Sub[/code]

Julen

well
5,4,3,2,1
5,4,3,1,2
5,4,1,2,3
5,1,2,3,4
and so on

there’s 120 different unique orderings of those 5 digits

is that what you mean ?

Yes, I don’t seem to have explained myself very well.

I want to be able to generate the various ordering of the values no matter if numbers or letters.

So as you say 1.2.3.4.5 or a.b.c.d.e in its many permutations.

If 5! = !20 permutation then I wish to generate them as an array.

If 6! = 720 Permutations and so on…

Just wish I could find the Program I wrote years ago…

Thanks for any help.

VB6 is easy to convert to Xojo.

Julen

[quote=101321:@Ian Cartwright]Please understand am not sure if this is the place for my question if not then please remove.

I need to solve all possible combinations of the solution to n!

So if 5! then there are 120 possible combinations so how do I work out what they are…?

I did solve this once many years ago but my brain seems to have seized up with regard to this as have been working on it for a couple of weeks but no luck.

Thanks for any help in advance.[/quote]

Here is a method that returns all the permutations for a delimited character string as a string array…
(E.g. “1.2.3” will give you [“1.2.3”, “2.1.3”, “2.3.1”, “1.3.2”, “3.1.2”, “3.2.1”])

It also has an optional delimiter parameter so that you can change the delimiter if you need to.

``````Function Permutations(s As String, delimiter As String = ".") As String()
Dim tmpArr() As String
Dim perms() As String
Dim result() As String
Dim firstStr As String
Dim i As Integer
Dim j As Integer

tmpArr = Split(s, delimiter)

if tmpArr.Ubound <= 0 then
result.Append tmpArr(0)
else

firstStr = tmpArr(0)

tmpArr.Remove(0)
perms = Permutations(Join(tmpArr, delimiter))

for i = 0 to perms.Ubound
tmpArr = Split(perms(i), delimiter)
for j = 0 to tmpArr.Ubound + 1
tmpArr.Insert(j, firstStr)
result.Append(Join(tmpArr, delimiter))
tmpArr.Remove(j)
next j
next i

end if

return result

End Function``````

Well all I can say is thank you all for your replies and using Alwyn Besters solution I am now well on the way to getting my program finished.

Thank You.

Ian.

You’re welcome Ian… glad I could help.