From e72ebe972deea6eb49e07c4a2b6947bec1f4b06a Mon Sep 17 00:00:00 2001 From: "xw_y_am@rmbp" Date: Mon, 11 Sep 2017 21:29:17 +0800 Subject: [PATCH] update some --- python/108.py | 80 ++++++++++++++++++++++---------------------------- python/109.py | 39 ++++++++---------------- python/110.py | 80 ++++++++++++++++++++++---------------------------- python/119.py | 59 ++++++++++++++++++++----------------- python/162.py | 41 +++++++++++++------------- python/88.1.py | 30 +++++++++++++++++++ python/88.py | 50 ------------------------------- 7 files changed, 166 insertions(+), 213 deletions(-) create mode 100644 python/88.1.py delete mode 100644 python/88.py diff --git a/python/108.py b/python/108.py index 1e743da..71d8434 100644 --- a/python/108.py +++ b/python/108.py @@ -1,49 +1,39 @@ -def mkp(n): - P = [2] - p = [2] - x = 3 - while len(P) < n: - for i in p: - if x % i == 0: - break - else: - P.append(x) - while x > p[-1]**2: - p.append(P[len(p)]) - x += 2 - return P +''' +n = p1^k1 * p2^k2 * ... * pn^kn +then the diffrent solve number is ((2k1+1)(2k2+1)...(2kn+1)) / 2 +''' +import math +from functools import reduce +from tools import number_theory -primes = mkp(1000) +def updateList(limit, lst): + lst[0] = int(limit / reduce(lambda x, y: x * (2 * y + 1), lst[1:], 1) - 0.5) + 1 + lst[0] = max(lst[:2]) -def factor(x): - out = [] - for p in primes: - if p * p > x: - break - if x % p == 0: - count = 0 - while x % p == 0: - x /= p - count += 1 - out.append(count) - if x > 1: - out.append(1) - return out +def genList(limit, size): + result = [] + base = [0] + [1] * (size - 1) + while True: + updateList(limit, base) + result.append(tuple(base)) + if len(set(base)) == 1: + break + for i in range(1, size): + if base[i] < base[0]: + base[:i + 1] = [base[i] + 1] * (i + 1) + break + return result +def search(limit): + size_max = int(math.log2(limit)) + prime = list(number_theory.make_prime(size_max * 2)) + mini = 2 ** limit + for size in range(2, size_max): + mini = min(mini, min( + map(lambda x: reduce(lambda x, y: x * (y[0] ** y[1]), + zip(prime, x), 1), + genList(limit, size) + ))) + return mini -def sum(x): - pre = factor(x) - out = 1 - for i in pre: - out *= i * 2 + 1 - return (out + 1) / 2 - - -def maxx(x): - n = 4 - while sum(n) < x: - n += 1 - return n - - -print maxx(1000) +print(search(1000)) diff --git a/python/109.py b/python/109.py index 53c87e4..5244ed0 100644 --- a/python/109.py +++ b/python/109.py @@ -1,30 +1,17 @@ -from sys import argv +def gen3(lst, d): + return [d + lst[i] + lst[j] for i in range(len(lst)) for j in range(i, len(lst))] -limit = int(argv[1]) +def gen2(lst, d): + return [d + item for item in lst] -kind = range(1, 21) -kind.extend(range(2, 41, 2)) -kind.extend(range(3, 61, 3)) -kind.extend([25, 50]) +def generate(match): + scoreDbl = list(range(2, 41, 2)) + [50] + scoreAll = list(range(1, 21)) + list(range(3, 61, 3)) + [25] + scoreDbl + mix = scoreDbl[:] + for last in scoreDbl: + mix += gen3(scoreAll, last) + mix += gen2(scoreAll, last) + return list(filter(match, mix)) -doub = range(2, 41, 2) -doub.extend([50]) - -count = 0 - -for i in doub: - if i <= limit: - count += 1 - for j in kind: - if i + j <= limit: - count += 1 - -tmp = 0 -for i in doub: - for j in xrange(len(kind)): - for k in xrange(j, len(kind)): - if i + kind[j] + kind[k] <= limit: - tmp += 1 - -print count + tmp +print(len(generate(lambda x: x < 100))) diff --git a/python/110.py b/python/110.py index 86651d0..ed07be7 100644 --- a/python/110.py +++ b/python/110.py @@ -1,49 +1,39 @@ -def mkp(n): - P = [2] - p = [2] - x = 3 - while len(P) < n: - for i in p: - if x % i == 0: - break - else: - P.append(x) - while x > p[-1]**2: - p.append(P[len(p)]) - x += 2 - return P +''' +n = p1^k1 * p2^k2 * ... * pn^kn +then the diffrent solve number is ((2k1+1)(2k2+1)...(2kn+1)) / 2 +''' +import math +from functools import reduce +from tools import number_theory -primes = mkp(1000) +def updateList(limit, lst): + lst[0] = int(limit / reduce(lambda x, y: x * (2 * y + 1), lst[1:], 1) - 0.5) + 1 + lst[0] = max(lst[:2]) -def factor(x): - out = [] - for p in primes: - if p * p > x: - break - if x % p == 0: - count = 0 - while x % p == 0: - x /= p - count += 1 - out.append(count) - if x > 1: - out.append(1) - return out +def genList(limit, size): + result = [] + base = [0] + [1] * (size - 1) + while True: + updateList(limit, base) + result.append(tuple(base)) + if len(set(base)) == 1: + break + for i in range(1, size): + if base[i] < base[0]: + base[:i + 1] = [base[i] + 1] * (i + 1) + break + return result +def search(limit): + size_max = int(math.log2(limit)) + prime = list(number_theory.make_prime(size_max * 2)) + mini = 2 ** limit + for size in range(2, size_max): + mini = min(mini, min( + map(lambda x: reduce(lambda x, y: x * (y[0] ** y[1]), + zip(prime, x), 1), + genList(limit, size) + ))) + return mini -def sum(x): - pre = factor(x) - out = 1 - for i in pre: - out *= i * 2 + 1 - return (out + 1) / 2 - - -def maxx(x): - n = 4 - while sum(n) < x: - n += 1 - return n - - -print maxx(100000) +print(search(4000000)) diff --git a/python/119.py b/python/119.py index 66cbf31..1337c19 100644 --- a/python/119.py +++ b/python/119.py @@ -1,29 +1,34 @@ -def ala(x): - tmp = 0 - while x != 0: - tmp += x % 10 - x /= 10 - return tmp +'''search order: -def ist(x, i): - if i == 1: - return False - if x < 10: - return False - while x != 1: - if x % i != 0: - return False - x /= i - return True +number 2**1 2**2 2**3 2**4 2**5 2**6 2**7 ... -n = 2 -count = [] -while n < 100: - for i in xrange(100): - tmp = n ** i - if ist(tmp, ala(tmp)): - if count.count(tmp) == 0: - count.append(tmp) - count.sort() - n += 1 -print count[29] +exp 2 / 2 / 2 / 2 / 2 / 2 + ---/ /----/ /----/ /----/ /----/ + 3 / 3 / 3 / 3 / 3 + ---/ /----/ /----/ /----/ + 4 / 4 / 4 / 4 + ---/ /----/ /----/ + 5 / 5 / 5 + ---/ /----/ + 6 / 6 + ---/ + 7 +''' + +def test(find, base, exp, flag): + num = base ** exp + if sum(map(lambda x: int(x), str(num))) == base: + find.append(num) + +def expand(limit): + find = [] + exp = 3 + while len(find) < limit: + for e in range(2, exp): + start = 2 ** (exp - e) + for num in range(start, 2 * start): + test(find, num, e, exp) + exp += 1 + return find + +print(expand(30)) diff --git a/python/162.py b/python/162.py index c2bfbba..277fe92 100644 --- a/python/162.py +++ b/python/162.py @@ -1,25 +1,26 @@ -def frag(x): - out = [] - for i in xrange(1, x - 1): - for j in xrange(1, x - i): - out.append((i, j, x - i - j)) - return out -def C(n, m): - total = 1 - for i in xrange(n, n - m, -1): - total *= i - for i in xrange(m, 1, -1): - total /= i - return total +from functools import reduce -total = 0 +def conbine(n, m): + return reduce(lambda x, y: x // y, range(m, 1, -1), + reduce(lambda x, y: x * y, range(n, n - m, -1), 1)) -for length in xrange(3, 17): - for same in xrange(3, length + 1): - for (x,y,z) in frag(same): - total += C(length - 1, x) * C(length - x, y) * C(length - - x - y, z) * 13 ** (length - same) -print '%X' % total +def gen_array(length): + for i in range(1, length - 1): + for j in range(1, length - i): + yield i, j, length - i - j +def calc(limit): + total = 0 + for length in range(3, limit + 1): + for same in range(3, length + 1): + total += sum(map(lambda x: + conbine(length - 1 , x[0]) * + conbine(length - x[0], x[1]) * + conbine(length - x[0] - x[1], x[2]) * + (limit - 3) ** (length - same), + gen_array(same) + )) + return hex(total).upper()[2:] +print(calc(16)) diff --git a/python/88.1.py b/python/88.1.py new file mode 100644 index 0000000..a8ab447 --- /dev/null +++ b/python/88.1.py @@ -0,0 +1,30 @@ + +from functools import reduce + +def calc(k): + eq = [] + not_one = 2 # how many numbers that != 1 + while True: + lst = [2] * not_one + while True: + diff = sum(lst) + (k - not_one) - reduce(lambda x, y: x * y, lst, 1) + if not diff: + eq.append((sum(lst) + k - not_one, lst)) + break + elif diff > 0: + lst[0] += 1 + continue + for i in range(1, len(lst)): + if lst[i] != lst[0]: + lst[:i + 1] = [lst[i] + 1] * (i + 1) + break + else: + break + not_one += 1 + if 2 ** not_one > 2 * not_one + k - not_one: + break + print(k, len(eq), min(eq)) + return min(eq) + +lll = list(map(lambda x: calc(x), range(2, 12))) +print(sum(map(lambda x: x[0], lll))) diff --git a/python/88.py b/python/88.py deleted file mode 100644 index 43ff93a..0000000 --- a/python/88.py +++ /dev/null @@ -1,50 +0,0 @@ - -from functools import reduce - -class Num: - n = 0 - l = [] - over = False - - def __init__(self, limit): - if limit < 2: - self.over = True - self.num = limit - self.l = [1] * limit - self.l[:2] = [2, 2] - - def multi(self): - return reduce(lambda x, y: x * y, self.l, 1) - - def add(self): - return sum(self.l) - - def is_overload(self): - return self.multi() > self.add() - - def inc(self): - if self.over: - return - if not self.is_overload(): - self.l[0] += 1 - else: - for i in range(len(self.l)): - if self.l[i + 1] < self.l[i]: - self.l[i + 1] += 1 - self.l[:i] = [self.l[i + 1]] * (i + 1) - if self.is_overload(): - self.over = True - - def eq(self): - return self.multi() == self.add() - - def get_min(self): - while not self.eq(): - self.inc() - if self.over: - return NULL - return self.add() - -for x in range(3, 20): - f = Num(x) - print(x, f.get_min())