def ans(upper_bound, nums): result = 0 for i in range(1, len(nums) + 1): for each in combinations(nums, i): if i % 2: result += sum_divisble_by(product(each), upper_bound - 1) else: result -= sum_divisble_by(product(each), upper_bound - 1) return result
def ans(upper_bound, nums): result = 0 for i in range(1, len(nums)+1): for each in combinations(nums, i): if i % 2: result += sum_divisble_by(product(each), upper_bound-1) else: result -= sum_divisble_by(product(each), upper_bound-1) return result
def solution(): products = [] for i, row in enumerate(grid): for j, num in enumerate(row): if (j + ADJ) <= width: numbers = row[j:j + ADJ] products.append(euler.product(numbers)) if (i + ADJ) <= height: products.append(euler.product(adjacent(i, j, 1, 1))) if (i + ADJ) <= height: products.append(euler.product(adjacent(i, j, 1, 0))) if (j - ADJ) >= -1: products.append(euler.product(adjacent(i, j, 1, -1))) return max(products)
def main(): lst = [ [int(item) for item in line.split()] for line in open('../txt/problem011.txt') ] length = len(lst) maximum = 0 for i in range(length - NUMBERS + 1): for j in range(length - NUMBERS + 1): for start_i, start_j, d_i, d_j in [ (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (NUMBERS - 1, 0, -1, 1) ]: maximum = max( maximum, euler.product( ( lst[i + start_i + d_i * n][j + start_j + d_j * n] for n in range(NUMBERS) ) ) ) return maximum
def chinese_remainder(divisors, remainders): result = 0 product = euler.product(divisors) for divisor, remainder in zip(divisors, remainders): product_i = product // divisor result += remainder * mul_inv(product_i, divisor) * product_i return result % product
def count_sets(permut, prev): global count if not permut: prod = product(prev) if prod not in uniq_prods: uniq_prods.add(prod) count += 1 for end in range(len(permut)): candidate = get_num(permut, end) if isPrime(candidate): count_sets(permut[end+1:], prev + [candidate])
def main(): prime_list = euler.prime_list(100) total = 0 for combination in itertools.combinations(prime_list, 4): maximum = combination[-1] lst = [ prime for prime in prime_list if prime < maximum and prime not in combination ] product = euler.product(combination) total += euler.is_not_divisible(LIMIT // product, lst, len(lst)) return total
def getans(asl, minans): global minanslen candidate = product(p**a for p, a in zip(ps, asl)) if candidate >= minans: return minans prod = product(2*a + 1 for a in asl) if prod > 2*THRESH: print((candidate, asl, product(2*a + 1 for a in asl))) minans = candidate minanslen = len(asl) if all(a == 1 for a in asl): minans = getans(asl + [1], minans) for j in range(len(asl)): ascpy = asl[:] ascpy[j] += 1 if j > 0 and ascpy[j] > ascpy[j - 1]: continue if ascpy[j] > 10: break minans = getans(ascpy, minans) if len(asl) < minanslen - 2: return minans return minans
def largest_product_in_a_grid(grid, chunk_len): """ finds the largest consecutive product in a grid (horiz, vert, diag) grid (list): 2d list or array representing grid dimensions and values chunk_len (int): length of consecutive numbers to check returns (int): solution """ stub = Stub() #stub.start() grid_rows = len(grid) grid_cols = len(grid[0]) largest = 0 # check N-S, W-E, NW-SE products for i in range(grid_rows - chunk_len): for j in range(grid_cols - chunk_len): n_s = [grid[i+n][j] for n in range(chunk_len)] w_e = grid[i][j:j+chunk_len] nw_se = [grid[i+n][j+n] for n in range(chunk_len)] for chunk in [n_s, w_e, nw_se]: if product(chunk) > largest: stub.msg(chunk) largest = product(chunk) # check SW-NE products for i in range(chunk_len-1, grid_rows): for j in range(grid_cols - chunk_len): sw_ne = [grid[i-n][j+n] for n in range(chunk_len)] if product(sw_ne) > largest: stub.msg(sw_ne) largest = product(sw_ne) return largest
def numPermutsViable(xs, permut): ys = xs[:] for x in permut: if x != 0: ys.remove(x) if not ys: return 1 permuts = 0 for leadingZeroes in range(0, 12 - len(ys)): for y in set(ys): zs = ys[:] zs.remove(y) equivs = [zs.count(z) for z in zs] + [11 - leadingZeroes - len(ys)] permuts += fac(10 - leadingZeroes) // product([fac(e) for e in equivs]) return permuts
def special_pythagorean_triplet(): """ finds the special pythagorean triplet that also sums to 1000 returns (int): product of triplet """ stub = Stub() # stub.start() # generate power set of numbers that sum to 1000 counter = 0 for triplet in gen_triplets_that_sum_to(1000): if is_pythagorean(*triplet): stub.msg(triplet, counter) return product(triplet) counter += 1 raise ValueError("unable to find special pythagorean triplet after " + str(counter) + " trials.")
def largest_product_in_a_series(series, length): """ finds the largest product of a subseries in a series series (str): numeric series length (int): length of subseries to check products of returns (int): solution """ # reassign 'series' as a list of integers series = [int(char) for char in series] # loop through 'series' in size 'length' chunks and compare w/ 'largest' largest = None for i in range(len(series)-length): calc_product = product(series[i:i+length]) if calc_product > largest: largest = calc_product return largest
def main(): arrays = {(2, 2)} d = {k: k * 2 for k in range(2, LIMIT + 1)} while arrays: new_arrays = set() for lst in arrays: product = euler.product(lst) k = product - sum(lst) + len(lst) if k > LIMIT: continue if d[k] > product: d[k] = product new_arrays.add((lst[0] + 1, ) + lst[1:]) for i in range(1, len(lst)): if lst[i - 1] > lst[i]: new_arrays.add(lst[:i] + (lst[i] + 1, ) + lst[i + 1:]) new_arrays.add(lst + (2, )) arrays = new_arrays return sum(set(d.values()))
def euler8(): number = """731671765313306249192251196744265747 4235534919493496983520312774506326239578318016 9848018694788518438586156078911294949545950173 7958331952853208805511125406987471585238630507 1569329096329522744304355766896648950445244523 1617318564030987111217223831136222989342338030 8135336276614282806444486645238749303589072962 9049156044077239071381051585930796086670172427 1218839987979087922749219016997208880937766572 7333001053367881220235421809751254540594752243 5258490771167055601360483958644670632441572215 5397536978179778461740649551492908625693219784 6862248283972241375657056057490261407972968652 4145351004748216637048440319989000889524345065 8541227588666881164271714799244429282308634656 7481391912316282458617866458359124566529476545 6828489128831426076900422421902267105562632111 1109370544217506941658960408071984038509624554 4436298123098787992724428490918884580156166097 9191338754992005240636899125607176060588611646 7109405077541002256983155200055935729725716362 69561882670428252483600823257530420752963450""".replace('\n', '') print(max(product(number[i:i+13]) for i in range(1000-13)))
import euler def divisor_permutations(n, largest): if n == 1: yield [] divisors = euler.divisors(n) divisors = [ d for d in divisors if d > 1 and d <= largest] for d in divisors: for rest in divisor_permutations(n/d, d): yield [d] + rest solns = {} for i in xrange(15000): for divs in divisor_permutations(i, i): # pad the sequence to the correct length act = divs + ([1] * (euler.product(divs) - sum(divs))) if len(divs) > 1: if len(act) not in solns or solns[len(act)] > i: solns[len(act)] = i vals = set() for k in xrange(2,12000): vals.add(solns[k]) print vals print 'total', sum(vals)
# Also note that the denominator must be more than the numerator from euler import digits, product from math import gcd # Find the numerators and denominators of the fractions that satisfy this fracs = set() for num in range(10, 100): num_digs = digits(num) for den in range(num + 1, 100): den_digs = digits(den) # Skip the 'trivial' cases if num_digs[0] == 0 and den_digs[0] == 0: continue # If we have matching digits if num_digs[0] == den_digs[1]: if (num * den_digs[0] == den * num_digs[1]): fracs.add((num_digs[1], den_digs[0])) if num_digs[1] == den_digs[0]: if (num * den_digs[1] == den * num_digs[0]): fracs.add((num_digs[0], den_digs[1])) # Once we've got the fractions, we need to find the product and get its reduced denominator # We first find the lcm of the fraction denominators prod_num = product(x[0] for x in fracs) prod_den = product(x[1] for x in fracs) g = gcd(prod_num, prod_den) print(prod_den // g)
# 648 import euler N = 100 print sum([int(c) for c in str(euler.product(xrange(1, N + 1)))])
from euler import product from itertools import count def end_l(lim): end_l = [0] pos, nd = 0, 0 while pos <= lim: pos += (10**nd * 9) * (nd+1) nd += 1 end_l.append(pos) return end_l def get_digit(pos, end_l): for nd in count(1): end_p = end_l[nd] if pos <= end_p: break pos -= end_l[nd-1] + 1 n_pos = pos/nd n = n_pos + 10**(nd-1) dig_pos = pos - n_pos*nd return int(str(n)[dig_pos]) l = end_l(1000000) print product(get_digit(10**p, l) for p in xrange(7))
def main(): return max( euler.product((int(digit) for digit in NUMBER[i:i + 13])) for i in range(len(NUMBER) - 13 + 1))
from decimal import Decimal from euler import choose,product ways = [Decimal(0) for _ in range(0,8)] for r in range(0,11): for o in range(0,min(11, 21-r)): for y in range(0,min(11,21-r-o)): for g in range(0,min(11,21-r-o-y)): for b in range(0,min(11,21-r-o-y-g)): for i in range(0,min(11,21-r-o-y-g-b)): v = 20 - sum([r,o,y,g,b,i]) roygbiv = [r,o,y,g,b,i,v] count = sum(1 for x in roygbiv if x > 0) different_ways = product(choose(10,x) for x in roygbiv) ways[count] += Decimal(different_ways) tot_ways = sum(ways) EV = sum(Decimal(x) * (ways[x] / tot_ways) for x in range(2,8)) print(EV)
from euler import product s = '.' + ''.join((str(x) for x in range(1, 10**6))) print product(int(s[10**d]) for d in range(7))
min_north_row = prod_size - 1 max_south_row = n_rows - prod_size min_west_col = prod_size - 1 max_east_col = n_cols - prod_size for row in range(n_rows): for col in range(n_cols): # Southeast if row <= max_south_row and col <= max_east_col: indices += [[(row + i, col + i) for i in range(prod_size)]] # Northwest if row >= min_north_row and col >= min_west_col: indices += [[(row - i, col - i) for i in range(prod_size)]] # Southwest if row <= max_south_row and col >= min_west_col: indices += [[(row + i, col - i) for i in range(prod_size)]] # Northeast if row >= min_north_row and col <= max_east_col: indices += [[(row - i, col + i) for i in range(prod_size)]] # We've now got all of the indices to check # So now we can find the max product for index_set in indices: prod = product([grid[r][c] for (r,c) in index_set]) if prod > max_prod: max_prod = prod print(max_prod)
from euler import product s = '.' + ''.join((str(x) for x in range(1,10**6))) print product(int(s[10**d]) for d in range(7))
# 2783915460 import euler N = 10 T = 1000000 f = euler.product(xrange(1, N)) v = T - 1 a = [] d = [n for n in xrange(N)] for n in xrange(N): x = v / f v %= f a.append(d[x]) del d[x] if n + 1 != N: f /= N - n - 1 print ''.join([str(x) for x in a])
def main(): string = ''.join(str(i) for i in range(0, 250000)) return euler.product(int(string[10**i]) for i in range(7))
# 7652413 import euler N = 7 m = 0 d = '123456789' for i in xrange(euler.product(xrange(1, N + 1))): v = [] dd = list(d) for j in xrange(N): v.append(dd.pop(i % (N - j))) i /= (N - j) vv = int(''.join(v)) if euler.is_prime(vv) and vv > m: m = vv print m
# 40730 import euler f = [1] f += [euler.product(range(1, i + 1)) for i in range(1, 10)] s = 0 for n in xrange(11, 7 * f[9]): if n == sum([f[int(c)] for c in str(n)]): s += n print s
73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 """ s = ''.join(s.split()) m = 0 for n in xrange(len(s) - 4): val = s[n:n+5] x = euler.product([int(c) for c in val]) if x > m: m = x print m
import euler def divisor_permutations(n, largest): if n == 1: yield [] divisors = euler.divisors(n) divisors = [d for d in divisors if d > 1 and d <= largest] for d in divisors: for rest in divisor_permutations(n / d, d): yield [d] + rest solns = {} for i in xrange(15000): for divs in divisor_permutations(i, i): # pad the sequence to the correct length act = divs + ([1] * (euler.product(divs) - sum(divs))) if len(divs) > 1: if len(act) not in solns or solns[len(act)] > i: solns[len(act)] = i vals = set() for k in xrange(2, 12000): vals.add(solns[k]) print vals print 'total', sum(vals)
# for more information about this problem, check # http://mathschallenge.net/index.php?section=faq&ref=number/number_of_divisors # # According to Fundamental Theorem of Arithmetic (or Unique Prime-Factorization # theorem) <http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic>, # n can be uniquely represented by a^x * b^y * c^z, where a, b, c are primes # let d(n) = the number of divisors of n # then d(n) = (x+1) * (y+1) * (z+1) from euler import uniprimefact, product from itertools import count def trinum(n): ''' Return the n-th triangle number ''' return n*(n+1)/2 def trinumgen(): ''' Generate the sequence of triangle numbers ''' for n in count(1): yield trinum(n) for trin in trinumgen(): if product(each + 1 for each in uniprimefact(trin).values()) > 500: print trin break
# 4075 import euler N = 1000000 M = 100 s = 0 f = [1] f += [euler.product(xrange(1,n+1)) for n in xrange(1, M+1)] for n in xrange(1,M+1): for r in xrange(n+1): if f[n] / f[r] / f[n-r] > N: s += 1 print s
def rad(n): return euler.product(euler.primeFactors(n))
def Pr(q, n, k): C = cmath.exp(complex(0,2*cmath.pi/(n + 1))) return 1/(n+1) * sum(C**(-l * k) * product(1 + (C**l - 1)*p(m,q) for m in range(1,n+1)) for l in range(0, n+1))
''' the idea is simple: factorize every number from 1 to 20, that is to reduece every number to its basic form (a unique form expessed using only prime numbers, due to the fact that every natural number has a unique form). and then find the maximum power of each prime element. 1*2*3*(2*2)*5*(2*3)*7*(2*2*2)*(3*3)*(2*5)*11*(2*2*3)*13*(2*7)*(3*5)*(2*2*2*2)*17*(2*3*3)*19*(2*2*5) 1*(2**4)*(3**2)*5*7*11*13*17*19 ''' from euler import uniprimefact, product nums = range(1, 20) d = {} for i in nums: for (prime, power) in uniprimefact(i).items(): if (prime in d and d[prime] < power) or (prime not in d): d[prime] = power print product(prime**power for (prime, power) in d.items())
# 232792560 import euler D = [n for n in xrange(2, 21)] n = euler.product(D) m = 0 for d in D: nx = n / d while all([nx % dx == 0 for dx in D]): n = nx nx = n / d m = n print m
def main(): return euler.product( next(trio for c in range(SUM) for trio in euler.pythagorean_trio_my(c) if sum(trio) == SUM))
# 137846528820 import euler N = 20 f20 = euler.product(xrange(1, N + 1)) print euler.product(xrange(1, N * 2 + 1)) / (f20 * f20)
52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450""" # Initial setup n_factors = 13 values = [ int(digits[x]) for x in range(n_factors) ] max_prod = product(values) # Perform the calculation for i in range(n_factors, len(digits)): try: new_value = int(digits[i]) except: continue # Skip newline characters values.pop(0) values.append(new_value) new_prod = product(values) if new_prod > max_prod: max_prod = new_prod print(max_prod)
import euler PRIME_LIST = euler.prime_list(190) LIMIT = euler.product(PRIME_LIST) SQRT = euler.int_sqrt(LIMIT) GLOBAL = {'result': 0} def work(number, product, index): for i in range(42 - 1, index, -1): new_product = product * PRIME_LIST[i] new_number = number - number % new_product if new_number > GLOBAL['result']: if not LIMIT % new_number: GLOBAL['result'] = new_number print(GLOBAL['result'] % 10**16) work(new_number, new_product, i) def main(): work(SQRT, 1, -1) return GLOBAL['result'] % 10**16 if __name__ == '__main__': print(main())
def main(): return euler.product( prime ** int(math.log(LIMIT, prime)) for prime in euler.prime_list(LIMIT + 1) )
from euler import rational, primes, product def ways(n): w = 0 for a in range(1, 2*n + 1): if (rational(1, n) - rational(1, a)).num == 1: w += 1 return w ps = primes(100) min_val = float("inf") for n in range(1, len(ps)): for k in range(0,4): for m in range(0,4): N = product(ps[:n]) * (2**k) * (3**m) if N < min_val: ws = ways(N) if ws > 1000: min_val = N print(min_val, ws) print(min_val)
# for more information about this problem, check # http://mathschallenge.net/index.php?section=faq&ref=number/number_of_divisors # # According to Fundamental Theorem of Arithmetic (or Unique Prime-Factorization # theorem) <http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic>, # n can be uniquely represented by a^x * b^y * c^z, where a, b, c are primes # let d(n) = the number of divisors of n # then d(n) = (x+1) * (y+1) * (z+1) from euler import uniprimefact, product from itertools import count def trinum(n): ''' Return the n-th triangle number ''' return n * (n + 1) / 2 def trinumgen(): ''' Generate the sequence of triangle numbers ''' for n in count(1): yield trinum(n) for trin in trinumgen(): if product(each + 1 for each in uniprimefact(trin).values()) > 500: print trin break
def find_max_product(nums): nums = map(int, nums) return max(product(x) for x in n_grams(nums, 5))
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 """ s = s.split() grid = [s[i*20:i*20+20] for i in xrange(20)] for i in xrange(20): grid[i] = [int(v, 10) for v in grid[i]] m = 0 for r in xrange(20): for c in xrange(20): if c < 20 - 3: v = grid[r][c:c+4] t = euler.product(v) if t > m: m = t if r < 20 - 3: v = [grid[r + i][c] for i in xrange(4)] t = euler.product(v) if t > m: m = t if r < 20 - 3 and c < 20 - 3: v = [grid[r + i][c + i] for i in xrange(4)] t = euler.product(v) if t > m: m = t v = [grid[r + 3 - i][c + i] for i in xrange(4)] t = euler.product(v) if t > m: