From d19ee31562cd5f06caeb2233c85d43dc68b0b9eb Mon Sep 17 00:00:00 2001 From: "xw_y_am@rmbp" Date: Wed, 23 Aug 2017 17:57:34 +0800 Subject: [PATCH] update a lot --- python/18.py | 13 +++---- python/2.py | 4 +- python/32.py | 2 +- python/49.py | 2 +- python/51.py | 87 +++++++++++++---------------------------- python/60.py | 12 +++--- python/61.py | 107 ++++++++++++++++----------------------------------- python/63.py | 20 +++++----- python/64.py | 50 ++++++++++-------------- python/65.py | 39 +++++++------------ python/66.py | 75 ++++++++++++++---------------------- python/67.py | 52 ++++++------------------- python/69.py | 78 +++++++++++++------------------------ python/70.py | 86 +++++++++++++++-------------------------- python/72.py | 62 +++++++++++++++-------------- python/73.py | 13 +++---- python/74.py | 59 +++++++++++++--------------- python/76.py | 35 ++++++++--------- python/77.py | 60 +++++++++++------------------ python/78.py | 8 ++-- python/79.py | 14 +++++++ python/87.py | 50 +++++++++--------------- 22 files changed, 358 insertions(+), 570 deletions(-) create mode 100644 python/79.py diff --git a/python/18.py b/python/18.py index d661742..66a76e9 100644 --- a/python/18.py +++ b/python/18.py @@ -1,12 +1,11 @@ def trace(pick, triangle): - maxi = [0] * (len(triangle[-1]) + 1) - for line in reversed(triangle): - for i, value in enumerate(line): - maxi[i] = value + pick(maxi[i], maxi[i + 1]) - return maxi[0] + path = triangle.pop(0) + for line in triangle: + path = list(map(lambda x: pick(x[0], x[1]) + x[2], zip(path, path[1:], line))) + return path[0] -print(trace(max,[ +print(trace(max,list(reversed([ [75], [95, 64], [17, 47, 82], @@ -21,4 +20,4 @@ print(trace(max,[ [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31], -[ 4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]])) +[ 4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]])))) diff --git a/python/2.py b/python/2.py index f969f7e..d3e7b8e 100644 --- a/python/2.py +++ b/python/2.py @@ -6,12 +6,12 @@ def fibonacci(): a, b = b, a + b def target_sum(limit, match): - total = 0 + total = 0 for x in fibonacci(): if x >= limit: break if match(x): total += x - return total + return total print(target_sum(4000000, lambda x: not (x & 1))) diff --git a/python/32.py b/python/32.py index 703fe79..7518451 100644 --- a/python/32.py +++ b/python/32.py @@ -16,7 +16,7 @@ def add_type(a, b): for x, left in make_permutation(a, '123456789'): for y, cmpr in make_permutation(b, left): multi = int(x) * int(y) - if str(sorted(cmpr)) == str(sorted(str(multi))): + if sorted(cmpr) == sorted(str(multi)): s.add(multi) return sum(s) diff --git a/python/49.py b/python/49.py index 8eb289a..f7f951a 100644 --- a/python/49.py +++ b/python/49.py @@ -2,7 +2,7 @@ from tools import number_theory def same(x, y): - return str(sorted(str(x))) == str(sorted(str(y))) + return sorted(str(x)) == sorted(str(y)) def search(limit): prime = list(filter(lambda x: x > 1487, number_theory.make_prime(limit))) diff --git a/python/51.py b/python/51.py index 3a695c4..ebe450a 100644 --- a/python/51.py +++ b/python/51.py @@ -1,64 +1,29 @@ -''' -from string import maketrans, translate - -def numbreak(x): - out = [] - while x != 0: - out.append(x % 10) - x /= 10 - return set(out) - -def numloop(x, a, lis): - out = [] - for i in range(10 - a): - tt = maketrans(str(a), str(i + a)) - tmp = int(translate(str(x), tt)) - if isp(tmp, lis): - out.append(tmp) - return out - -def isp(x, lis): - for i in lis: - if x % i == 0: - return False - if x < i ** 2: - break - return True - -def makeP(x): - p = [2] - P = [2] - n = 3 - while n < x: - for i in p: - if n % i == 0: - break - else: - P.append(n) - n += 2 - while n > p[-1] ** 2: - p.append(P[len(p)]) - return P - -prime = makeP(100000) - -def main(): - xx = 56003 - while 1: - ss = numbreak(xx) - for syn in range(3): - if syn in ss: - tmp = numloop(xx, syn, prime) - if len(tmp) >= 8: - print(xx, tmp) - return - xx += 2 - while not isp(xx, prime): xx += 2 - -main() -''' from tools import number_theory -prime = list(number_theory.make_prime(1000)) -print(prime) +def trans_num(num): + s_num = str(num) + digit = set(s_num) + digit.remove(s_num[-1]) + for d in digit: + if d == s_num[0]: + yield [int(s_num.replace(d, ch)) for ch in '123456789' if ch != d] + else: + yield [int(s_num.replace(d, ch)) for ch in '0123456789' if ch != d] + +def is_prime(num): + for p in range(3, int(num ** 0.5) + 1): + if not num % p: + return False + return True + +def search(limit, count): + prime = list(filter(lambda x: x > 100000, number_theory.make_prime(limit))) + while len(prime): + p = prime.pop(0) + for l in trans_num(p): + pl = list(filter(is_prime, l)) + if count - 1 <= len(pl): + return [p] + pl + +print(min(search(200000, 8))) diff --git a/python/60.py b/python/60.py index e3826d6..4a45883 100644 --- a/python/60.py +++ b/python/60.py @@ -29,11 +29,11 @@ prime.remove(5) #print len(prime) dic = {} -for i in xrange(len(prime)): - if i % (maxx / 10) == 0: - print i +for i in range(len(prime)): + if i % (maxx // 10) == 0: + print(i) tmp = set([]) - for j in xrange(i + 1, len(prime)): + for j in range(i + 1, len(prime)): aa = int(str(prime[i]) + str(prime[j])) bb = int(str(prime[j]) + str(prime[i])) if isp(aa, prime) and isp(bb, prime): @@ -44,7 +44,7 @@ for i in xrange(len(prime)): #else: #print -print len(dic.keys()) +print(len(dic.keys())) out = [] @@ -73,4 +73,4 @@ for x1 in dic.keys(): for x5 in both4: out.append([x1, x2, x3, x4, x5]) -print sum(sorted(out)) +print(sum(sorted(out))) diff --git a/python/61.py b/python/61.py index e690532..e639656 100644 --- a/python/61.py +++ b/python/61.py @@ -1,73 +1,34 @@ -from math import sqrt, log10 - -formula = {3:((1,1,0),(2,1),2), - 5:((3,-1,0),(6,-1),2), - 6:((2,-1,0),(4,-1),1), - 7:((5,-3,0),(10,-3),2), - 8:((3,-2,0),(6,-2),1)} - - -def poly(x, coef): - out = 0 - for i in coef: - out = x * out + i - return out - - -def tt_ori(x, f, fd, flag): - tmp = int(sqrt(flag * x)) - tmp_ = 0 - while tmp_ != tmp: - tmp_ = tmp - tmp -= (poly(tmp, f) - flag * x) / poly(tmp, fd) - while poly(tmp, f) - flag * x > 0: - tmp -= 1 - return tmp + 1 - -def tt(x, num): - return tt_ori(x, formula.get(num)[0], formula.get(num)[1], formula.get(num)[2]) - -def test(x, num): - return poly(x, formula.get(num)[0]) / formula.get(num)[2] - -def sumri(num, n): - out = [] - tmp = tt(num, n) - while test(tmp, n) < num + 100: - out.append(tmp) - tmp += 1 - return out - - -def iter(num, lis, end, trap): - for i in lis: - trynum = sumri(num, i) - for j in trynum: - tmp = test(j, i) - if str(tmp)[2] == '0': - continue - if len(lis) == 1 and str(tmp)[2:] == end: - trap.append((tmp,j,i)) - total = 0 - for i in trap: - total += i[0] - print total - print trap - quit() - lis_ = lis[:] - lis_.remove(i) - trap.append((tmp, j,i)) - iter(tmp % 100 * 100, lis_, end, trap) - trap.pop(-1) - -def main(): - for i in xrange(32, 100): - tmp = i ** 2 - if str(tmp)[2] == '0': - continue - ch = str(tmp)[0:2] - iter(tmp % 100 * 100, [3,5,6,7,8], ch, [(tmp,i,4)]) - - -main() - + +def gen_num(start, limit, iter_func): + value = iter_func(start) + while value < limit: + yield value + start += 1 + value = iter_func(start) + +def init_nums(): + funcs = (lambda n: n * (n + 1) // 2, lambda n: n * n, lambda n: n * (3 * n - 1) // 2, + lambda n: n * (2 * n - 1), lambda n: n * (5 * n - 3) // 2, lambda n: n * (3 * n - 2)) + for f in funcs: + yield list(filter(lambda x: x > 1000, gen_num(1, 10000, f))) + +def find(platform, scan, result): + if not scan: + if result[-1] % 100 == result[0] // 100: + return result + else: + for shape in scan: + for num in platform[int(shape)]: + if num // 100 == result[-1] % 100: + get = find(platform, scan.replace(shape, ''), result + [num]) + if get: + return get + +def search(): + platform = list(init_nums()) + for first in platform[-1]: + result = find(platform[:-1], '01234', [first]) + if result: + return result + +print(sum(search())) diff --git a/python/63.py b/python/63.py index 3d81200..56a3a5f 100644 --- a/python/63.py +++ b/python/63.py @@ -1,9 +1,11 @@ -from math import log10 -total = [1] -for i in xrange(2, 10): - n = 1 - tmp = log10(i) - while int(n * tmp) + 1 == n: - total.append(i ** n) - n += 1 -print total +import math + +def gen(): + yield 1 + for i in range(2, 10): + n = 1 + while int(n * math.log10(i)) + 1 == n: + yield i ** n + n += 1 + +print(len(list(gen()))) diff --git a/python/64.py b/python/64.py index c0ff315..725fec1 100644 --- a/python/64.py +++ b/python/64.py @@ -1,29 +1,21 @@ -def div(sq, sub, quo): - return (sq ** 0.5 + sub) / quo - - -def cor(x): - a = 0 - b = 1 - v = 0 - out = [] - ab = [] - while 1: - v = int(div(x, a, b)) - tmp = b * v - a - b = (x - tmp ** 2) / b - a = tmp - ab.append((a,b)) - out.append(v) - num = ab.index((a,b)) + 1 - if num != len(ab): - return (out[:num], out[num:]) - - - -total = 0 -for i in xrange(2, 10001): - if int(i ** 0.5) ** 2 != i: - if len(cor(i)[1]) % 2: - total += 1 -print tota + +'''(sqrt(num) + a) / b''' + +def con_frac(num): + a, b = 0, 1 + para = [] + while b and (a, b) not in para: + d = int((num ** 0.5 + a) / b) + yield d + para.append((a, b)) + a = b * d - a + b = (num - a ** 2) // b + +def count(limit): + total = 0 + for x in range(2, limit + 1): + if not len(list(con_frac(x))) % 2: + total += 1 + return total + +print(count(10000)) diff --git a/python/65.py b/python/65.py index 39bb09c..d9e8c7b 100644 --- a/python/65.py +++ b/python/65.py @@ -1,25 +1,14 @@ -maxx = 99 - -a = [1] * maxx -for i in xrange(1, maxx, 3): - a[i] = ((i - 1) / 3 + 1) * 2 - -a.reverse() - - -x = [0, 1] -n = 0 -for i in a: - x[n % 2] += i * x[(n + 1) % 2] - n += 1 - -x[n % 2] += 2 * x[(n + 1) % 2] - -def sum(x): - out = 0 - while x != 0: - out += x %10 - x /= 10 - return out - -print sum(x[n % 2]) + +def con2frac(con_list): + a, b = 1, 0 + for r in reversed(con_list): + a, b = b + a * r, a + return a, b + +def exp_con(limit): + exp_list = [1] * limit + exp_list[0] = 2 + exp_list[2::3] = list(range(2, limit // 3 * 2 + 1, 2)) + return con2frac(exp_list)[0] + +print(sum(map(lambda x: int(x), str(exp_con(100))))) \ No newline at end of file diff --git a/python/66.py b/python/66.py index cf2bbd5..e6eb1a6 100644 --- a/python/66.py +++ b/python/66.py @@ -1,53 +1,34 @@ def issq(x): - sqr = int(x ** 0.5) - if sqr ** 2 == x: - return True - return False + sqr = int(x ** 0.5) + if sqr ** 2 == x: + return True + return False -def div(sq, sub, quo): - return (sq ** 0.5 + sub) / quo +def con_frac(num): + a, b = 0, 1 + para = [] + while b and (a, b) not in para: + d = int((num ** 0.5 + a) / b) + yield d + para.append((a, b)) + a = b * d - a + b = (num - a ** 2) // b +def con2frac(con_list): + a, b = 1, 0 + for r in reversed(con_list): + a, b = b + a * r, a + return a, b -def cor(x): - a = 0 - b = 1 - v = 0 - out = [] - ab = [] - while 1: - v = int(div(x, a, b)) - tmp = b * v - a - b = (x - tmp ** 2) / b - a = tmp - ab.append((a,b)) - out.append(v) - num = ab.index((a,b)) + 1 - if num != len(ab): - return out +def solve(d): + x, y = con2frac(list(con_frac(d))[:-1]) + if x ** 2 - d * y ** 2 == 1: + return (x, y) + else: + return (2 * x * y, x ** 2 + d * y ** 2) +def search(limit): + for num in range(2, limit + 1): + yield solve(num)[1], num -def pair(lis): - a = 1 - b = lis[0] - for i in lis[1:]: - a, b = b, b * i + a - return (a,b) - -def sol(x): - if issq(x): return (0, 0) - pp = pair(cor(x)[-2::-1]) - a = pp[0] - b = pp[1] - if b ** 2 - x * a ** 2 == 1: - return (a, b) - else: - return (2 * a * b, b ** 2 + x * a ** 2) - -maxx = [0, 0] - -for i in xrange(2,1001): - tmp = sol(i)[1] - if tmp > maxx[0]: - maxx = [tmp, i] - -print maxx +print(max(search(1000))[1]) diff --git a/python/67.py b/python/67.py index d6d5bfb..b93c1aa 100644 --- a/python/67.py +++ b/python/67.py @@ -1,44 +1,14 @@ -a = [] -ff = open('triangle.txt', 'r') -for i in ff.readlines(): - tmp = i.split(' ') - for j in xrange(len(tmp)): - tmp[j] = int(tmp[j]) - a.append(tmp) -ff.close() +def get_file(fn): + with open(fn, 'r') as f: + return list(map(lambda x: + list(map(lambda x: int(x), x.split(' '))), + f.read().strip().split('\n'))) +def shortest(pick, triangle): + path = triangle.pop(0) + for line in triangle: + path = list(map(lambda x: pick(x[0], x[1]) + x[2], zip(path, path[1:], line))) + return path -path = a[-1][:] -for i in xrange(len(a) - 2, 0, -1): - newpath = [] - for j in xrange(i + 1): - better = max(path[j], path[j + 1]) - newpath.append(a[i][j] + better) - path = newpath -print max(path) + a[0][0] - - -''' -path = [[a[0][0], [a[0][0]]]] -for i in xrange(1, len(a)): - newpath = [] - tmp = path[0][1][:] - tmp.append(a[i][0]) - newpath.append([path[0][0] + a[i][0], tmp]) - for j in xrange(1, i): - flag = (path[j - 1][0] > path[j][0]) and -1 or 0 - tmp = path[j + flag][1][:] - tmp.append(a[i][j]) - newpath.append([path[j + flag][0] + a[i][j], tmp]) - tmp = path[i - 1][1][:] - tmp.append(a[i][i]) - newpath.append([path[i - 1][0] + a[i][i], tmp]) - path = newpath - -maxx = [0, 0] -for i in path: - if i[0] > maxx[0]: - maxx = i -print maxx -''' \ No newline at end of file +print(shortest(max, list(reversed(get_file('../resource/triangle.txt'))))) diff --git a/python/69.py b/python/69.py index 4fb453a..2a91d4d 100644 --- a/python/69.py +++ b/python/69.py @@ -1,51 +1,27 @@ -def makeP(x): - p = [2] - P = [2] - n = 3 - while n < x: - for i in p: - if n % i == 0: - break - else: - P.append(n) - n += 2 - while p[-1] ** 2 < n: - p.append(P[len(p)]) - return P - -prime = makeP(1000) - -def factor(x): - dic = {} - for i in prime: - if i ** 2 > x: - break - if x % i == 0: - tmp = 0 - while x % i == 0: - tmp += 1 - x /= i - dic.update({i:tmp}) - if x != 1: - dic.update({x:1}) - return dic - - -def phi(x): - ff = factor(x) - out = 1 - for i in ff.keys(): - #print (i - 1) * i ** (ff.get(i) - 1) - out *= (i - 1) * i ** (ff.get(i) - 1) - return out - - - -maxx = [0, 0] -for i in xrange(2, 1000001): - if i % 100000 == 0: - print i - tmp = float(i) / phi(i) - if tmp > maxx[0]: - maxx = [tmp, i] -print maxx +from tools import number_theory +from functools import reduce + +def multi(l): + return reduce(lambda x, y: x * y, l, 1) + +def phi_rate(l): + return reduce(lambda x, y: x * y / (y - 1), l, 1) + +def find_longest(l, ) + +def calc(limit): + prime = list(number_theory.make_prime(int(limit ** 0.5) + 1)) + result = [] + for l in range(len(prime)): + if multi(prime[:l]) > limit: + l -= 1 + i = 0 + while True: + if multi(prime[i:i + l]) < limit: + result.append((phi_rate(prime[i:i + l]), prime[i:i + l])) + i += 1 + else: + break + return max(result) + +print(multi(calc(200000)[1])) diff --git a/python/70.py b/python/70.py index c868fe6..1792744 100644 --- a/python/70.py +++ b/python/70.py @@ -1,54 +1,32 @@ -maxx = 10000000 - -def mkp(x): - P = [2] - p = [2] - n = 3 - while n < x: - for i in p: - if n % i == 0: - break - else: - P.append(n) - n += 2 - while n > p[-1] ** 2: - p.append(P[len(p)]) - return P - -prime = mkp(2 * maxx ** 0.5) - -p = maxx ** 0.5 -pi = 0 -for i in xrange(len(prime)): - if prime[i] > p: - pi = i - break -q = maxx / p -qi = 0 - -nn, dndn = 0, 1 - -while pi > 1: - pi -= 1 - p = prime[pi] - q = maxx / p - for i in xrange(len(prime) - 1, pi, -1): - if prime[i] < q: - qi = i + 1 - break - while qi > pi: - qi -= 1 - q = prime[qi] - n = p * q - if n > maxx: - continue - dn = p + q - 1 - if n * dndn > nn * dn: - sn = str(n) - sdn = str(n - dn) - if sorted(sn) == sorted(sdn): - nn = n - dndn = dn - break - -print nn \ No newline at end of file +from tools import number_theory +from functools import reduce + +def update_factor(num, p, factor): + count = 0 + while not num % p: + num //= p + count += 1 + if count: + factor[p] = count + return num + +def calc_factor(num, prime): + factor = {} + for p in prime: + if p ** 2 > num: + break + num = update_factor(num, p, factor) + if num > 1: + factor[num] = 1 + return factor + +def phi(num, prime): + return reduce(lambda x, y: x * (y[0] - 1) * y[0] ** (y[1] - 1), calc_factor(num, prime).items(), 1) + +def search(limit): + prime = list(number_theory.make_prime(int(limit ** 0.5) + 1)) + for x in range(2, limit + 1): + if sorted(str(x)) == sorted(str(phi(x, prime))): + print(x, calc_factor(x, prime)) + +print(search(100000)) diff --git a/python/72.py b/python/72.py index feb4b15..6cb191f 100644 --- a/python/72.py +++ b/python/72.py @@ -1,35 +1,33 @@ -def phi(x, lis): - out = 1 - for p in lis: - if x % p == 0: - k = 0 - while x % p == 0: - k += 1 - x /= p - out *= p ** (k - 1) * (p - 1) - if x <= p ** 2: - if x == 1: - return out - return out * (x - 1) +from tools import number_theory +from functools import reduce +import time -def makep(x): - p = [2] - P = [2] - n = 3 - while n < x: - for i in p: - if n % i == 0: - break - else: - P.append(n) - n += 2 - while n > p[-1] ** 2: - p.append(P[len(p)]) - return P +def update_factor(num, p, factor): + count = 0 + while not num % p: + num //= p + count += 1 + if count: + factor[p] = count + return num -prime = makep(1010) -total = 0 -for i in xrange(2, 1000001): - total += phi(i, prime) -print total +def calc_factor(num, prime): + factor = {} + for p in prime: + if p ** 2 > num: + break + num = update_factor(num, p, factor) + if num > 1: + factor[num] = 1 + return factor +def phi(num, prime): + ret = reduce(lambda x, y: x * (y[0] - 1) * y[0] ** (y[1] - 1), calc_factor(num, prime).items(), 1) + return ret + +def count(limit): + prime = list(number_theory.make_prime(int(limit ** 0.5) + 1)) + return sum(map(lambda x: phi(x, prime), range(2, limit + 1))) + +print(count(1000000)) +print(time.process_time()) diff --git a/python/73.py b/python/73.py index 5cc22e6..81f7ef3 100644 --- a/python/73.py +++ b/python/73.py @@ -1,10 +1,9 @@ -gcd = lambda x, y: y == 0 and x or gcd(y, x % y) +from tools import number_theory +def deno_count(num): + return len(list(filter(lambda x: 1 == number_theory.gcd(num, x), range(num // 3 + 1, num // 2 + num % 2)))) -total = 0 -for i in xrange(12001): - for j in xrange(i / 3 + 1, i / 2 + i % 2): - if gcd(i, j) == 1: - total += 1 +def count(limit): + return sum(list(map(lambda x: deno_count(x), range(limit + 1)))) -print total +print(count(12000)) diff --git a/python/74.py b/python/74.py index 496bb26..384c2c0 100644 --- a/python/74.py +++ b/python/74.py @@ -1,38 +1,31 @@ -def mul(x): - out = 1 - while x != 1 and x != 0: - out *= x - x -= 1 - return out - -def next(x): - out = 0 - while x != 0: - out += mul(x % 10) - x /= 10 - return out -def make(x): - out = [x] - while 1: - tmp = next(out[-1]) - if out.count(tmp): - return out - out.append(tmp) +def fac_sum(num, fac_map): + tale = 0 + while num: + tale += fac_map[num % 10] + num //= 10 + return tale +def fac_update(fac_map, num, target, fac_list, frm_set = set()): + s_num = str(sorted(str(num))) + if s_num in frm_set: + fac_list.append(num) + return + loop = [] + shift = num + while shift not in loop: + loop.append(shift) + shift = fac_sum(shift, fac_map) + if len(loop) == target: + fac_list.append(num) + frm_set.add(s_num) -total = [] -bak = [] -for i in xrange(1, 2000):#1000001): - if bak.count(next(i)): - print i, bak - total.append([i, next(i)]) - continue - tmp = make(i) - if len(tmp) >= 60: - print i, tmp - total.append(tmp) - bak.append(tmp[1]) +def count(limit, target): + fac_map = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] + tale = [] + for x in range(limit): + fac_update(fac_map, x, target, tale) + return tale -print total +print(len(count(1000000, 60))) diff --git a/python/76.py b/python/76.py index 35d5de2..c0e30e0 100644 --- a/python/76.py +++ b/python/76.py @@ -1,21 +1,20 @@ -a = [0, 1, 2] -dic = {(2, 1): 1} -maxx = 100 -from sys import argv -if len(argv) > 0: - maxx = int(argv[1]) +def get(x, y, lst, dct): + if y < x: + return dct[(x, y)] + return lst[x] -def get(x, y): - if y >= x: - return a[x] - return dic.get((x, y)) +def count(num): + lst = [0, 1, 2] + dct = {(2, 1): 1} + if 3 > num: + return lst[num] + for x in range(3, num + 1): + tale = 0 + for n in range(1, x): + tale += get(x - n, n, lst, dct) + dct[(x, n)] = tale + lst.append(tale + 1) + return lst[num] - 1 -for n in xrange(3, maxx + 1): - tmp = 0 - for i in xrange(1, n): - tmp += get(n - i, i) - dic.update({(n, i): tmp}) - a.append(tmp + 1) - -print a[-1] - 1 +print(count(100)) diff --git a/python/77.py b/python/77.py index 4a1c631..fa727ca 100644 --- a/python/77.py +++ b/python/77.py @@ -1,40 +1,26 @@ -def mkp(x): - P = [2] - p = [2] - n = 3 - while n < x: - for i in p: - if n % i == 0: - break - else: - P.append(n) - n += 2 - while n > p[-1] ** 2: - p.append(P[len(p)]) - return P +from tools import number_theory -def get(x, y): - if x <= y: - return a[x] - return dic.get((x, y)) +def get(x, y, lst, dct): + if x > y: + return dct[(x, y)] + return lst[x] -maxx = 5000 +def count(limit): + prime = list(number_theory.make_prime(1000)) + lst = [0, 0, 1, 1, 1, 2] + dct = {(4, 3): 1, (4, 2): 1, (5, 3): 1, (5, 2): 0} + num = 5 + while lst[-1] <= limit: + num += 1 + tale = 0 + for p in prime: + if p > num: + break + tale += get(num - p, p, lst, dct) + dct[(num, p)] = tale + if num in prime: + tale += 1 + lst.append(tale) + return len(lst) - 1 -prime = mkp(10000) - -a = [0, 0, 1, 1, 1, 2] -dic = {(4, 3):1, (4, 2):1, (5, 3):1, (5, 2):0} -n = 5 -while a[-1] <= maxx: - n += 1 - tmp = 0 - for i in prime: - if i > n: - break - tmp += get(n - i, i) - #print n - i, i, get(n - i, i) - dic.update({(n, i):tmp}) - if n in prime: - tmp += 1 - a.append(tmp) -print len(a) - 1 \ No newline at end of file +print(count(5000)) diff --git a/python/78.py b/python/78.py index 2d6eb8b..e95df80 100644 --- a/python/78.py +++ b/python/78.py @@ -7,13 +7,13 @@ def get(x, y): return a[x] return dic.get((x, y)) -for n in xrange(3, maxx + 1): +for n in range(3, maxx + 1): tmp = 0 - for i in xrange(1, n): + for i in range(1, n): tmp += get(n - i, i) dic.update({(n, i): tmp}) a.append(tmp + 1) -for i in xrange(len(a)): +for i in range(len(a)): if a[i] % 10 == 0: - print i, a[i] + print(i, a[i]) diff --git a/python/79.py b/python/79.py new file mode 100644 index 0000000..b3b463b --- /dev/null +++ b/python/79.py @@ -0,0 +1,14 @@ + +def get_file(fn): + with open(fn, 'r') as f: + content = f.readlines() + return list(sorted(set(map(lambda x: x.strip()[:2], content)) | \ + set(map(lambda x: x.strip()[1:], content)) | \ + set(map(lambda x: x.strip()[::2], content)))) + +def resort(lst): + lst = [list(filter(lambda x: x[0] == c, lst)) for c in set(map(lambda x: x[0], lst))] + lst.sort(key=lambda x: len(x)) + return ''.join(list(reversed(list(map(lambda x: x[0][0], lst[1:]))))) + lst[0][0] + +print(resort(get_file('../resource/keylog.txt'))) diff --git a/python/87.py b/python/87.py index a9cd526..13de496 100644 --- a/python/87.py +++ b/python/87.py @@ -1,36 +1,22 @@ -def makep(x): - P = [2] - p = [2] - n = 3 - while x > n: - for i in p: - if n % i == 0: - break - else: - P.append(n) - n += 2 - while n > p[-1] ** 2: - p.append(P[len(p)]) - return P - -maxx = 50000000 - -prime = makep(int(maxx ** 0.5)) +from tools import number_theory def make(x, y, z): - return x ** 2 + y ** 3 + z ** 4 + return x ** 2 + y ** 3 + z ** 4 -out = set([]) +def count(limit): + result = set() + prime = list(number_theory.make_prime(int(limit ** 0.5) + 1)) + for i in prime: + for j in prime: + for k in prime: + trns = make(i, j, k) + if trns > limit: + break + result.add(trns) + if make(i, j, 2) > limit: + break + if make(i, 2, 2) > limit: + break + return result -for i in prime: - for j in prime: - for k in prime: - tmp = make(i, j, k) - if tmp > maxx: - break - out.add(tmp) - if make(i, j, 2) > maxx: - break - if make(i, 2, 2) > maxx: - break -print len(out) +print(len(count(50000000)))