#🔒 Optimise code for summing pandigital products

4 messages · Page 1 of 1 (latest)

native aurora
#

How can I optimise the following code to sum all the products whose multiplicand/multiplier/product identity is writable as a
1 through n pandigital?

def pandigital_products(n):
    digits = set("123456789"[:n])
    products = set()
    """ maximum possible multiplicand ensures the product remains within a 4-digit 
    number. """
    for i in range(2, 80):
        for j in range(2, 9000 // i):
            """ if the concatenation of i, j, and i * j forms a pandigital of 
            length n. An empty resulting string means all digits from 1-n were used 
            exactly once. """
            product = i * j
            identity = str(i) + str(j) + str(product)
            if len(identity) == n and set(identity) == digits:
                """ set ensures that each product is unique. """
                products.add(product)
    return sum(products)```
gentle sorrelBOT
#

@native aurora

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gentle sorrelBOT
#

@native aurora

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.