0

I'd like to calculate how many different variations of a certain amount of numbers are possible. The number of elements is variable.

Example: I have 5 elements and each element can vary between 0 and 8. Only the first element is a bit more defined and can only vary between 1 and 8. So far I'd say I have 8*9^4 possibilities. But I have some more conditions. As soon as one of the elements gets zero the next elements should be automatically zero as well.

E.G:

6 5 4 7 8 is ok

6 3 6 8 0 is ok

3 6 7 0 5 is not possible and would turn to 3 6 7 0 0

Would somebody show me how to calculate the amount of combinations for this case and also in general, because I'd like to be able to calculate it also for 4 or 8 or 9 etc. elements. Later on I'd like to calculate this number in VBA to be able give the user a forecast how long my calculations will take.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Shalloon84
  • 21
  • 3
  • 2
    This is a pure mathematical question, not a programmatical one! And you also know how to distinguish the first value; how would you go for the next ones? – Joël Dec 01 '17 at 14:18
  • Yes but that's valid for any positions except the first one. That means 3 0 6 5 4 is also not valid. I thought the first element is not important for the caluclation because it anyway can't be zero. So if I do 8*9^4 - 9^3 -9^2 -9 i should have the possibilities. But that seems to be far too much. I coded the case with 5 elements in VBA and implemented a counter and I get 37448. – Shalloon84 Dec 01 '17 at 14:42
  • Are you saying you already have a VBA program to calculate the number, or do you want help in making such a program? Or are you saying you can now do it brute-force in VBA and you want a more efficient routine? Or something else? – Rory Daulton Dec 01 '17 at 15:00
  • Hi Rory, I have a VBA program to calculate values with the numbers mentioned above. But I can only do brute force in VBA and I would like to have a more efficient way. At the moment with 5 elements I have about 37448 iterations which takes with my laptop 0.4s. When I increase the number to 8 elements it takes already almost 4 minutes. – Shalloon84 Dec 01 '17 at 15:15

2 Answers2

2

Since once a 0 is present in the sequence, all remaining numbers in the sequence will also be 0, these are all of the possibilities: (where # below represents any digit from 1 to 8):

##### (accounts for 8^5 combinations)
####0 (accounts for 8^4 combinations)
...
#0000 (accounts for 8^1 combinations)

Therefore, the answer is (in pseudocode):

int sum = 0;

for (int x = 1; x <= 5; x++)
{
   sum = sum + 8^x;
}

Or equivalently,

int prod = 0;

for (int x = 1; x <= 5; x++)
{
   prod = 8*(prod+1);
}
ImaginaryHuman072889
  • 4,953
  • 7
  • 19
  • 51
0

great thank you.

Sub test()
Dim sum As Single

Dim x As Integer
For x = 1 To 6
    sum = sum + 8 ^ x
Next
Debug.Print sum
End Sub

With this code I get exactly 37488. I tried also with e.g. 6 elements and it worked as well. Now I can try to estimate the calculation time

Shalloon84
  • 21
  • 3
  • 1
    I agree with @Imaginary, except to say that self-answers are welcome, as long as that they are an final solution and not an intermediate progress report. – halfer Dec 10 '17 at 11:29