Example #1
0
def main(target):
    # trick: minimize f(n, m) = |countRec(n, m) - target| over the integers, easy since convex
    maxw = int(sqrt(0.25 + 2 * sqrt(target)) - 0.5)
    dims = ((w, round(sqrt(0.25 + 4 * target / (w * (w + 1))) - 0.5))
            for w in range(1, maxw + 1))
    distToTarget = lambda d: abs(target - countRec(d[0], d[1]))
    return multiply(min(dims, key=distToTarget))
Example #2
0
def main(limit):
  hits = set()
  for c in range(limit):
    f = list(primes.factors(c))
    R = multiply(p**(n-1) for p, n in f)
    badf = set(p for p, _ in f)
    for a in aa((p for p in primes.gen() if p not in badf), c):
      b = c - a
      if a < b and rad(a)*rad(b) < R and dpf(b).isdisjoint(dpf(a) | badf):
        hits.add((a, b, c))
  return sum(c for _, _, c in hits)
Example #3
0
def main(limit):
    hits = set()
    for c in range(limit):
        f = list(primes.factors(c))
        R = multiply(p**(n - 1) for p, n in f)
        badf = set(p for p, _ in f)
        for a in aa((p for p in primes.gen() if p not in badf), c):
            b = c - a
            if a < b and rad(a) * rad(b) < R and dpf(b).isdisjoint(
                    dpf(a) | badf):
                hits.add((a, b, c))
    return sum(c for _, _, c in hits)
Example #4
0
def main(limit):
	solns = lambda n: (multiply(2 * e + 1 for _, e in primes.factors(n)) + 1) // 2

	n = 1
	bases = []
	for p in primes.gen():
		n *= p
		bases.append(n)
		if solns(n) > limit:
			break

	rv = bases.pop()
	for base in reversed(bases):
		candidates = (base * n for n in count(1))
		n = next(n for n in candidates if solns(n) > limit)
		if rv <= n:
			return rv
		rv = n
Example #5
0
def main(limit):
	solns = lambda n: (multiply(2 * e + 1 for _, e in primes.factors(n)) + 1) // 2

	n = 1
	bases = []
	for p in primes.gen():
		n *= p
		bases.append(n)
		if solns(n) > limit:
			break

	rv = bases.pop()
	for base in reversed(bases):
		candidates = (base * n for n in count(1))
		n = next(n for n in candidates if solns(n) > limit)
		if rv <= n:
			return rv
		rv = n
Example #6
0
def main(limit):
  hits = set()
  for c in range(limit):
    f = list(primes.factors(c))
    R = multiply(p**(n-1) for p, n in f)
    badf = set(p for p, _ in f)
    for secs in sec((p for p in primes.gen() if p not in badf), R):
      ws = [1]
      for n in secs:
        nw = []
        for a in ws:
          while a < c:
            nw.append(a)
            a *= n
        ws = nw
      for a in ws:
        b = c - a
        if a < b and rad(a)*rad(b) < R and dpf(b).isdisjoint(dpf(a) | badf):
          hits.add((a, b, c))
  return sum(c for _, _, c in hits)
Example #7
0
def main(limit):
    hits = set()
    for c in range(limit):
        f = list(primes.factors(c))
        R = multiply(p**(n - 1) for p, n in f)
        badf = set(p for p, _ in f)
        for secs in sec((p for p in primes.gen() if p not in badf), R):
            ws = [1]
            for n in secs:
                nw = []
                for a in ws:
                    while a < c:
                        nw.append(a)
                        a *= n
                ws = nw
            for a in ws:
                b = c - a
                if a < b and rad(a) * rad(b) < R and dpf(b).isdisjoint(
                        dpf(a) | badf):
                    hits.add((a, b, c))
    return sum(c for _, _, c in hits)
Example #8
0
#!/usr/bin/env python3
import lib
from collections import Counter

# The prime factors of each number 1 to 20
counts = ([Counter(lib.prime_factors(i)) for i in range(2, 21)])
total = Counter()
# Get the max count of each factor
for count in counts:
    for n in count:
        if total[n] < count[n]:
            total[n] = count[n]

res = lib.multiply([n**total[n] for n in total.keys()])
print(res)
import sys
import lib


matrix1 = []
matrix2 = []


lib.read('./matrix1.csv', matrix1)
lib.read('./matrix2.csv', matrix2)

# lib.read(sys.argv[1], matrix1)
# lib.read(sys.argv[2], matrix2)

if len(matrix2) != len(matrix1[0]):
    print('undefinied')
    sys.exit()


added = lib.multiply(matrix1, matrix2)

if lib.modify(added, mode='transpone') == added:
    print('true')
else:
    print('false')
Example #10
0
def countPrimeSets2(digits):
    rv = 0
    for groups in subsetCombinations(digits, filter=countPrimes):
        rv += multiply(map(countPrimes, groups))
    return rv
Example #11
0
def divisorCount(n):
	return multiply(e + 1 for _, e in primes.factors(n))
Example #12
0
def main(indices):
    return multiply(map(d, indices))
Example #13
0
def getNumber(factors):
    return multiply(b**e for b, e in factors)
Example #14
0
def main(n, k):
	d = digits(n)
	return max(multiply(d[i: i + k]) for i in range(len(d) - k))
Example #15
0
def rad(n):
    return multiply(p for p, _ in primes.factors(n))
Example #16
0
def main(indices):
	return multiply(map(d, indices))
Example #17
0
def main(limit, k):
    rad = lambda n: multiply(f for f, _ in primes.factors(n))
    return sorted(range(1, limit + 1), key=rad)[k - 1]
Example #18
0
def divisorCount(n):
    return multiply(e + 1 for _, e in primes.factors(n))
Example #19
0
def getNumber(factors):
	return multiply(b ** e for b, e in factors)
Example #20
0
def main(n):
	# trick: consider the maximum individual contribution of each prime factor
	maxContribution = lambda p: max(contribution(p, n) for n in range(2, n + 1))
	factors = takewhile(lambda k: k <= n, primes.gen())
	return multiply(map(maxContribution, factors))
Example #21
0
N="73167176531330624919225119674426574742355349194934"+\
"96983520312774506326239578318016984801869478851843"+\
"85861560789112949495459501737958331952853208805511"+\
"12540698747158523863050715693290963295227443043557"+\
"66896648950445244523161731856403098711121722383113"+\
"62229893423380308135336276614282806444486645238749"+\
"30358907296290491560440772390713810515859307960866"+\
"70172427121883998797908792274921901699720888093776"+\
"65727333001053367881220235421809751254540594752243"+\
"52584907711670556013604839586446706324415722155397"+\
"53697817977846174064955149290862569321978468622482"+\
"83972241375657056057490261407972968652414535100474"+\
"82166370484403199890008895243450658541227588666881"+\
"16427171479924442928230863465674813919123162824586"+\
"17866458359124566529476545682848912883142607690042"+\
"24219022671055626321111109370544217506941658960408"+\
"07198403850962455444362981230987879927244284909188"+\
"84580156166097919133875499200524063689912560717606"+\
"05886116467109405077541002256983155200055935729725"+\
"71636269561882670428252483600823257530420752963450"
nums=list(map(int, N))
L=13
max_index = 0
max_prod = 0
for i in range(0, len(N)-L):
  prod=lib.multiply(nums[i:i+L])
  if prod > max_prod:
    max_index = i
    max_prod = prod

print(max_prod)
Example #22
0
def countPrimeSets2(digits):
	rv = 0
	for groups in subsetCombinations(digits, filter=countPrimes):
		rv += multiply(map(countPrimes, groups))
	return rv
Example #23
0
def main(target):
	# trick: minimize f(n, m) = |countRec(n, m) - target| over the integers, easy since convex
	maxw = int(sqrt(0.25 + 2 * sqrt(target)) - 0.5)
	dims = ((w, round(sqrt(0.25 + 4 * target / (w * (w + 1))) - 0.5)) for w in range(1, maxw + 1))
	distToTarget = lambda d: abs(target - countRec(d[0], d[1]))
	return multiply(min(dims, key=distToTarget))
Example #24
0
# 9 10 11 12
# 13 14 15 16"""
table=[]
for line in S.split("\n"):
  parts = line.split(' ')
  nums = map(lambda x: int(x), parts)
  table.append(list(nums))

N_rows = len(table)
N_cols = len(table[0])

max_prod = 0
# horizontal
for i in range(N_rows):
  for j in range(N_cols - N + 1):
    prod = multiply(table[i][j:j+N])
    if prod > max_prod:
      max_prod = prod
#vertical
for i in range(N_cols):
  for j in range(N_rows - N + 1):
    prod = multiply([table[j+k][i] for k in range(N)])
    if prod > max_prod:
      max_prod = prod

#diag left to right
for i in range(N_rows - N + 1):
  for j in range(N_cols - N  + 1):
    prod = multiply([table[i+k][j+k] for k in range(N)])
    if prod > max_prod:
      max_prod = prod
Example #25
0
def rad(n):
  return multiply(p for p, _ in primes.factors(n))
Example #26
0
def main(limit, k):
	rad = lambda n: multiply(f for f, _ in primes.factors(n))
	return sorted(range(1, limit + 1), key=rad)[k - 1]
Example #27
0
	def test_multiply(self):
		self.assertEqual(multiply(4,5), 20)
		self.assertEqual(multiply(2,8), 16)