update with eol lf
This commit is contained in:
parent
61ac578636
commit
83308d74e5
@ -1,29 +1,29 @@
|
||||
def ala(x):
|
||||
tmp = 0
|
||||
while x != 0:
|
||||
tmp += x % 10
|
||||
x /= 10
|
||||
return tmp
|
||||
|
||||
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
|
||||
|
||||
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]
|
||||
def ala(x):
|
||||
tmp = 0
|
||||
while x != 0:
|
||||
tmp += x % 10
|
||||
x /= 10
|
||||
return tmp
|
||||
|
||||
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
|
||||
|
||||
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]
|
||||
|
18
python/17.py
Normal file
18
python/17.py
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
def under_20():
|
||||
words = 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen'
|
||||
return len(words.replace(' ', ''))
|
||||
|
||||
def under_100():
|
||||
ten_words = 'twenty thirty forty fifty sixty seventy eighty ninety'
|
||||
digit_words = 'one two three four five six seven eight nine'
|
||||
return len(ten_words.replace(' ', '')) * 10 + len(digit_words.replace(' ', '')) * 8 + under_20()
|
||||
|
||||
def handreds():
|
||||
words = 'one hundred two hundred three hundred four hundred five hundred six hundred seven hundred eight hundred nine hundred'
|
||||
return len(words.replace(' ', '')) * 100 + len('and') * 99 * 9 + under_100() * 10
|
||||
|
||||
def count():
|
||||
return handreds() + len('onethousand')
|
||||
|
||||
print(count())
|
48
python/18.py
48
python/18.py
@ -1,24 +1,24 @@
|
||||
|
||||
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]
|
||||
|
||||
print(trace(max,[
|
||||
[75],
|
||||
[95, 64],
|
||||
[17, 47, 82],
|
||||
[18, 35, 87, 10],
|
||||
[20, 4, 82, 47, 65],
|
||||
[19, 1, 23, 75, 3, 34],
|
||||
[88, 2, 77, 73, 7, 63, 67],
|
||||
[99, 65, 4, 28, 6, 16, 70, 92],
|
||||
[41, 41, 26, 56, 83, 40, 80, 70, 33],
|
||||
[41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
|
||||
[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
|
||||
[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]]))
|
||||
|
||||
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]
|
||||
|
||||
print(trace(max,[
|
||||
[75],
|
||||
[95, 64],
|
||||
[17, 47, 82],
|
||||
[18, 35, 87, 10],
|
||||
[20, 4, 82, 47, 65],
|
||||
[19, 1, 23, 75, 3, 34],
|
||||
[88, 2, 77, 73, 7, 63, 67],
|
||||
[99, 65, 4, 28, 6, 16, 70, 92],
|
||||
[41, 41, 26, 56, 83, 40, 80, 70, 33],
|
||||
[41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
|
||||
[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
|
||||
[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]]))
|
||||
|
90
python/37.py
90
python/37.py
@ -1,45 +1,45 @@
|
||||
|
||||
from tools import number_theory
|
||||
|
||||
def shift(num):
|
||||
left = str(num)[:-1]
|
||||
while left:
|
||||
yield int(left)
|
||||
left = left[:-1]
|
||||
right = str(num)[1:]
|
||||
while right:
|
||||
yield int(right)
|
||||
right = right[1:]
|
||||
|
||||
def check_prime(x):
|
||||
x = str(x)
|
||||
if ('9' == x[0]) or ('9' == x[-1]):
|
||||
return False
|
||||
if ('1' == x[0]) or ('1' == x[-1]):
|
||||
return False
|
||||
if '5' in x[1:]:
|
||||
return False
|
||||
if '2' in x[1:]:
|
||||
return False
|
||||
if '0' in x:
|
||||
return False
|
||||
if '4' in x:
|
||||
return False
|
||||
if '6' in x:
|
||||
return False
|
||||
if '8' in x:
|
||||
return False
|
||||
return True
|
||||
|
||||
def search():
|
||||
find = []
|
||||
prime = list(number_theory.make_prime(1000000))
|
||||
for p in filter(lambda x: check_prime(x), prime):
|
||||
for s in shift(p):
|
||||
if s not in prime:
|
||||
break
|
||||
else:
|
||||
find.append(p)
|
||||
return find[4:]
|
||||
|
||||
print(sum(search()))
|
||||
|
||||
from tools import number_theory
|
||||
|
||||
def shift(num):
|
||||
left = str(num)[:-1]
|
||||
while left:
|
||||
yield int(left)
|
||||
left = left[:-1]
|
||||
right = str(num)[1:]
|
||||
while right:
|
||||
yield int(right)
|
||||
right = right[1:]
|
||||
|
||||
def check_prime(x):
|
||||
x = str(x)
|
||||
if ('9' == x[0]) or ('9' == x[-1]):
|
||||
return False
|
||||
if ('1' == x[0]) or ('1' == x[-1]):
|
||||
return False
|
||||
if '5' in x[1:]:
|
||||
return False
|
||||
if '2' in x[1:]:
|
||||
return False
|
||||
if '0' in x:
|
||||
return False
|
||||
if '4' in x:
|
||||
return False
|
||||
if '6' in x:
|
||||
return False
|
||||
if '8' in x:
|
||||
return False
|
||||
return True
|
||||
|
||||
def search():
|
||||
find = []
|
||||
prime = list(number_theory.make_prime(1000000))
|
||||
for p in filter(lambda x: check_prime(x), prime):
|
||||
for s in shift(p):
|
||||
if s not in prime:
|
||||
break
|
||||
else:
|
||||
find.append(p)
|
||||
return find[4:]
|
||||
|
||||
print(sum(search()))
|
||||
|
@ -13,6 +13,5 @@ def file_get():
|
||||
context = f.read().replace('"', '').split(',')
|
||||
return list(filter(lambda x: is_tri_num(x), map(lambda x: word_num(x), context)))
|
||||
|
||||
open('../resource/words')
|
||||
|
||||
print(len(file_get()))
|
||||
|
||||
|
35
python/44.1.py
Normal file
35
python/44.1.py
Normal file
@ -0,0 +1,35 @@
|
||||
import time
|
||||
|
||||
def sum_is_five(m, n):
|
||||
test = 3 *((3 * m - 1) * m + (3 * n - 1) * n)
|
||||
sqrt = int(test ** 0.5) + 1
|
||||
if sqrt * (sqrt - 1) == test:
|
||||
q, r = divmod(sqrt * 2, 6)
|
||||
if not r:
|
||||
return q
|
||||
return 0
|
||||
|
||||
def gen_five(start):
|
||||
five = (3 * start - 1) * start // 2
|
||||
while True:
|
||||
yield start, five
|
||||
five += 3 * start + 1
|
||||
start += 1
|
||||
|
||||
def search():
|
||||
stage = (0, 0, 0)
|
||||
for n, fn in gen_five(1):
|
||||
print(n, fn)
|
||||
for m, fm in gen_five(n + 1):
|
||||
if 3 * m + 1 > fn:
|
||||
break
|
||||
if 0 < stage[0] <= n:
|
||||
return stage
|
||||
x = sum_is_five(m, n)
|
||||
if x:
|
||||
if sum_is_five(m, x):
|
||||
return (n, m, x)
|
||||
if sum_is_five(n, x):
|
||||
stage = (m, n, x)
|
||||
|
||||
print(search())
|
40
python/44.py
40
python/44.py
@ -1,20 +1,20 @@
|
||||
def test(x):
|
||||
sq = 12 * x + 1
|
||||
ss = int(sq ** 0.5)
|
||||
if ss * ss != sq:
|
||||
return False
|
||||
if ss % 6 != 5:
|
||||
return False
|
||||
return True
|
||||
|
||||
def main(limit):
|
||||
for d in xrange(1, limit):
|
||||
for n in xrange(5, limit):
|
||||
m = n + d
|
||||
a = 3 * (m * m + n * n) - m - n
|
||||
b = d * (3 * (m + n) - 1)
|
||||
if test(a) and test(b):
|
||||
print a / 2, b / 2, m, n
|
||||
return
|
||||
|
||||
main(1300)
|
||||
|
||||
def test(x):
|
||||
sq = 12 * x + 1
|
||||
ss = int(sq ** 0.5)
|
||||
if ss * ss != sq:
|
||||
return False
|
||||
if ss % 6 != 5:
|
||||
return False
|
||||
return True
|
||||
|
||||
def main(limit):
|
||||
for d in range(1, limit):
|
||||
for n in range(5, limit):
|
||||
m = n + d
|
||||
a = 3 * (m * m + n * n) - m - n
|
||||
b = d * (3 * (m + n) - 1)
|
||||
if test(a) and test(b):
|
||||
return b // 2
|
||||
|
||||
print(main(1300))
|
||||
|
19
python/45.py
19
python/45.py
@ -1,8 +1,11 @@
|
||||
m = 166
|
||||
while 1:
|
||||
five = m * (3 * m - 1)
|
||||
n = int(five ** 0.5)
|
||||
if five == n * (n + 1) and n % 2 == 1:
|
||||
break
|
||||
m += 1
|
||||
print m, (n + 1) / 2, m * (3 * m - 1) / 2
|
||||
|
||||
def search(m):
|
||||
while True:
|
||||
five = m * (3 * m - 1)
|
||||
n = int(five ** 0.5)
|
||||
if n * (n + 1) == five and n % 2:
|
||||
break
|
||||
m += 1
|
||||
return m * (3 * m - 1) // 2
|
||||
|
||||
print(search(166))
|
||||
|
42
python/46.py
42
python/46.py
@ -1,21 +1,21 @@
|
||||
|
||||
from tools import number_theory
|
||||
import time
|
||||
time.clock()
|
||||
|
||||
def match(x, prime):
|
||||
for base in range(1, int(((x - 3) // 2) ** 0.5) + 1):
|
||||
if x - 2 * base * base in prime:
|
||||
return True
|
||||
return False
|
||||
|
||||
def search(limit):
|
||||
prime = list(number_theory.make_prime(limit))
|
||||
for x in range(21, limit, 2):
|
||||
if x in prime:
|
||||
continue
|
||||
if not match(x, prime):
|
||||
return x
|
||||
|
||||
print(search(10000))
|
||||
print(time.clock())
|
||||
|
||||
from tools import number_theory
|
||||
import time
|
||||
time.clock()
|
||||
|
||||
def match(x, prime):
|
||||
for base in range(1, int(((x - 3) // 2) ** 0.5) + 1):
|
||||
if x - 2 * base * base in prime:
|
||||
return True
|
||||
return False
|
||||
|
||||
def search(limit):
|
||||
prime = list(number_theory.make_prime(limit))
|
||||
for x in range(21, limit, 2):
|
||||
if x in prime:
|
||||
continue
|
||||
if not match(x, prime):
|
||||
return x
|
||||
|
||||
print(search(10000))
|
||||
print(time.clock())
|
||||
|
80
python/47.py
80
python/47.py
@ -1,40 +1,40 @@
|
||||
|
||||
from tools import number_theory
|
||||
|
||||
def num_factor(num):
|
||||
factor = []
|
||||
if not num % 2:
|
||||
factor.append(2)
|
||||
while not num % 2:
|
||||
num //= 2
|
||||
p = 3
|
||||
while p * p < num:
|
||||
if not num % p:
|
||||
factor.append(p)
|
||||
while not num % p:
|
||||
num //= p
|
||||
p += 2
|
||||
if 1 < num:
|
||||
factor.append(num)
|
||||
return factor
|
||||
|
||||
def find(count, a, b):
|
||||
seq = 0
|
||||
a += 1
|
||||
while b + seq >= count + a:
|
||||
if len(num_factor(a)) == count:
|
||||
seq += 1
|
||||
else:
|
||||
seq = 0
|
||||
if seq == count:
|
||||
return a - count + 1
|
||||
a += 1
|
||||
|
||||
def search(count, limit):
|
||||
prime = list(number_theory.make_prime(limit))
|
||||
for i in range(len(prime) - 1):
|
||||
get = find(count, prime[i], prime[i + 1])
|
||||
if get:
|
||||
return get
|
||||
|
||||
print(search(4, 200000))
|
||||
|
||||
from tools import number_theory
|
||||
|
||||
def num_factor(num):
|
||||
factor = []
|
||||
if not num % 2:
|
||||
factor.append(2)
|
||||
while not num % 2:
|
||||
num //= 2
|
||||
p = 3
|
||||
while p * p < num:
|
||||
if not num % p:
|
||||
factor.append(p)
|
||||
while not num % p:
|
||||
num //= p
|
||||
p += 2
|
||||
if 1 < num:
|
||||
factor.append(num)
|
||||
return factor
|
||||
|
||||
def find(count, a, b):
|
||||
seq = 0
|
||||
a += 1
|
||||
while b + seq >= count + a:
|
||||
if len(num_factor(a)) == count:
|
||||
seq += 1
|
||||
else:
|
||||
seq = 0
|
||||
if seq == count:
|
||||
return a - count + 1
|
||||
a += 1
|
||||
|
||||
def search(count, limit):
|
||||
prime = list(number_theory.make_prime(limit))
|
||||
for i in range(len(prime) - 1):
|
||||
get = find(count, prime[i], prime[i + 1])
|
||||
if get:
|
||||
return get
|
||||
|
||||
print(search(4, 200000))
|
||||
|
56
python/49.py
56
python/49.py
@ -1,48 +1,16 @@
|
||||
def alanum(x):
|
||||
lis = []
|
||||
while x != 0:
|
||||
lis.append(x % 10)
|
||||
x /= 10
|
||||
return lis
|
||||
|
||||
def pick(x, lis, out, a = 0):
|
||||
if x == 0:
|
||||
out.append([a, lis])
|
||||
return
|
||||
a *= 10
|
||||
for i in range(len(lis)):
|
||||
tmp = lis[:]
|
||||
tmpa = a + lis[i]
|
||||
tmp.pop(i)
|
||||
pick(x - 1, tmp, out, tmpa)
|
||||
from tools import number_theory
|
||||
|
||||
def isp(x):
|
||||
if x == 2:
|
||||
return True
|
||||
if x <= 1 or x & 1 == 0:
|
||||
return False
|
||||
for i in range(3, int(x ** 0.5) + 1, 2):
|
||||
if x % i == 0:
|
||||
return False
|
||||
return True
|
||||
def same(x, y):
|
||||
return str(sorted(str(x))) == str(sorted(str(y)))
|
||||
|
||||
def main():
|
||||
n = 1489
|
||||
while 1:
|
||||
if isp(n):
|
||||
tmp = alanum(n)
|
||||
ttmp = []
|
||||
pick(4, tmp, ttmp)
|
||||
tt = []
|
||||
for ti in ttmp:
|
||||
tt.append(ti[0])
|
||||
#print n, tmp, tt
|
||||
while tt.count(n) > 0:
|
||||
tt.remove(n)
|
||||
for ii in tt:
|
||||
jj = 2 * ii - n
|
||||
if jj > 0 and isp(ii) and isp(jj) and tt.count(jj):
|
||||
return str(n) + str(ii) + str(jj) #(n, ii, jj)
|
||||
n += 1
|
||||
def search(limit):
|
||||
prime = list(filter(lambda x: x > 1487, number_theory.make_prime(limit)))
|
||||
for i, pi in enumerate(prime):
|
||||
for pj in prime[i + 1:]:
|
||||
if same(pi, pj):
|
||||
pn = 2 * pj - pi
|
||||
if same(pn, pi) and pn in prime:
|
||||
return str(pi) + str(pj) + str(pn)
|
||||
|
||||
print(main())
|
||||
print(search(10000))
|
||||
|
30
python/52.py
30
python/52.py
@ -1,15 +1,15 @@
|
||||
|
||||
def multi_same(num, multi):
|
||||
s = set(str(num))
|
||||
for m in range(multi, 1, -1):
|
||||
if set(str(num * m)) != s:
|
||||
return False
|
||||
return True
|
||||
|
||||
def search(multi):
|
||||
num = 124847
|
||||
while not multi_same(num, multi):
|
||||
num += 1
|
||||
return num
|
||||
|
||||
print(search(6))
|
||||
|
||||
def multi_same(num, multi):
|
||||
s = set(str(num))
|
||||
for m in range(multi, 1, -1):
|
||||
if set(str(num * m)) != s:
|
||||
return False
|
||||
return True
|
||||
|
||||
def search(multi):
|
||||
num = 124847
|
||||
while not multi_same(num, multi):
|
||||
num += 1
|
||||
return num
|
||||
|
||||
print(search(6))
|
||||
|
110
python/54.py
110
python/54.py
@ -1,61 +1,49 @@
|
||||
#valuetab = ('High Card', 'One Pair', 'Two Pairs', 'Three of a Kind', 'Straight', 'Flush', 'Full House', 'Four of a Kind', 'Straight Flush', 'Royal Flush')
|
||||
|
||||
trans = {'A':14, 'T':10, 'J':11, 'Q':12, 'K':13}
|
||||
|
||||
def calc(x):
|
||||
color = []
|
||||
num = []
|
||||
for i in x:
|
||||
color.append(i[1])
|
||||
if '1' < i[0] <= '9':
|
||||
num.append(ord(i[0]) - ord('0'))
|
||||
else:
|
||||
num.append(trans.get(i[0]))
|
||||
num.sort()
|
||||
num.append(0)
|
||||
step = 1
|
||||
dic = {4:[], 3:[], 2:[], 1:[]}
|
||||
same = 1
|
||||
for i in range(1, len(num)):
|
||||
diff = num[i] - num[i - 1]
|
||||
if diff == 0:
|
||||
same += 1
|
||||
else:
|
||||
dic.get(same).append(num[i - 1])
|
||||
same = 1
|
||||
if diff == 1:
|
||||
step += 1
|
||||
if len(dic.get(4)):
|
||||
return (7, dic.get(4)[0], dic.get(1))
|
||||
if len(dic.get(3)) == len(dic.get(2)) == 1:
|
||||
return (6, dic.get(3)[0], dic.get(2)[0])
|
||||
flag = 0
|
||||
if len(set(color)) == 1:
|
||||
if step == 5:
|
||||
return (8, dic.get(1)[-2])
|
||||
flag = 5
|
||||
if step == 5:
|
||||
return (flag, 4, num[-2])
|
||||
if len(dic.get(3)):
|
||||
dic.get(1).reverse()
|
||||
return (flag, 3, dic.get(3)[0], dic.get(1))
|
||||
if len(dic.get(2)) > 1:
|
||||
dic.get(2).reverse()
|
||||
return (flag, 2, dic.get(2), dic.get(1))
|
||||
dic.get(1).reverse()
|
||||
if len(dic.get(2)):
|
||||
return (flag, 1, dic.get(2)[0], dic.get(1))
|
||||
return (flag, 0, dic.get(1))
|
||||
|
||||
def main():
|
||||
ff = open('../resource/poker.txt', 'r')
|
||||
out = 0
|
||||
for line in ff.readlines():
|
||||
strlis = line.split(' ')
|
||||
if calc(strlis[0:5]) > calc(strlis[5:]):
|
||||
out += 1
|
||||
ff.close()
|
||||
return out
|
||||
|
||||
#print calc(['7C','4C','4C','4C','7C'])
|
||||
print(main())
|
||||
|
||||
def is_order(num):
|
||||
mini = min(num)
|
||||
return list(range(mini, mini + len(num))) == list(num)
|
||||
|
||||
def anlz_num(num):
|
||||
anlz = {1: [], 2: [], 3: [], 4: []}
|
||||
for n in set(num):
|
||||
anlz[num.count(n)].append(n)
|
||||
if len(anlz[4]):
|
||||
return (8, max(anlz[4]))
|
||||
if len(anlz[3]):
|
||||
if len(anlz[2]):
|
||||
return (7, max(anlz[3]))
|
||||
else:
|
||||
return (4, max(anlz[3]))
|
||||
if len(anlz[2]) == 2:
|
||||
return (3, max(anlz[2]))
|
||||
elif len(anlz[2]):
|
||||
return (2, max(anlz[2]))
|
||||
return (1, max(anlz[1]))
|
||||
|
||||
def score(p):
|
||||
follow = set(p[1::3])
|
||||
number = list(sorted(map(lambda x: '23456789TJQKA'.find(x), p[0::3])))
|
||||
if is_order(number):
|
||||
if len(follow) == 1:
|
||||
return (9, max(number))
|
||||
else:
|
||||
return (5, max(number))
|
||||
if len(follow) == 1:
|
||||
return (6, max(number))
|
||||
return anlz_num(number)
|
||||
|
||||
def seek_file(fn):
|
||||
with open(fn, 'r') as f:
|
||||
l = f.readline()
|
||||
while l:
|
||||
yield l[:14], l[15:-1]
|
||||
l = f.readline()
|
||||
|
||||
def judge():
|
||||
count = 0
|
||||
for p, q in seek_file('../resource/poker.txt'):
|
||||
if score(p) > score(q):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
print(judge())
|
||||
|
26
python/59.py
26
python/59.py
@ -1,18 +1,12 @@
|
||||
from string import atoi
|
||||
|
||||
fi = open('../lib/cipher1.txt', 'r')
|
||||
st = fi.read()
|
||||
li = st.split(',')
|
||||
for i in xrange(len(li)):
|
||||
li[i] = atoi(li[i])
|
||||
def get_file(fn):
|
||||
with open(fn, 'r') as f:
|
||||
return list(map(lambda x: int(x), f.read().split(',')))
|
||||
|
||||
'''['g', 'o', 'd']'''
|
||||
de = [103, 111, 100]
|
||||
count = 0
|
||||
string = ''
|
||||
for i in xrange(len(li)):
|
||||
count += li[i] ^ de[i % 3]
|
||||
tmp = chr(li[i] ^ de[i % 3])
|
||||
string += tmp
|
||||
print string
|
||||
print count
|
||||
def xor_list(p, q):
|
||||
return list(map(lambda x: x[0] ^ x[1], zip(p * (len(q) // len(p) + 1), q)))
|
||||
|
||||
def decrypt():
|
||||
return sum(xor_list([103, 111, 100], get_file('../resource/cipher1.txt')))
|
||||
|
||||
print(decrypt())
|
||||
|
152
python/60.py
152
python/60.py
@ -1,76 +1,76 @@
|
||||
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 len(P) < 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
|
||||
|
||||
maxx = 1500
|
||||
prime = makep(maxx)
|
||||
prime.remove(2)
|
||||
prime.remove(5)
|
||||
|
||||
#print len(prime)
|
||||
|
||||
dic = {}
|
||||
for i in xrange(len(prime)):
|
||||
if i % (maxx / 10) == 0:
|
||||
print i
|
||||
tmp = set([])
|
||||
for j in xrange(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):
|
||||
tmp.add(prime[j])
|
||||
if len(tmp) > 0:
|
||||
dic.update({prime[i]: tmp})
|
||||
#print tmp
|
||||
#else:
|
||||
#print
|
||||
|
||||
print len(dic.keys())
|
||||
|
||||
out = []
|
||||
|
||||
for x1 in dic.keys():
|
||||
for x2 in dic.get(x1):
|
||||
if not x2 in dic.keys():
|
||||
continue
|
||||
both2 = dic.get(x1) & dic.get(x2)
|
||||
if len(both2) == 0:
|
||||
continue
|
||||
else:
|
||||
for x3 in both2:
|
||||
if not x3 in dic.keys():
|
||||
continue
|
||||
both3 = both2 & dic.get(x3)
|
||||
if len(both3) == 0:
|
||||
continue
|
||||
else:
|
||||
for x4 in both3:
|
||||
if not x4 in dic.keys():
|
||||
continue
|
||||
both4 = both3 & dic.get(x4)
|
||||
if len(both4) == 0:
|
||||
continue
|
||||
else:
|
||||
for x5 in both4:
|
||||
out.append([x1, x2, x3, x4, x5])
|
||||
|
||||
print sum(sorted(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 len(P) < 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
|
||||
|
||||
maxx = 1500
|
||||
prime = makep(maxx)
|
||||
prime.remove(2)
|
||||
prime.remove(5)
|
||||
|
||||
#print len(prime)
|
||||
|
||||
dic = {}
|
||||
for i in xrange(len(prime)):
|
||||
if i % (maxx / 10) == 0:
|
||||
print i
|
||||
tmp = set([])
|
||||
for j in xrange(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):
|
||||
tmp.add(prime[j])
|
||||
if len(tmp) > 0:
|
||||
dic.update({prime[i]: tmp})
|
||||
#print tmp
|
||||
#else:
|
||||
#print
|
||||
|
||||
print len(dic.keys())
|
||||
|
||||
out = []
|
||||
|
||||
for x1 in dic.keys():
|
||||
for x2 in dic.get(x1):
|
||||
if not x2 in dic.keys():
|
||||
continue
|
||||
both2 = dic.get(x1) & dic.get(x2)
|
||||
if len(both2) == 0:
|
||||
continue
|
||||
else:
|
||||
for x3 in both2:
|
||||
if not x3 in dic.keys():
|
||||
continue
|
||||
both3 = both2 & dic.get(x3)
|
||||
if len(both3) == 0:
|
||||
continue
|
||||
else:
|
||||
for x4 in both3:
|
||||
if not x4 in dic.keys():
|
||||
continue
|
||||
both4 = both3 & dic.get(x4)
|
||||
if len(both4) == 0:
|
||||
continue
|
||||
else:
|
||||
for x5 in both4:
|
||||
out.append([x1, x2, x3, x4, x5])
|
||||
|
||||
print sum(sorted(out))
|
||||
|
@ -18,7 +18,6 @@ def tt_ori(x, f, fd, flag):
|
||||
tmp = int(sqrt(flag * x))
|
||||
tmp_ = 0
|
||||
while tmp_ != tmp:
|
||||
#print '*', tmp
|
||||
tmp_ = tmp
|
||||
tmp -= (poly(tmp, f) - flag * x) / poly(tmp, fd)
|
||||
while poly(tmp, f) - flag * x > 0:
|
||||
|
76
python/62.py
76
python/62.py
@ -1,38 +1,38 @@
|
||||
def cuberoot(x):
|
||||
sqr = int(x ** 0.5)
|
||||
return rootiter(x, sqr)
|
||||
|
||||
def rootiter(x, pre):
|
||||
cur = pre - float(pre ** 3 - x) / (3 * pre ** 2)
|
||||
if abs(cur - pre) <= 0.001:
|
||||
return int(cur)
|
||||
else:
|
||||
return rootiter(x, cur)
|
||||
|
||||
def breaknum(x):
|
||||
out = []
|
||||
while x != 0:
|
||||
out.append(str(x % 10))
|
||||
x /= 10
|
||||
return out
|
||||
|
||||
def main():
|
||||
dic = {}
|
||||
i = 1
|
||||
while 1:
|
||||
tmplis = breaknum(i ** 3)
|
||||
tmplis.sort()
|
||||
tmp = ''.join(tmplis)
|
||||
if dic.has_key(tmp):
|
||||
dic.get(tmp).append(i)
|
||||
if len(dic.get(tmp)) == 5:
|
||||
return dic.get(tmp)
|
||||
else:
|
||||
dic.update({tmp:[i]})
|
||||
i += 1
|
||||
|
||||
|
||||
xx = main()
|
||||
print xx
|
||||
for i in xx:
|
||||
print i ** 3
|
||||
def cuberoot(x):
|
||||
sqr = int(x ** 0.5)
|
||||
return rootiter(x, sqr)
|
||||
|
||||
def rootiter(x, pre):
|
||||
cur = pre - float(pre ** 3 - x) / (3 * pre ** 2)
|
||||
if abs(cur - pre) <= 0.001:
|
||||
return int(cur)
|
||||
else:
|
||||
return rootiter(x, cur)
|
||||
|
||||
def breaknum(x):
|
||||
out = []
|
||||
while x != 0:
|
||||
out.append(str(x % 10))
|
||||
x /= 10
|
||||
return out
|
||||
|
||||
def main():
|
||||
dic = {}
|
||||
i = 1
|
||||
while 1:
|
||||
tmplis = breaknum(i ** 3)
|
||||
tmplis.sort()
|
||||
tmp = ''.join(tmplis)
|
||||
if dic.has_key(tmp):
|
||||
dic.get(tmp).append(i)
|
||||
if len(dic.get(tmp)) == 5:
|
||||
return dic.get(tmp)
|
||||
else:
|
||||
dic.update({tmp:[i]})
|
||||
i += 1
|
||||
|
||||
|
||||
xx = main()
|
||||
print xx
|
||||
for i in xx:
|
||||
print i ** 3
|
||||
|
18
python/63.py
18
python/63.py
@ -1,9 +1,9 @@
|
||||
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
|
||||
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
|
||||
|
58
python/64.py
58
python/64.py
@ -1,29 +1,29 @@
|
||||
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
|
||||
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
|
||||
|
50
python/65.py
50
python/65.py
@ -1,25 +1,25 @@
|
||||
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])
|
||||
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])
|
||||
|
106
python/66.py
106
python/66.py
@ -1,53 +1,53 @@
|
||||
def issq(x):
|
||||
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 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 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
|
||||
def issq(x):
|
||||
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 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 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
|
||||
|
86
python/67.py
86
python/67.py
@ -1,44 +1,44 @@
|
||||
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()
|
||||
|
||||
|
||||
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
|
||||
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()
|
||||
|
||||
|
||||
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
|
||||
'''
|
102
python/69.py
102
python/69.py
@ -1,51 +1,51 @@
|
||||
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
|
||||
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
|
||||
|
106
python/70.py
106
python/70.py
@ -1,54 +1,54 @@
|
||||
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
|
||||
|
||||
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
|
70
python/72.py
70
python/72.py
@ -1,35 +1,35 @@
|
||||
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)
|
||||
|
||||
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(1010)
|
||||
total = 0
|
||||
for i in xrange(2, 1000001):
|
||||
total += phi(i, prime)
|
||||
print total
|
||||
|
||||
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)
|
||||
|
||||
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(1010)
|
||||
total = 0
|
||||
for i in xrange(2, 1000001):
|
||||
total += phi(i, prime)
|
||||
print total
|
||||
|
||||
|
20
python/73.py
20
python/73.py
@ -1,10 +1,10 @@
|
||||
gcd = lambda x, y: y == 0 and x or gcd(y, x % y)
|
||||
|
||||
|
||||
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
|
||||
|
||||
print total
|
||||
gcd = lambda x, y: y == 0 and x or gcd(y, x % y)
|
||||
|
||||
|
||||
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
|
||||
|
||||
print total
|
||||
|
76
python/74.py
76
python/74.py
@ -1,38 +1,38 @@
|
||||
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)
|
||||
|
||||
|
||||
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])
|
||||
|
||||
print total
|
||||
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)
|
||||
|
||||
|
||||
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])
|
||||
|
||||
print total
|
||||
|
82
python/75.py
82
python/75.py
@ -1,41 +1,41 @@
|
||||
gcd_ori = lambda x, y: y == 0 and x or gcd_ori(y, x % y)
|
||||
|
||||
def gcd(lis):
|
||||
out = lis[0]
|
||||
for i in lis:
|
||||
out = gcd_ori(out, i)
|
||||
return out
|
||||
|
||||
def calc(i, j):
|
||||
return (i ** 2 - j ** 2, 2 * i * j, i ** 2 + j ** 2)
|
||||
|
||||
def make(x):
|
||||
out = []
|
||||
for i in xrange(2, int(x ** 0.5)):
|
||||
for j in xrange(1, i):
|
||||
c = calc(i, j)
|
||||
if gcd(c) == 1:
|
||||
out.append(sum(c))
|
||||
return out
|
||||
|
||||
|
||||
maxx = 1500000
|
||||
unit = make(maxx)
|
||||
out = set([])
|
||||
delete = set([])
|
||||
for i in unit:
|
||||
n = 1
|
||||
while 1:
|
||||
tmp = i * n
|
||||
if tmp > maxx:
|
||||
break
|
||||
if tmp in out:
|
||||
out.remove(tmp)
|
||||
delete.add(tmp)
|
||||
else:
|
||||
if not tmp in delete:
|
||||
out.add(tmp)
|
||||
n += 1
|
||||
|
||||
|
||||
print len(out)
|
||||
gcd_ori = lambda x, y: y == 0 and x or gcd_ori(y, x % y)
|
||||
|
||||
def gcd(lis):
|
||||
out = lis[0]
|
||||
for i in lis:
|
||||
out = gcd_ori(out, i)
|
||||
return out
|
||||
|
||||
def calc(i, j):
|
||||
return (i ** 2 - j ** 2, 2 * i * j, i ** 2 + j ** 2)
|
||||
|
||||
def make(x):
|
||||
out = []
|
||||
for i in xrange(2, int(x ** 0.5)):
|
||||
for j in xrange(1, i):
|
||||
c = calc(i, j)
|
||||
if gcd(c) == 1:
|
||||
out.append(sum(c))
|
||||
return out
|
||||
|
||||
|
||||
maxx = 1500000
|
||||
unit = make(maxx)
|
||||
out = set([])
|
||||
delete = set([])
|
||||
for i in unit:
|
||||
n = 1
|
||||
while 1:
|
||||
tmp = i * n
|
||||
if tmp > maxx:
|
||||
break
|
||||
if tmp in out:
|
||||
out.remove(tmp)
|
||||
delete.add(tmp)
|
||||
else:
|
||||
if not tmp in delete:
|
||||
out.add(tmp)
|
||||
n += 1
|
||||
|
||||
|
||||
print len(out)
|
||||
|
50
python/80.py
50
python/80.py
@ -1,25 +1,25 @@
|
||||
|
||||
def fiter(a, finit, fcntn, ftrns):
|
||||
x = finit(a)
|
||||
while fcntn(a, x):
|
||||
x = ftrns(a, x)
|
||||
return x
|
||||
|
||||
|
||||
def newton_root(a):
|
||||
return fiter(a,
|
||||
lambda x: x // 2,
|
||||
lambda p, x: not (0 < p - x ** 2 < 2 * x + 1),
|
||||
lambda p, x: (x + p // x) // 2)
|
||||
|
||||
|
||||
def all_root(limit, digit):
|
||||
root_sum = []
|
||||
digit -= 1
|
||||
for x in range(2, limit + 1):
|
||||
if int(x ** 0.5) ** 2 != x:
|
||||
root_sum.append(sum(map(lambda x: int(x), str(newton_root(100 ** digit * x)))))
|
||||
return root_sum
|
||||
|
||||
print(sum(all_root(100, 100)))
|
||||
|
||||
|
||||
def fiter(a, finit, fcntn, ftrns):
|
||||
x = finit(a)
|
||||
while fcntn(a, x):
|
||||
x = ftrns(a, x)
|
||||
return x
|
||||
|
||||
|
||||
def newton_root(a):
|
||||
return fiter(a,
|
||||
lambda x: x // 2,
|
||||
lambda p, x: not (0 < p - x ** 2 < 2 * x + 1),
|
||||
lambda p, x: (x + p // x) // 2)
|
||||
|
||||
|
||||
def all_root(limit, digit):
|
||||
root_sum = []
|
||||
digit -= 1
|
||||
for x in range(2, limit + 1):
|
||||
if int(x ** 0.5) ** 2 != x:
|
||||
root_sum.append(sum(map(lambda x: int(x), str(newton_root(100 ** digit * x)))))
|
||||
return root_sum
|
||||
|
||||
print(sum(all_root(100, 100)))
|
||||
|
||||
|
62
python/85.py
62
python/85.py
@ -1,31 +1,31 @@
|
||||
|
||||
def gen_tri(limit):
|
||||
tale = 1
|
||||
base = 1
|
||||
while tale < limit:
|
||||
yield (base, tale)
|
||||
base += 1
|
||||
tale += base
|
||||
|
||||
def tri_pair(num):
|
||||
seq = int(((8 * num - 1) ** 0.5 - 1) / 2)
|
||||
return ((seq , (seq + 1) * seq // 2),
|
||||
(seq + 1, (seq + 1) * (seq + 2) // 2))
|
||||
|
||||
def renew(result, limit, t_n, t_o):
|
||||
cmpr = t_n[1] * t_o[1]
|
||||
new_result = [max(limit, cmpr) - min(limit, cmpr), t_n[0] * t_o[0]]
|
||||
if new_result[0] < result[0]:
|
||||
return new_result
|
||||
else:
|
||||
return result
|
||||
|
||||
def search(limit):
|
||||
result = [limit, 0]
|
||||
for t_n in gen_tri(int(limit ** 0.5) + 1):
|
||||
t_l, t_r = tri_pair(limit // t_n[1])
|
||||
result = renew(result, limit, t_n, t_l)
|
||||
result = renew(result, limit, t_n, t_r)
|
||||
return result[1]
|
||||
|
||||
print(search(2000000))
|
||||
|
||||
def gen_tri(limit):
|
||||
tale = 1
|
||||
base = 1
|
||||
while tale < limit:
|
||||
yield (base, tale)
|
||||
base += 1
|
||||
tale += base
|
||||
|
||||
def tri_pair(num):
|
||||
seq = int(((8 * num - 1) ** 0.5 - 1) / 2)
|
||||
return ((seq , (seq + 1) * seq // 2),
|
||||
(seq + 1, (seq + 1) * (seq + 2) // 2))
|
||||
|
||||
def renew(result, limit, t_n, t_o):
|
||||
cmpr = t_n[1] * t_o[1]
|
||||
new_result = [max(limit, cmpr) - min(limit, cmpr), t_n[0] * t_o[0]]
|
||||
if new_result[0] < result[0]:
|
||||
return new_result
|
||||
else:
|
||||
return result
|
||||
|
||||
def search(limit):
|
||||
result = [limit, 0]
|
||||
for t_n in gen_tri(int(limit ** 0.5) + 1):
|
||||
t_l, t_r = tri_pair(limit // t_n[1])
|
||||
result = renew(result, limit, t_n, t_l)
|
||||
result = renew(result, limit, t_n, t_r)
|
||||
return result[1]
|
||||
|
||||
print(search(2000000))
|
||||
|
56
python/86.py
56
python/86.py
@ -1,28 +1,28 @@
|
||||
|
||||
import time
|
||||
|
||||
def is_sqrt(x, y):
|
||||
z = x ** 2 + y ** 2
|
||||
if int(z ** 0.5) ** 2 == z:
|
||||
if x > y:
|
||||
return x // 2 - x + y + 1
|
||||
else:
|
||||
return x // 2
|
||||
return 0
|
||||
|
||||
def count(m):
|
||||
count = 0
|
||||
for a in range(1, m * 2 + 1):
|
||||
count += is_sqrt(a, m)
|
||||
return count
|
||||
|
||||
def gen(limit):
|
||||
m = 1
|
||||
tale = 0
|
||||
while tale < limit:
|
||||
tale += count(m)
|
||||
m += 1
|
||||
return m - 1, tale
|
||||
|
||||
print(gen(1000000))
|
||||
print(time.process_time())
|
||||
|
||||
import time
|
||||
|
||||
def is_sqrt(x, y):
|
||||
z = x ** 2 + y ** 2
|
||||
if int(z ** 0.5) ** 2 == z:
|
||||
if x > y:
|
||||
return x // 2 - x + y + 1
|
||||
else:
|
||||
return x // 2
|
||||
return 0
|
||||
|
||||
def count(m):
|
||||
count = 0
|
||||
for a in range(1, m * 2 + 1):
|
||||
count += is_sqrt(a, m)
|
||||
return count
|
||||
|
||||
def gen(limit):
|
||||
m = 1
|
||||
tale = 0
|
||||
while tale < limit:
|
||||
tale += count(m)
|
||||
m += 1
|
||||
return m - 1, tale
|
||||
|
||||
print(gen(1000000))
|
||||
print(time.process_time())
|
||||
|
30
python/90.py
Normal file
30
python/90.py
Normal file
@ -0,0 +1,30 @@
|
||||
|
||||
def gen_iter(result, digit, pool, pick):
|
||||
if not digit:
|
||||
result.append(pick)
|
||||
else:
|
||||
for i in range(len(pool) - digit + 1):
|
||||
gen_iter(result, digit - 1, pool[i + 1:], pick + pool[i])
|
||||
|
||||
def gen_pick(digit, pool):
|
||||
result = []
|
||||
gen_iter(result, digit, pool, '')
|
||||
return result
|
||||
|
||||
def judge(state, c1, c2):
|
||||
for p1, p2 in state:
|
||||
if not ((p1 in c1 and p2 in c2) or (p2 in c1 and p1 in c2)):
|
||||
return False
|
||||
return True
|
||||
|
||||
def search():
|
||||
state = [('0', '1'), ('0', '4'), ('0', '6'), ('1', '6'), ('2', '5'), ('3', '6'), ('4', '6'), ('6', '4'), ('8', '1')]
|
||||
tale = []
|
||||
cube = gen_pick(6, '0123456786')
|
||||
for i in range(len(cube)):
|
||||
for j in range(i, len(cube)):
|
||||
if judge(state, cube[i], cube[j]):
|
||||
tale.append((cube[i], cube[j]))
|
||||
return len(tale)
|
||||
|
||||
print(search())
|
120
python/93.py
120
python/93.py
@ -1,60 +1,60 @@
|
||||
def two(a, b):
|
||||
out = set([])
|
||||
out.add(a + b)
|
||||
out.add(a * b)
|
||||
'''if a != b:
|
||||
out.add(abs(a - b))'''
|
||||
out.add(a - b)
|
||||
out.add(b - a)
|
||||
if a != 0:
|
||||
out.add(b / float(a))
|
||||
if b != 0:
|
||||
out.add(a / float(b))
|
||||
return out
|
||||
|
||||
def three(a, b, c):
|
||||
out = set([])
|
||||
for i in two(a, b):
|
||||
out |= two(i, c)
|
||||
for i in two(a, c):
|
||||
out |= two(i, b)
|
||||
for i in two(b, c):
|
||||
out |= two(i, a)
|
||||
return out
|
||||
|
||||
def four(a, b, c, d):
|
||||
first = two(a, b)
|
||||
second = two(c, d)
|
||||
out = set([])
|
||||
for i in first:
|
||||
out |= three(i, c, d)
|
||||
for j in second:
|
||||
out |= two(i, j)
|
||||
for j in second:
|
||||
out |= three(j, a, b)
|
||||
return out
|
||||
|
||||
def calc(a, b, c, d):
|
||||
tmp = set([])
|
||||
tmp |= four(a, b, c, d)
|
||||
tmp |= four(a, c, b, d)
|
||||
tmp |= four(a, d, b, c)
|
||||
out = []
|
||||
for i in tmp:
|
||||
if i == int(i) > 0:
|
||||
out.append(int(i))
|
||||
return sorted(out)
|
||||
|
||||
|
||||
maxx = [0, 0]
|
||||
for m in xrange(1, 10):
|
||||
for n in xrange(m + 1, 10):
|
||||
for s in xrange(n + 1, 10):
|
||||
for t in xrange(s + 1, 10):
|
||||
tmp = calc(m, n, s, t)
|
||||
for i in xrange(len(tmp)):
|
||||
if i + 1 != tmp[i]:
|
||||
break
|
||||
if i > maxx[0]:
|
||||
maxx = [i, (m, n, s, t)]
|
||||
print maxx
|
||||
def two(a, b):
|
||||
out = set([])
|
||||
out.add(a + b)
|
||||
out.add(a * b)
|
||||
'''if a != b:
|
||||
out.add(abs(a - b))'''
|
||||
out.add(a - b)
|
||||
out.add(b - a)
|
||||
if a != 0:
|
||||
out.add(b / float(a))
|
||||
if b != 0:
|
||||
out.add(a / float(b))
|
||||
return out
|
||||
|
||||
def three(a, b, c):
|
||||
out = set([])
|
||||
for i in two(a, b):
|
||||
out |= two(i, c)
|
||||
for i in two(a, c):
|
||||
out |= two(i, b)
|
||||
for i in two(b, c):
|
||||
out |= two(i, a)
|
||||
return out
|
||||
|
||||
def four(a, b, c, d):
|
||||
first = two(a, b)
|
||||
second = two(c, d)
|
||||
out = set([])
|
||||
for i in first:
|
||||
out |= three(i, c, d)
|
||||
for j in second:
|
||||
out |= two(i, j)
|
||||
for j in second:
|
||||
out |= three(j, a, b)
|
||||
return out
|
||||
|
||||
def calc(a, b, c, d):
|
||||
tmp = set([])
|
||||
tmp |= four(a, b, c, d)
|
||||
tmp |= four(a, c, b, d)
|
||||
tmp |= four(a, d, b, c)
|
||||
out = []
|
||||
for i in tmp:
|
||||
if i == int(i) > 0:
|
||||
out.append(int(i))
|
||||
return sorted(out)
|
||||
|
||||
|
||||
maxx = [0, 0]
|
||||
for m in xrange(1, 10):
|
||||
for n in xrange(m + 1, 10):
|
||||
for s in xrange(n + 1, 10):
|
||||
for t in xrange(s + 1, 10):
|
||||
tmp = calc(m, n, s, t)
|
||||
for i in xrange(len(tmp)):
|
||||
if i + 1 != tmp[i]:
|
||||
break
|
||||
if i > maxx[0]:
|
||||
maxx = [i, (m, n, s, t)]
|
||||
print maxx
|
||||
|
402
python/96.py
402
python/96.py
@ -1,201 +1,201 @@
|
||||
from copy import deepcopy
|
||||
|
||||
n = 3
|
||||
maxx = n ** 2
|
||||
allc = list(xrange(1, maxx + 1))
|
||||
for i in xrange(len(allc)):
|
||||
allc[i] = str(allc[i])
|
||||
|
||||
area = []
|
||||
for x in xrange(maxx):
|
||||
area.append([(x, i) for i in xrange(maxx)])
|
||||
for y in xrange(maxx):
|
||||
area.append([(i, y) for i in xrange(maxx)])
|
||||
for i in xrange(n):
|
||||
for j in xrange(n):
|
||||
area.append([(x, y) for x in xrange(n * i, n * i + n) for y in xrange(n * j, n * j + n)])
|
||||
|
||||
|
||||
|
||||
def psh(x):
|
||||
return [(x, i) for i in xrange(maxx)]
|
||||
|
||||
def psv(y):
|
||||
return [(i, y) for i in xrange(maxx)]
|
||||
|
||||
def psb(x, y):
|
||||
x0 = x // n * n
|
||||
y0 = y // n * n
|
||||
return [(i, j) for i in xrange(x0, x0 + n) for j in xrange(y0, y0 + n)]
|
||||
|
||||
def unit(x, y, flag = 4):
|
||||
out = set([])
|
||||
if flag == 1 or flag > 3:
|
||||
for i in xrange(maxx):
|
||||
out.add((x, i))
|
||||
if flag == 2 or flag > 3:
|
||||
for i in xrange(maxx):
|
||||
out.add((i, y))
|
||||
if flag == 3 or flag > 3:
|
||||
x0 = x // n * n
|
||||
y0 = y // n * n
|
||||
for i in xrange(x0, x0 + n):
|
||||
for j in xrange(y0, y0 + n):
|
||||
out.add((i, j))
|
||||
return list(out)
|
||||
|
||||
|
||||
def psudo(matrix):
|
||||
for i in xrange(maxx):
|
||||
for j in xrange(maxx):
|
||||
if j % n == 0:
|
||||
#print(' ', end='')
|
||||
print '',
|
||||
print matrix[i][j] + ' ',# end='')
|
||||
print('')
|
||||
if i % n == n - 1:
|
||||
print('')
|
||||
|
||||
|
||||
|
||||
def get(point, matrix):
|
||||
out = set([])
|
||||
for i in point:
|
||||
out.add(matrix[i[0]][i[1]])
|
||||
out.discard('0')
|
||||
return out
|
||||
|
||||
|
||||
def init(matrix):
|
||||
out = {}
|
||||
haveh = [get(psh(i), matrix) for i in xrange(maxx)]
|
||||
havev = [get(psv(i), matrix) for i in xrange(maxx)]
|
||||
haveb = [[get(psb(i * n, j * n), matrix) for j in xrange(n)] for i in xrange(n)]
|
||||
for i in xrange(maxx):
|
||||
for j in xrange(maxx):
|
||||
if matrix[i][j] == '0':
|
||||
tmp = set(allc)
|
||||
tmp -= haveh[i]
|
||||
tmp -= havev[j]
|
||||
tmp -= haveb[i // n][j // n]
|
||||
out.update({(i, j): tmp})
|
||||
return out
|
||||
|
||||
|
||||
def rmkey(k, dic, matrix, value = ''):
|
||||
tmp = list(dic[k])[0]
|
||||
if len(value) == 1:
|
||||
tmp = value
|
||||
dic.pop(k)
|
||||
matrix[k[0]][k[1]] = tmp
|
||||
for p in unit(k[0], k[1]):
|
||||
if p in dic.keys():
|
||||
dic[p].discard(tmp)
|
||||
|
||||
|
||||
def one(dic, matrix):
|
||||
while 1:
|
||||
for k in dic.keys():
|
||||
if len(dic[k]) == 1:
|
||||
rmkey(k, dic, matrix)
|
||||
break
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
def getd(dic):
|
||||
for i in sorted(dic):
|
||||
print i, dic[i]
|
||||
|
||||
|
||||
|
||||
def diff(dic, matrix):
|
||||
if len(dic.keys()) < 2:
|
||||
return
|
||||
for parea in area:
|
||||
pnow = parea[:]
|
||||
tt = []
|
||||
have = set([])
|
||||
for pp in parea:
|
||||
if pp in dic.keys():
|
||||
tt.extend(list(dic[pp]))
|
||||
have |= dic[pp]
|
||||
else:
|
||||
pnow.remove(pp)
|
||||
for i in have:
|
||||
if tt.count(i) == 1:
|
||||
for thek in pnow:
|
||||
try:
|
||||
if i in dic[thek]:
|
||||
rmkey(thek, dic, matrix, i)
|
||||
except:
|
||||
pass
|
||||
#print thek
|
||||
#getd(dic)
|
||||
#psudo(matrix)
|
||||
#continue
|
||||
|
||||
|
||||
def esay(dic, matrix, trytime = 20):
|
||||
limit = 0
|
||||
#out = ''.join(matrix[0][:n])
|
||||
while limit < trytime: #'0' in out and
|
||||
one(dic, matrix)
|
||||
diff(dic, matrix)
|
||||
#out = ''.join(matrix[0][:n])
|
||||
limit += 1
|
||||
#psudo(matrix)
|
||||
if limit < trytime:
|
||||
return True
|
||||
else:
|
||||
for test in dic.values():
|
||||
if len(test) < 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def solve(matrix, trytime = 10):
|
||||
dicc = init(matrix)
|
||||
out = ''.join(matrix[0][:n])
|
||||
limit = 0
|
||||
while '0' in out:
|
||||
limit += 1
|
||||
if limit > trytime:
|
||||
for p in dicc.keys():
|
||||
if len(dicc[p]) == 2:
|
||||
matrix_ = deepcopy(matrix)
|
||||
dicc_ = deepcopy(dicc)
|
||||
rmkey(p, dicc_, matrix_, list(dicc[p])[0])
|
||||
if esay(dicc_, matrix_):
|
||||
dicc = dicc_
|
||||
matrix = matrix_
|
||||
else:
|
||||
rmkey(p, dicc, matrix, list(dicc[p])[1])
|
||||
limit = 0
|
||||
break
|
||||
else:
|
||||
break
|
||||
if esay(dicc, matrix):
|
||||
out = ''.join(matrix[0][:n])
|
||||
limit += 1
|
||||
return int(out)
|
||||
|
||||
|
||||
def main():
|
||||
ff = open('sudoku.txt', 'r')
|
||||
nums = 0
|
||||
total = 0
|
||||
while nums < 50:
|
||||
ff.readline()
|
||||
aa = []
|
||||
nums += 1
|
||||
for i in xrange(maxx):
|
||||
aa.append('.'.join(ff.readline().strip()).split('.'))
|
||||
|
||||
tmp = solve(aa)
|
||||
print tmp
|
||||
total += tmp
|
||||
|
||||
print '***', total
|
||||
|
||||
main()
|
||||
from copy import deepcopy
|
||||
|
||||
n = 3
|
||||
maxx = n ** 2
|
||||
allc = list(xrange(1, maxx + 1))
|
||||
for i in xrange(len(allc)):
|
||||
allc[i] = str(allc[i])
|
||||
|
||||
area = []
|
||||
for x in xrange(maxx):
|
||||
area.append([(x, i) for i in xrange(maxx)])
|
||||
for y in xrange(maxx):
|
||||
area.append([(i, y) for i in xrange(maxx)])
|
||||
for i in xrange(n):
|
||||
for j in xrange(n):
|
||||
area.append([(x, y) for x in xrange(n * i, n * i + n) for y in xrange(n * j, n * j + n)])
|
||||
|
||||
|
||||
|
||||
def psh(x):
|
||||
return [(x, i) for i in xrange(maxx)]
|
||||
|
||||
def psv(y):
|
||||
return [(i, y) for i in xrange(maxx)]
|
||||
|
||||
def psb(x, y):
|
||||
x0 = x // n * n
|
||||
y0 = y // n * n
|
||||
return [(i, j) for i in xrange(x0, x0 + n) for j in xrange(y0, y0 + n)]
|
||||
|
||||
def unit(x, y, flag = 4):
|
||||
out = set([])
|
||||
if flag == 1 or flag > 3:
|
||||
for i in xrange(maxx):
|
||||
out.add((x, i))
|
||||
if flag == 2 or flag > 3:
|
||||
for i in xrange(maxx):
|
||||
out.add((i, y))
|
||||
if flag == 3 or flag > 3:
|
||||
x0 = x // n * n
|
||||
y0 = y // n * n
|
||||
for i in xrange(x0, x0 + n):
|
||||
for j in xrange(y0, y0 + n):
|
||||
out.add((i, j))
|
||||
return list(out)
|
||||
|
||||
|
||||
def psudo(matrix):
|
||||
for i in xrange(maxx):
|
||||
for j in xrange(maxx):
|
||||
if j % n == 0:
|
||||
#print(' ', end='')
|
||||
print '',
|
||||
print matrix[i][j] + ' ',# end='')
|
||||
print('')
|
||||
if i % n == n - 1:
|
||||
print('')
|
||||
|
||||
|
||||
|
||||
def get(point, matrix):
|
||||
out = set([])
|
||||
for i in point:
|
||||
out.add(matrix[i[0]][i[1]])
|
||||
out.discard('0')
|
||||
return out
|
||||
|
||||
|
||||
def init(matrix):
|
||||
out = {}
|
||||
haveh = [get(psh(i), matrix) for i in xrange(maxx)]
|
||||
havev = [get(psv(i), matrix) for i in xrange(maxx)]
|
||||
haveb = [[get(psb(i * n, j * n), matrix) for j in xrange(n)] for i in xrange(n)]
|
||||
for i in xrange(maxx):
|
||||
for j in xrange(maxx):
|
||||
if matrix[i][j] == '0':
|
||||
tmp = set(allc)
|
||||
tmp -= haveh[i]
|
||||
tmp -= havev[j]
|
||||
tmp -= haveb[i // n][j // n]
|
||||
out.update({(i, j): tmp})
|
||||
return out
|
||||
|
||||
|
||||
def rmkey(k, dic, matrix, value = ''):
|
||||
tmp = list(dic[k])[0]
|
||||
if len(value) == 1:
|
||||
tmp = value
|
||||
dic.pop(k)
|
||||
matrix[k[0]][k[1]] = tmp
|
||||
for p in unit(k[0], k[1]):
|
||||
if p in dic.keys():
|
||||
dic[p].discard(tmp)
|
||||
|
||||
|
||||
def one(dic, matrix):
|
||||
while 1:
|
||||
for k in dic.keys():
|
||||
if len(dic[k]) == 1:
|
||||
rmkey(k, dic, matrix)
|
||||
break
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
def getd(dic):
|
||||
for i in sorted(dic):
|
||||
print i, dic[i]
|
||||
|
||||
|
||||
|
||||
def diff(dic, matrix):
|
||||
if len(dic.keys()) < 2:
|
||||
return
|
||||
for parea in area:
|
||||
pnow = parea[:]
|
||||
tt = []
|
||||
have = set([])
|
||||
for pp in parea:
|
||||
if pp in dic.keys():
|
||||
tt.extend(list(dic[pp]))
|
||||
have |= dic[pp]
|
||||
else:
|
||||
pnow.remove(pp)
|
||||
for i in have:
|
||||
if tt.count(i) == 1:
|
||||
for thek in pnow:
|
||||
try:
|
||||
if i in dic[thek]:
|
||||
rmkey(thek, dic, matrix, i)
|
||||
except:
|
||||
pass
|
||||
#print thek
|
||||
#getd(dic)
|
||||
#psudo(matrix)
|
||||
#continue
|
||||
|
||||
|
||||
def esay(dic, matrix, trytime = 20):
|
||||
limit = 0
|
||||
#out = ''.join(matrix[0][:n])
|
||||
while limit < trytime: #'0' in out and
|
||||
one(dic, matrix)
|
||||
diff(dic, matrix)
|
||||
#out = ''.join(matrix[0][:n])
|
||||
limit += 1
|
||||
#psudo(matrix)
|
||||
if limit < trytime:
|
||||
return True
|
||||
else:
|
||||
for test in dic.values():
|
||||
if len(test) < 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def solve(matrix, trytime = 10):
|
||||
dicc = init(matrix)
|
||||
out = ''.join(matrix[0][:n])
|
||||
limit = 0
|
||||
while '0' in out:
|
||||
limit += 1
|
||||
if limit > trytime:
|
||||
for p in dicc.keys():
|
||||
if len(dicc[p]) == 2:
|
||||
matrix_ = deepcopy(matrix)
|
||||
dicc_ = deepcopy(dicc)
|
||||
rmkey(p, dicc_, matrix_, list(dicc[p])[0])
|
||||
if esay(dicc_, matrix_):
|
||||
dicc = dicc_
|
||||
matrix = matrix_
|
||||
else:
|
||||
rmkey(p, dicc, matrix, list(dicc[p])[1])
|
||||
limit = 0
|
||||
break
|
||||
else:
|
||||
break
|
||||
if esay(dicc, matrix):
|
||||
out = ''.join(matrix[0][:n])
|
||||
limit += 1
|
||||
return int(out)
|
||||
|
||||
|
||||
def main():
|
||||
ff = open('sudoku.txt', 'r')
|
||||
nums = 0
|
||||
total = 0
|
||||
while nums < 50:
|
||||
ff.readline()
|
||||
aa = []
|
||||
nums += 1
|
||||
for i in xrange(maxx):
|
||||
aa.append('.'.join(ff.readline().strip()).split('.'))
|
||||
|
||||
tmp = solve(aa)
|
||||
print tmp
|
||||
total += tmp
|
||||
|
||||
print '***', total
|
||||
|
||||
main()
|
||||
|
38
python/97.py
38
python/97.py
@ -1,19 +1,19 @@
|
||||
def powmod(x, y, z):
|
||||
ybi = []
|
||||
while y != 0:
|
||||
ybi.append(y % 2)
|
||||
y /= 2
|
||||
out = 1
|
||||
ybi.reverse()
|
||||
for i in ybi:
|
||||
out = out * out % z
|
||||
if i == 1:
|
||||
out = out * x % z
|
||||
return out
|
||||
|
||||
|
||||
a = powmod(2, 7830457, 100000000000)
|
||||
b = a * 28433 + 1
|
||||
b %= 10000000000
|
||||
|
||||
print b
|
||||
def powmod(x, y, z):
|
||||
ybi = []
|
||||
while y != 0:
|
||||
ybi.append(y % 2)
|
||||
y /= 2
|
||||
out = 1
|
||||
ybi.reverse()
|
||||
for i in ybi:
|
||||
out = out * out % z
|
||||
if i == 1:
|
||||
out = out * x % z
|
||||
return out
|
||||
|
||||
|
||||
a = powmod(2, 7830457, 100000000000)
|
||||
b = a * 28433 + 1
|
||||
b %= 10000000000
|
||||
|
||||
print b
|
||||
|
Loading…
x
Reference in New Issue
Block a user