How do I find the lowest combination of multiplied values

Get help using Construct 2

Post » Sun Apr 30, 2017 1:03 pm

If I have an array and I want to find which combination of number pairs gives me the lowest total how would I do that?
For example if my array has the values 2,3,5 and 7.
2*3= 6, 5*7= 35. 35+6 = 41
2*5= 10, 3*7= 21. 21+10 = 31
2*7= 14, 3*5= 15. 15+14 = 29

So for these numbers the best combination of numbers is 2*7 and 3*5

Does that make sense?
B
22
S
8
Posts: 203
Reputation: 2,059

Post » Sun Apr 30, 2017 1:46 pm

The min function returns the smallest value

Min(7, 4, 3) = 3
B
25
S
16
G
26
Posts: 125
Reputation: 16,103

Post » Sun Apr 30, 2017 1:58 pm

Ok, that'll be really helpful thanks :) I'm still not sure how to get the program to run through every possible combination of the initial values though
B
22
S
8
Posts: 203
Reputation: 2,059

Post » Sun Apr 30, 2017 2:43 pm

I'm on mobile so I can't really play around with it atm, but it you would have to make a loop that iterates through the array with a nested loop in a subevent so it will run each time through the main loop. You may need a second array to store the new values
B
25
S
16
G
26
Posts: 125
Reputation: 16,103

Post » Sun Apr 30, 2017 3:55 pm

@bbjGames

I think you will always get the lowest total by multiplying the biggest number by the smallest, then the next biggest number by the next smallest...
you shouldn't have to try every combination. Just sort them and match them up - first with last, second with second last...
B
98
S
35
G
20
Posts: 438
Reputation: 17,875

Post » Sun Apr 30, 2017 4:15 pm

That works if it's groups of two but if it's 3 or 4 numbers multiplied together it doesn't work so well
B
22
S
8
Posts: 203
Reputation: 2,059

Post » Mon May 01, 2017 2:36 am

Turns out you don't need a recursive function to try all different orderings of the numbers. Instead just sort the list first and just multiply and sum them in order.
B
94
S
33
G
113
Posts: 5,356
Reputation: 73,273

Post » Wed May 03, 2017 1:18 am

How would that work? I mean like if my list is 1,2,3,4,5,6,7,8,9 in sets of 3 I would get something like
1*2*3 = 6
4*5*6 = 120
7*8*9 = 504

But there is definitely a better combination for that right? Or am I misunderstanding what you're saying
B
22
S
8
Posts: 203
Reputation: 2,059

Post » Wed May 03, 2017 2:44 am

I somehow missed you wanted the lowest value. Sorted it will give the highest value with pairs or probably triples. For the lowest you could reorder the array every possible way and try each one to see which is lowest.

I have a capx that does it that I can provide tomorrow if you want.

edit:
Here it is. It tries every combination and eventually gives the order with the lowest value:
https://www.dropbox.com/s/ajbvvy32cwd8m ... .capx?dl=0

The algorithm is O(n!) and the implementation is also slow. With 9 values it takes like 10 seconds on my machine. and gives this answer:
1*8*9 + 3*4*6 + 2*5*7 = 214
B
94
S
33
G
113
Posts: 5,356
Reputation: 73,273

Post » Fri May 05, 2017 11:30 pm

Thanks ^-^ even if it's slow it saves me a lot of hand work
B
22
S
8
Posts: 203
Reputation: 2,059


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 18 guests