update some & clean eol
This commit is contained in:
parent
969812d51c
commit
30aba93f05
0
python/100.py
Executable file → Normal file
0
python/100.py
Executable file → Normal file
@ -1,30 +1,24 @@
|
||||
def poly(x, coef):
|
||||
out = 0
|
||||
for i in coef:
|
||||
out = out * x + i
|
||||
return out
|
||||
from functools import reduce
|
||||
|
||||
def poly(x, l):
|
||||
return reduce(lambda s, a: x * (s + a), l[:-1], 0) + l[-1]
|
||||
|
||||
un = (1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1)
|
||||
def gen_item(l):
|
||||
for i in range(1, len(l)):
|
||||
yield poly(i, l)
|
||||
|
||||
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
for i in xrange(len(a)):
|
||||
a[i] = poly(a[i], un)
|
||||
def gen_lagrange(n, k):
|
||||
lst = list(filter(lambda x: x != k, range(n)))
|
||||
tale = 1
|
||||
for x in lst:
|
||||
tale *= n - x
|
||||
for x in lst:
|
||||
tale //= k - x
|
||||
return tale
|
||||
|
||||
total = 0
|
||||
for length in xrange(2, len(a) + 1):
|
||||
y = a[:length]
|
||||
n = len(y)
|
||||
L = 0
|
||||
for k in xrange(n):
|
||||
l = 1
|
||||
for xi in xrange(n):
|
||||
if xi != k:
|
||||
l *= (n - xi)
|
||||
for xi in xrange(n):
|
||||
if xi != k:
|
||||
l /= (k - xi)
|
||||
L += (l * y[k])
|
||||
total += L
|
||||
def calc(l):
|
||||
lst = list(gen_item(l))
|
||||
for n in range(1, len(l)):
|
||||
yield sum(map(lambda x: x[0] * x[1], zip(lst, [gen_lagrange(n, k) for k in range(n)])))
|
||||
|
||||
print total + 1
|
||||
print(sum(calc([1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1])))
|
||||
|
@ -1,26 +1,17 @@
|
||||
def pl(p1, p2):
|
||||
return p1[0] * p2[1] - p1[1] * p2[0]
|
||||
|
||||
def isit(lis):
|
||||
if pl(lis[1], lis[0]) * pl(lis[2], lis[0]) > 0:
|
||||
return False
|
||||
if pl(lis[0], lis[1]) * pl(lis[2], lis[1]) > 0:
|
||||
return False
|
||||
if pl(lis[0], lis[2]) * pl(lis[1], lis[2]) > 0:
|
||||
return False
|
||||
return True
|
||||
def get_file(fn):
|
||||
for l in open(fn, 'r'):
|
||||
lst = list(map(lambda x: int(x), l.strip().split(',')))
|
||||
yield ((lst[0], lst[1]), (lst[2], lst[3]), (lst[4], lst[5]),
|
||||
(lst[0], lst[1]), (lst[2], lst[3]))
|
||||
|
||||
def breaknum(lis):
|
||||
return [[int(lis[0]), int(lis[1])], [int(lis[2]), int(lis[3])], [int(lis[4]), int(lis[5])]]
|
||||
def pro_multi(p, q):
|
||||
return p[0] * q[1] - p[1] * q[0]
|
||||
|
||||
def judge(l):
|
||||
for i in range((len(l) + 1) // 2):
|
||||
if pro_multi(l[i + 1], l[i]) * pro_multi(l[i + 2], l[i]) > 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
ff = open('../triangles.txt', 'r')
|
||||
points = ff.readlines()
|
||||
ff.close()
|
||||
|
||||
|
||||
count = 0
|
||||
for i in points:
|
||||
if isit(breaknum(i.split(','))):
|
||||
count += 1
|
||||
print count
|
||||
print(len(list(filter(judge, get_file('../resource/triangles.txt')))))
|
||||
|
@ -1,48 +1,23 @@
|
||||
unit = 1000000000
|
||||
bigunit = 1.0e+100
|
||||
import math
|
||||
|
||||
def musq(a, b):
|
||||
bi = []
|
||||
while b != 0:
|
||||
bi.append(b % 2)
|
||||
b /= 2
|
||||
bi.reverse()
|
||||
out = 1
|
||||
for p in bi:
|
||||
out = out * out
|
||||
if out > bigunit:
|
||||
out /= bigunit
|
||||
if p == 1:
|
||||
out *= a
|
||||
if out > bigunit:
|
||||
out /= bigunit
|
||||
return out
|
||||
def num_match(num):
|
||||
return sorted(str(num)) == sorted('123456789')
|
||||
|
||||
def fibnacci_mod():
|
||||
i, a, b = 0, 0, 1
|
||||
while True:
|
||||
i, a, b = i + 1, b % 1000000000, a + b
|
||||
yield i, a
|
||||
|
||||
def fib(x):
|
||||
sq5 = 5 ** 0.5
|
||||
oa = musq((1 + sq5) / 2, n)
|
||||
return oa / sq5
|
||||
def head_match(k):
|
||||
exp = k * math.log10((1 + 5 ** 0.5) / 2) - math.log10(5) / 2
|
||||
n = 10 ** (8 + exp % 1)
|
||||
return num_match(str(n)[:9])
|
||||
|
||||
n = 0
|
||||
a, b = 0, 1
|
||||
def search():
|
||||
for k, n in fibnacci_mod():
|
||||
if num_match(n):
|
||||
if head_match(k):
|
||||
return k, n
|
||||
|
||||
while 1:
|
||||
a, b = b, a + b
|
||||
a %= unit
|
||||
b %= unit
|
||||
n += 1
|
||||
bit = set('#'.join(str(a)).split('#'))
|
||||
if len(bit) == 9 and (not '0' in bit):
|
||||
try:
|
||||
pre = str(fib(n))
|
||||
if pre == 'inf':
|
||||
break
|
||||
except OverflowError:
|
||||
print n
|
||||
break
|
||||
pre = pre[0] + pre[2:]
|
||||
bitend = set('#'.join(pre[:9]).split('#'))
|
||||
if len(bitend) == 9 and (not '0' in bitend):
|
||||
print n, a, pre[:9]
|
||||
break
|
||||
print(search())
|
||||
|
68
python/61.py
68
python/61.py
@ -1,34 +1,34 @@
|
||||
|
||||
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()))
|
||||
|
||||
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()))
|
||||
|
42
python/64.py
42
python/64.py
@ -1,21 +1,21 @@
|
||||
|
||||
'''(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))
|
||||
|
||||
'''(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))
|
||||
|
26
python/65.py
26
python/65.py
@ -1,14 +1,14 @@
|
||||
|
||||
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]
|
||||
|
||||
|
||||
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)))))
|
54
python/69.py
54
python/69.py
@ -1,27 +1,27 @@
|
||||
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]))
|
||||
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]))
|
||||
|
64
python/70.py
64
python/70.py
@ -1,32 +1,32 @@
|
||||
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))
|
||||
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))
|
||||
|
0
python/92.py
Executable file → Normal file
0
python/92.py
Executable file → Normal file
151
python/98.py
151
python/98.py
@ -1,115 +1,54 @@
|
||||
ff = open('../words.txt', 'r')
|
||||
ww = ff.readline()
|
||||
ff.close()
|
||||
|
||||
longest = 0
|
||||
dic = {}
|
||||
lisw = ww.split(',')
|
||||
for i in lisw:
|
||||
i = i[1:-1]
|
||||
if len(i) > 2:
|
||||
tmp = '.'.join(i).split('.')
|
||||
tmp = ''.join(sorted(tmp))
|
||||
if tmp in dic.keys():
|
||||
dic.get(tmp).append(i)
|
||||
else:
|
||||
dic.update({tmp:[i]})
|
||||
def get_file(fn):
|
||||
with open(fn, 'r') as f:
|
||||
return list(map(lambda x: x[1:-1], f.read().split(',')))
|
||||
|
||||
def get_pair(lst):
|
||||
for i in range(len(lst) - 1):
|
||||
for j in range(i + 1, len(lst)):
|
||||
yield (lst[i], lst[j])
|
||||
|
||||
move = []
|
||||
def calc_rearray(pair):
|
||||
def do_iter(para):
|
||||
repeat = {}
|
||||
for ch in para[0]:
|
||||
map_index = para[1].find(ch, repeat.setdefault(ch, 0))
|
||||
yield map_index
|
||||
repeat[ch] = map_index + 1
|
||||
return tuple(do_iter(pair))
|
||||
|
||||
dicmove = {}
|
||||
def reverse_rearray(map_rearray):
|
||||
def do_iter(para):
|
||||
for i in sorted(para):
|
||||
yield para.index(i)
|
||||
return tuple(do_iter(map_rearray))
|
||||
|
||||
def howtomove(a, b):
|
||||
tmp = ''
|
||||
for i in a:
|
||||
tmp += str(b.index(i))
|
||||
if dicmove.has_key(tmp):
|
||||
dicmove.get(tmp).append([a, b])
|
||||
else:
|
||||
dicmove.update({tmp:[[a, b]]})
|
||||
return tmp
|
||||
def word_rearray(lst):
|
||||
pick = {}
|
||||
for item in lst:
|
||||
s_item = ''.join(sorted(item))
|
||||
pick.setdefault(s_item, []).append(item)
|
||||
result = {}
|
||||
while len(pick):
|
||||
key, value = pick.popitem()
|
||||
if len(value) > 1:
|
||||
for pair in get_pair(value):
|
||||
map_rearray = calc_rearray(pair)
|
||||
result.setdefault(map_rearray, []).append(pair)
|
||||
result.setdefault(reverse_rearray(map_rearray), []).append(tuple(reversed(pair)))
|
||||
return result
|
||||
|
||||
def howtolis(lis):
|
||||
out = set([])
|
||||
for i in lis:
|
||||
for j in lis:
|
||||
if i != j:
|
||||
tmp = howtomove(i, j)
|
||||
out.add(tmp)
|
||||
return list(out)
|
||||
def pow_range(digit):
|
||||
return int((10 ** (digit // 2)) * ((10 ** 0.5) ** (digit % 2)))
|
||||
|
||||
words = []
|
||||
def num_power(digit):
|
||||
return list(map(lambda x: str(x ** 2), range(pow_range(digit - 1), pow_range(digit) + 1)))
|
||||
|
||||
def num_rearray(digit):
|
||||
result = []
|
||||
for n in range(3, digit + 1):
|
||||
result.append(word_rearray(num_power(n)))
|
||||
return result
|
||||
|
||||
for i in dic.keys():
|
||||
if len(dic.get(i)) > 1:
|
||||
wordlen = len(i)
|
||||
while len(move) - 1 < wordlen:
|
||||
move.append([])
|
||||
words.append([])
|
||||
move[wordlen].extend(howtolis(dic.get(i)))
|
||||
words[wordlen].extend(dic.get(i))
|
||||
|
||||
#print words
|
||||
#print move
|
||||
|
||||
def movenum(num, mm):
|
||||
out = ''
|
||||
num = str(num)
|
||||
for i in xrange(len(mm)):
|
||||
try:
|
||||
out += num[int(mm[i])]
|
||||
except IndexError:
|
||||
return 3
|
||||
return int(out)
|
||||
|
||||
|
||||
def issq(x):
|
||||
tmp = int(x ** 0.5)
|
||||
if tmp ** 2 == x:
|
||||
return True
|
||||
return False
|
||||
|
||||
result = []
|
||||
length = len(move[-1][0])
|
||||
#print length
|
||||
n = int((10 ** length) ** 0.5)
|
||||
while length > 1 and n > 0:
|
||||
sq = n ** 2
|
||||
length = len(str(sq))
|
||||
while len(move[length]) <= 0:
|
||||
length -= 1
|
||||
n = int((10 ** length) ** 0.5)
|
||||
for moveit in move[length]:
|
||||
newsq = movenum(sq, moveit)
|
||||
if len(str(sq)) == len(str(newsq)):
|
||||
if issq(newsq):
|
||||
#print sq, newsq
|
||||
result.append([sq, dicmove.get(moveit)[0][0]])
|
||||
#if len(str(result[-1])) != len(str(result[0])):
|
||||
#break
|
||||
n -= 1
|
||||
|
||||
#print result
|
||||
|
||||
for item in result:
|
||||
nums = str(item[0])
|
||||
dicju1 = {}
|
||||
dicju2 = {}
|
||||
for i in xrange(len(item[1])):
|
||||
numm = nums[i]
|
||||
chh = item[1][i]
|
||||
if numm in dicju1.keys():
|
||||
if chh != dicju1.get(numm):
|
||||
break
|
||||
else:
|
||||
dicju1.update({numm:chh})
|
||||
if chh in dicju2.keys():
|
||||
if numm != dicju2.get(numm):
|
||||
break
|
||||
else:
|
||||
dicju2.update({chh:numm})
|
||||
else:
|
||||
print nums
|
||||
quit()
|
||||
print(sorted(word_rearray(get_file('../resource/words.txt')), key=lambda x: len(x)))
|
||||
#print(num_rearray(9))
|
||||
|
22
python/99.py
22
python/99.py
@ -1,16 +1,12 @@
|
||||
ff = open('../base_exp.txt', 'r')
|
||||
lis = ff.readlines()
|
||||
ff.close()
|
||||
import math
|
||||
|
||||
from math import log10
|
||||
def search(fn):
|
||||
lst = list(map(lambda x: (int(x[0]), int(x[1])),
|
||||
map(lambda x: x.strip().split(','),
|
||||
open('../resource/base_exp.txt', 'r').readlines()
|
||||
)
|
||||
))
|
||||
return lst.index(max(lst, key=lambda x: x[1] * math.log(x[0]))) + 1
|
||||
|
||||
maxx = [0, 0]
|
||||
for i in xrange(len(lis)):
|
||||
tmp = lis[i].split(',')
|
||||
base = int(tmp[0])
|
||||
power = int(tmp[1])
|
||||
loga = power * log10(base)
|
||||
if loga > maxx[0]:
|
||||
maxx = [loga, i + 1]
|
||||
|
||||
print maxx
|
||||
print(search('../resource/base_exp.txt'))
|
||||
|
Loading…
x
Reference in New Issue
Block a user