Esempio n. 1
0
def problem43():
    """ The number, 1406357289, is a 0 to 9 pandigital number because it is 
    made up of each of the digits 0 to 9 in some order, but it also has a 
    rather interesting sub-string divisibility property. 

    Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we 
    note the following: 

    d2d3d4=406 is divisible by 2 d3d4d5=063 is divisible by 3 d4d5d6=635 is 
    divisible by 5 d5d6d7=357 is divisible by 7 d6d7d8=572 is divisible by 
    11 d7d8d9=728 is divisible by 13 d8d9d10=289 is divisible by 17 Find the 
    sum of all 0 to 9 pandigital numbers with this property. """ 
    def calculate(nums, divisor, level):
        new_nums = []
        for n in nums:
            for i in range(0, 10):
                k = i*(10**(level-1)) + int(n)
                if (k / (10**(level-3))) % divisor == 0:
                    new_nums.append(k)
        return new_nums

    nums = [str(k) for k in range(100, 999) if k % 17 == 0]
    divs = (13, 11, 7, 5, 3, 2, 1)
    level = 4
    for d in divs:
        nums = calculate(nums, d, level)
        level += 1
    
    pandigital = [n for n in nums if mlib.is_pandigital(n, 10)] 
    return sum(pandigital)
Esempio n. 2
0
def problem32():
    """ 
    We shall say that an n-digit number is pandigital if it makes 
    use of all the digits 1 to n exactly once; for example, the 
    5-digit number, 15234, is 1 through 5 pandigital.

    The product 7254 is unusual, as the identity, 39 x 186 = 7254, 
    containing multiplicand, multiplier, and product is 1 through 9 pandigital.

    Find the sum of all products whose multiplicand/multiplier/product 
    identity can be written as a 1 through 9 pandigital.    
    """
    res_map = {}
    i = 1
    j = 1
    while j < 10 ** 4:
        while True:
            x = str(i * j) + str(i) + str(j)
            if mlib.is_pandigital(x):
                res_map[i * j] = 1

            if len(x) > 9:
                i = 1
                break
            i += 1

        j += 1

    return sum(res_map.keys())
Esempio n. 3
0
def problem38():
    """ 
    Take the number 192 and multiply it by each of 1, 2, and 3:
    192 x 1 = 192
    192 x 2 = 384
    192 x 3 = 576
            
    By concatenating each product we get the 1 to 9 pandigital, 192384576. 
    We will call 192384576 the concatenated product of 192 and (1,2,3)
               
    The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 
    4, and 5, giving the pandigital, 918273645, which is the concatenated 
    product of 9 and (1,2,3,4,5).
           
    What is the largest 1 to 9 pandigital 9-digit number that can be formed 
    as the concatenated product of an integer with (1,2, ... , n) where n > 1?
    """
    # estimate max = 50000*1 + 50000*2
    max = 50000
    max_pandigit = 0

    k = 0
    while k < max:
        k += 1
        n = 1
        while True:
            n += 1

            ret = [str(k * x) for x in range(1, n)]
            ret_j = "".join(ret)
            if len(ret_j) > 9 and k > 0:
                break
            if mlib.is_pandigital(ret_j):
                if int(ret_j) > max_pandigit:
                    max_pandigit = int(ret_j)

    return max_pandigit