Consider the following problem:
How many ways are there to split the integers 1 through 14 into 7 pairs such that in each pair, the greater number is at least 2 times the lesser number?
This is obviously a math problem, and there are several solutions to it. I wondered if there is a way to write a python program instead, to automatically search through all possible pairs, and give me the correct answer. Right now I'll take this moment to clarify: I do not want a "reasonable" solution to this problem, I want a python program to solve it (goes through all the possible pairs).
While going through the possibilities of how I might do this, I considered using a set
, since the order of a set doesn't matter. If I had a for loop for instance, that counted all the pairs, the program would need to know when to stop counting the pairs or when it had already counted a certain pair. Since the for loop would have randomly ordered pairs, if I put those pairs into a set, it would likely make it easier to figure out if I had already counted that pair, since order does not matter in a set.
I'm kind of stuck right now...so I'm open to any suggestions on how I can build on this idea, or any other potential ideas (I don't have any code yet to show...hopefully coming soon).
UPDATE:
Resolved. It seems there are several ways to do this, and I built some code that works (using some helpful answers as well). Efficiency doesn't really matter here (since we're only pairing the numbers from 1-14), but as the number of pairs increase, it becomes more and more important to write efficient code, so that's a suggestion if you're doing something similar to this.
First we need to understand the problem. There are lots of ways to pair the numbers 1 through 14, for example:
(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12), (13, 14)
(one pair meets the criteria)(1, 14), (2, 13), (3, 12), (4, 11), (5, 10), (6, 9), (7, 8)
(five pairs meet the criteria)(1, 8), (2, 9), (3, 10), (4, 11), (5, 12), (6, 13), (7, 14)
(all pairs meet the criteria)When coming up with these pairings by hand, I first pick a number to pair with 1
, then I pick a number to pair with 2
unless two was already paired with 1
in which case I pick a pairing for 3
. The pattern continues until all numbers have been paired.
Lets implement that recursively in python:
def generate_pairings(unpicked_numbers, picked_pairs = ()):
if not unpicked_numbers:
# all of the numbers have been picked, and the pairs are finalized
yield picked_pairs
return
# the pairing is incomplete - there are more numbers to pair up
# go through every possible pairing and yield that
for i in range(1, len(unpicked_numbers)):
new_pair = (unpicked_numbers[0], unpicked_numbers[i])
new_unpicked_numbers = unpicked_numbers[1:i] + unpicked_numbers[i+1:]
yield from generate_pairings(new_unpicked_numbers, picked_pairs + (new_pair,))
Then, we implement a function to check if a certain pairing matches your criteria:
def all_pairs_max_double_min(pairs):
return all(max(pair) >= 2 * min(pair) for pair in pairs)
And finally, we simply loop through all of the combinations & check them individually.
numbers = tuple(range(1, 15))
count = 0
for pairing in generate_pairings(numbers):
if all_pairs_max_double_min(pairing):
count += 1
print(f"There are {count} combinations in which all pairs' larger value is at least double the smaller value.")
There are 144 combinations in which all pairs' larger value is at least double the smaller value.