コード例 #1
0
def largest_product_of_series(length):
    largest_product = 0
    coord_x = coord_y = -1
    for y in xrange(HEIGHT):
        for x in xrange(WIDTH):
            is_in_bounds_right = x + length < WIDTH + 1
            is_in_bounds_bottom = y + length < HEIGHT + 1
            is_in_bounds_left = x > length
            # Right
            if is_in_bounds_right:
                s = product(GRID[y][x:x + length])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
            # Down
            if is_in_bounds_bottom:
                s = product([GRID[y + i][x] for i in xrange(length)])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
            # Diagonal down right
            if is_in_bounds_right and is_in_bounds_bottom:
                s = product([GRID[y + i][x + i] for i in xrange(length)])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
            # Diagonal down left
            if is_in_bounds_left and is_in_bounds_bottom:
                s = product([GRID[y + i][x - i] for i in xrange(length)])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
    # Y, X, PRODUCT
    return Result(coord_x, coord_y, largest_product)
コード例 #2
0
ファイル: euler11.py プロジェクト: explodes/euler-python
def largest_product_of_series(length):
    largest_product = 0
    coord_x = coord_y = -1
    for y in xrange(HEIGHT):
        for x in xrange(WIDTH):
            is_in_bounds_right = x + length < WIDTH + 1
            is_in_bounds_bottom = y + length < HEIGHT + 1
            is_in_bounds_left = x > length
            # Right
            if is_in_bounds_right:
                s = product(GRID[y][x:x + length])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
            # Down
            if is_in_bounds_bottom:
                s = product([GRID[y + i][x] for i in xrange(length)])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
            # Diagonal down right
            if is_in_bounds_right and is_in_bounds_bottom:
                s = product([GRID[y + i][x + i] for i in xrange(length)])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
            # Diagonal down left
            if is_in_bounds_left and is_in_bounds_bottom:
                s = product([GRID[y + i][x - i] for i in xrange(length)])
                if s > largest_product:
                    largest_product = s
                    coord_x, coord_y = x, y
    # Y, X, PRODUCT
    return Result(coord_x, coord_y, largest_product)
コード例 #3
0
ファイル: euler5.py プロジェクト: explodes/euler-python
def least_common_multiple(nums):
    return product(collect_factors(nums))
コード例 #4
0
ファイル: euler9.py プロジェクト: explodes/euler-python
 def test(self):
     assert product(triplet(3 + 4 + 5)) == 3 * 4 * 5
コード例 #5
0
ファイル: euler9.py プロジェクト: explodes/euler-python
 def run(self):
     return product(triplet(TARGET, search_delta=TARGET))
コード例 #6
0
ファイル: euler9.py プロジェクト: explodes/euler-python
def triplet_product(n):
    return product(triplet(n))
コード例 #7
0
ファイル: euler8.py プロジェクト: explodes/euler-python
def product_of_digits(digits):
    return product(map(int, digits))