Skip to content

Instantly share code, notes, and snippets.

/-

Created August 14, 2014 18:37
Show Gist options
  • Save anonymous/83378f66ed6dd4144f58 to your computer and use it in GitHub Desktop.
Save anonymous/83378f66ed6dd4144f58 to your computer and use it in GitHub Desktop.
import operator
def generate_decreasing_n_sequence_with_bounded_sum(sequence_length, sum_bound, value_bound=float('inf')):
if sequence_length == 0:
yield []
return
min_remaining = sequence_length*(sequence_length - 1)/2
bound_for_current = min(sum_bound - min_remaining, value_bound)
for value in range(sequence_length, bound_for_current):
for sequence in generate_decreasing_n_sequence_with_bounded_sum(sequence_length - 1, sum_bound - value, value_bound=value):
yield [value] + sequence
def build_products_to_sequences_map():
product_to_sequences_map = {}
for sequence in generate_decreasing_n_sequence_with_bounded_sum(4, 18):
product = reduce(operator.mul, sequence, 1)
product_to_sequences_map.setdefault(product, []).append(sequence)
return product_to_sequences_map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment