Example #1
0
File: e0011.py Project: eykd/euler
def main():
    line = []
    values = []
    max_prod = 0
    for y in xrange(20):
        for x in xrange(20):
            for dy, dx in transformations:
                ady = abs(dy)
                adx = abs(dx)
                mdy = ady / dy if dy != 0 else 0
                mdx = adx / dx if dx != 0 else 0
                line[:] = ((y, x),)
                for extension in xrange(3):
                    line.append((y+dy, x+dx))
                    dy += mdy
                    dx += mdx
                try:
                    values[:] = (grid[y][x] for y, x in line)
                except IndexError:
                    continue
                prod = product(values)
                if prod > max_prod:
                    print "Origin:", y, x, "Line:", line
                    print "Magnitude:", mdy, mdx
                    print "Val: ", values, "Product:", product(values)
                max_prod = max(max_prod, prod)
    return max_prod
def getDiscriminativeScore(counts_all):
    counts_reduce = []
    for counts in counts_all:
        c_r = [float(c) / sum(counts) for c in counts]
        counts_reduce.append(c_r)
    ranks = [util.product(counts) for counts in counts_reduce]
    return ranks
def getDiscriminativeScore(counts_all):
    counts_reduce=[];
    for counts in counts_all:
        c_r=[float(c)/sum(counts) for c in counts];
        counts_reduce.append(c_r);
    ranks=[util.product(counts) for counts in counts_reduce];
    return ranks
Example #4
0
def divisors(n):
  factors = {}
  for f in primes.factors(n, p):
    if not f in factors:
      factors[f] = 1
    factors[f] = factors[f] + 1
  return util.product(factors.values())
Example #5
0
def products():
    for y, x, dy, dx in lines():
        try:
            yield product(numbers[y + dy * i][x + dx * i]
                          for i in xrange(length))
        except IndexError:
            pass
Example #6
0
def solve():
    fraction = []
    for i in count(1):
        fraction.extend(str(i))
        if len(fraction) >= 1000000:
            break
    return product(int(fraction[10 ** i - 1]) for i in xrange(7))
Example #7
0
def solve():
    max_factors = defaultdict(int)
    for i in xrange(1, 21):
        factors = defaultdict(int)
        for j in factor(i):
            factors[j] += 1
        for k in factors:
            max_factors[k] = max(max_factors[k], factors[k])
    return product(k ** v for k, v in max_factors.iteritems())
Example #8
0
def bias_variable(shape):
    '''
    Generates a TensorFlow Tensor. This Tensor gets initialized with values sampled from <some?> distribution.
    Its purpose will be to store bias values.
    :param shape: The dimensions of the desired Tensor
    :return: The initialized Tensor
    '''
    size = 1.0 / math.sqrt(util.product(shape))
    initial = tf.random_uniform(shape, -size, size)
    return tf.Variable(initial, name='bias')
Example #9
0
def largest_product(num_str, digits):
    largest_product = None

    for start in xrange(len(num_str) - digits + 1):
        string = num_str[start: start + digits]
        nums = map(int, list(string))
        prod = product(nums)

        if prod > largest_product:
            largest_product = prod

    return largest_product
Example #10
0
    def _check_prime_factors_multiply(num,
                                      prime_facts,
                                      new_prime_facts=None,
                                      prime_idx_to_exponent=0,
                                      exponent=2):
        product = ut.product(
            new_prime_facts) if new_prime_facts else ut.product(prime_factors)
        if product == num:
            return True

        # if smaller, for each of the prime factors, try increasingly higher powers UNTIL exceed
        elif product < num:
            if new_prime_facts:
                new_prime_facts = new_prime_facts[0:prime_idx_to_exponent] + [
                    new_prime_facts[prime_idx_to_exponent]**exponent,
                ] + new_prime_facts[prime_idx_to_exponent + 1:]

            else:
                new_prime_facts = prime_facts[0:prime_idx_to_exponent] + [
                    prime_facts[prime_idx_to_exponent]**exponent,
                ] + prime_facts[prime_idx_to_exponent + 1:]

            return _check_prime_factors_multiply(num, prime_facts,
                                                 new_prime_facts,
                                                 prime_idx_to_exponent,
                                                 exponent + 1)

        elif product > num and prime_idx_to_exponent + 1 < len(prime_facts):
            # exceeded target number BUT still other
            return _check_prime_factors_multiply(
                num,
                prime_facts,
                new_prime_facts=None,
                prime_idx_to_exponent=prime_idx_to_exponent + 1,
                exponent=2)

        else:
            return False
Example #11
0
def problem011(data, n):
  rows = len(data)
  cols = len(data[0])
  
  m = 0
  # horizontal slices of length n
  horiz = (data[row][col:col+n] for row in xrange(0, rows) for col in xrange(0, cols - n + 1))
  # vertical slices of length n
  vert =  ([data[row+i][col] for i in xrange(0, n)] for col in xrange(0, cols) for row in xrange(0, rows - n + 1))
  # diagonal slices of length n
  diag =  itertools.ifilter(lambda x: len(x) == n, ([data[row+i][col+i] for i in xrange(0, n) if row + i < rows and col + i < cols] for row in xrange(0, rows) for col in xrange(0, cols)))
  # anti-diagonal slices of length n
  anti = itertools.ifilter(lambda x: len(x) == n, ([data[row+i][col-i] for i in xrange(0, n) if row + i < rows and col - i >= 0] for row in xrange(0, rows) for col in xrange(0, cols)))
  
  return max(util.product(range) for range in itertools.chain(horiz, vert, diag, anti))
Example #12
0
File: e0008.py Project: eykd/euler
def main():
    # Take the digits from the docstring and throw them into a deque.
    digits = deque(reversed(''.join(__doc__.split('\n\n')[-1].strip().split())))

    max_product = 0

    # Deques don't support slice notation, so we have to use a fancy
    # generator to grab the first five items. We want to use a deque,
    # since it offers fast endpoint access/modification.
    slice_size = range(5)

    while len(digits) >= 5:
        di = iter(digits)
        max_product = max(max_product, product(int(di.next()) for d in slice_size))
        digits.popleft()
    
    return max_product
Example #13
0
def left(r,c):
    if c < 3:
        return 0
    else:
        return product(grid[r][c-3:c+1])
Example #14
0
def right(r,c):
    if c > len(grid)-4:
        return 0
    else:
        return product(grid[r][c:c+4])
Example #15
0
def up(r,c):
    if r < 3:
        return 0
    else:
        return product(grid[r-i][c] for i in range(0,4))
Example #16
0
def down(r,c):
    if r > len(grid)-4:
        return 0
    else:
        return product(grid[r+i][c] for i in range(0,4))
Example #17
0
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""

if __name__ == "__main__":
    grid = [map(int, x.split(' ')) for x in grid.split('\n')]

    mp = 0  # max product

    # horizontal
    for y in xrange(len(grid)):
        for x in xrange(len(grid) - 3):
            mp = max(mp, product([grid[y][x + i] for i in xrange(4)]))
    # vertical
    for x in xrange(len(grid[0])):
        for y in xrange(len(grid) - 3):
            mp = max(mp, product([grid[y + i][x] for i in xrange(4)]))
    # diagonal right
    for x in xrange(len(grid[0]) - 3):
        for y in xrange(len(grid) - 3):
            mp = max(mp, product([grid[y + i][x + i] for i in xrange(4)]))
    # diagonal left
    for x in xrange(3, len(grid[0]) - 3):
        for y in xrange(len(grid) - 3):
            mp = max(mp, product([grid[y + i][x - i] for i in xrange(4)]))

    print mp
Example #18
0
def main():
    digits = concat_digit()
    print product(int(digits[10**i]) for i in range(6))
Example #19
0
def solve():
    length = 5
    digits = [int(c) for c in number if c.isdigit()]
    return max(product(digits[i:i + length])
               for i in xrange(len(digits) - length))
Example #20
0
def problem8(data, digits=5):
    return max(util.product(map(int, data[i : i + digits])) for i in xrange(0, len(data) - digits))
Example #21
0
def num_factors(n):
    factors = prime_factorize(n)
    if n == 1:
        return 1
    return product(x + 1 for x in factors.values())
Example #22
0
def main():
    decimal = "".join(str(i) for i in xrange(1,1000000))[:1000000]
    digits = (int(decimal[i-1]) for i in (10**j for j in xrange(7)))
    print "The product of d1 * d10 * d100 * d1000 * d10000 * d100000",
    print "* d1000000 is:", util.product(digits)
Example #23
0
def diagonal_right(r,c):
    if r > len(grid)-4 or c > len(grid)-4:
        return 0
    else:
        return product(grid[r+i][c+i] for i in range(0,4))
Example #24
0
#! /usr/bin/env python

from util import product

if __name__ == "__main__":
    num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
    ans = 0

    for i in xrange(len(num) - 4):
        ans = max(ans, product(int(num[i + x]) for x in xrange(5)))

    print ans
Example #25
0
def divisorcount(n):
    factorcount = defaultdict(int)
    for i in factor(n):
        factorcount[i] += 1
    return product(count + 1 for count in factorcount.itervalues())
Example #26
0
def main():
    number = [int(i) for i in list(num)]
    print max(product(number[i:i+5]) for i in range(len(number)-5))
Example #27
0
def problem9(n):
  return util.product(util.nth(1, ((a,b,c) for a in xrange(1, n) for b in xrange(1, n) for c in xrange(1, n) if a+b+c == n and a**2 + b**2 == c**2)))
Example #28
0
def diagonal_left(r,c):
    if r > len(grid)-4 or c < 3:
        return 0
    else:
        return product(grid[r+i][c-i] for i in range(0,4))
Example #29
0
from math import sqrt
from util import primesfrom2to, product, proper_divisors

p = product([int(x) for x in list(primesfrom2to(190))])
print max(filter(lambda x: x < sqrt(p), proper_divisors(p)))