#๐Ÿ”’ Summing all products whose multiplicand/multiplier/product identity is writable as a 1-n pandigita

43 messages ยท Page 1 of 1 (latest)

cinder marsh
#

How could I use this method to sum all the products whose multiplicand/multiplier/product identity is writable as a
1 through n pandigital (i.e. pandigital_product(4) and (6) should return 12 and 162)?

def is_pandigital(num):
    n = len(str(num))
    result = 0
    
    while num > 0:
        digit = num % 10
        if digit == 0 or digit > n:
            return False
    
        result |= (1 << (digit - 1))
        num //= 10
    
    return result == (1 << n) - 1```
quaint narwhalBOT
#

@cinder marsh

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.

hybrid nimbus
#

is this project euler #104?

cinder marsh
#

32, here's my old solution to show that i've completed it once:

#
""" calculates the sum of all products whose multiplicand/multiplier/product identity can be written as a 1-9 pandigital. """
from itertools import product

def isPandigital(n, digits="123456789"):
  return "".join(sorted(str(n))) == digits

products = set()
""" 'product' computes the cartesian product of a variable, as per #29. """
for a, b in product(range(1, 100), range (1, 2000)):
    product = a * b
    """ checked if it has already been added to set. If so, skip to the next iteration. """
    if product in products:
        continue
    """ if not, the function checks whether the concatenated identity is pandigital. """
    if isPandigital(str(a) + str(b) + str(product)):
        products.add(product)

print(sum(products))```
hybrid nimbus
#

I only solved that one in Racket, not python

#

I think I just tried all permutations of the 10 digits

cinder marsh
#

this works for 1-9, but the second method is kinda slow and bulky: ```py
def is_pandigital(n):
mask = 0
i = 0
while n > 0:
mask |= 1 << n % 10
n //= 10
i += 1
return 2 + mask == 1 << (i + 1)

def sum_range(m0, mn, n0, nn):
products = set()
for m in range(m0, mn + 1):
for n in range(n0, nn + 1):
x = m * n
p = int(str(m) + str(n) + str(x))
if is_pandigital(p):
products.add(x)
return products

products = sum_range(1, 9, 1234, 9876).union(sum_range(12, 98, 123, 987))
print(sum(products))```

hybrid nimbus
#

it's funny -- I've got the racket code for it, and it works quickly, and yet ... I don't understand it

cinder marsh
#

looks like it splits the vector into parts and then checks if it satisfies right = left * middl and counts those as unique permutations

#

?

hybrid nimbus
#

yep

cinder marsh
#

how can i finish this? ```py
def is_pandigital(n):
mask = 0
i = 0
while n > 0:
""" cuts off the right most digit in base 10 and uses it as the index of a bit
that is set on mask m. Take n = 123 and mask = 1110 2. Adding 1 will fill the
LSB with a 1, and adding another 1 will result in 10000 2, which is exactly
1 shifted by the length plus 1. This seamingly excludes zero and checks if
every digit was present only once. """
mask |= 1 << n % 10
n //= 10
i += 1
return 2 + mask == 1 << (i + 1)

def pandigital_products(n):

if name == "main":
print(pandigital_products(4))```

hybrid nimbus
#

I guess compute every possible product = left * middle and then examine all the digits in those three numbers

#

that seems like it'd be awfully slow

#

it's 2 or 3 times slower ๐Ÿ˜

#

it won't help you though, since the algorithm is different

#

do you have some reason for using is_pandigital?

#

it seems like your technique is something like ```
for every possible 9-digit-number "n":
if is_pandigital(n):
see if we can split it into three pieces such that left * middle == right

#

the problem there is that there are a lot of 9 digit numbers, and checking them all will take a long time

#

whereas I'm doing ```
for every possible permutation of "123456789":
see if we can split it ...


that'll be faster because ```
>>> pow(10, 9)
1000000000
>>> 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
362880
>>> 
cinder marsh
#

because your looking for factors to mulitply, and multiplication is commutative, you can half the search space i figured

proven coral
#

didn't knew that offby1 was into euler problems as wel;

#

๐Ÿ‘€

cinder marsh
#

yeah this is painful

proven coral
#

first few problems can be done with pen-paper

hybrid nimbus
#

I haven't done any in a while, and never got all that far

cinder marsh
#

so im going back to them

hybrid nimbus
#

starting with the easiest, of course

proven coral
#

205 did so far

hybrid nimbus
#

I always use brute force if I possibly can

proven coral
hybrid nimbus
#

๐Ÿค”

#

I figure if someone wants to cheat, they're cheating themselves.

#

it's not like Harvard is basing their admissions on your Project Euler score.

cinder marsh
#

!close

quaint narwhalBOT
#
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.