update 89 93 94 97, add pow_mod
This commit is contained in:
parent
d19ee31562
commit
5198fce5b7
68
python/89.py
68
python/89.py
@ -1,42 +1,40 @@
|
||||
dic = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
|
||||
rdic = {1:'I', 2:'II', 3:'III', 4:'IV', 5:'V', 6:'VI', 7:'VII', 8:'VIII', 9:'IX',
|
||||
10:'X', 20:'XX', 30:'XXX', 40:'XL', 50:'L', 60:'LX', 70:'LXX', 80:'LXXX',
|
||||
90:'XC', 100:'C', 200:'CC', 300:'CCC', 400:'CD', 500:'D', 600:'DC',
|
||||
700:'DCC', 800:'DCCC', 900:'CM', 1000:'M'}
|
||||
from functools import reduce
|
||||
|
||||
def rtoi(ss):
|
||||
out = dic.get(ss[-1])
|
||||
for i in xrange(len(ss) - 1):
|
||||
if dic.get(ss[i]) < dic.get(ss[i + 1]):
|
||||
out -= dic.get(ss[i])
|
||||
else:
|
||||
out += dic.get(ss[i])
|
||||
return out
|
||||
dct_n2r = {0: '', 1:'I', 2:'II', 3:'III', 4:'IV', 5:'V', 6:'VI', 7:'VII', 8:'VIII', 9:'IX',
|
||||
10:'X', 20:'XX', 30:'XXX', 40:'XL', 50:'L', 60:'LX', 70:'LXX', 80:'LXXX', 90:'XC',
|
||||
100:'C', 200:'CC', 300:'CCC', 400:'CD', 500:'D', 600:'DC', 700:'DCC', 800:'DCCC', 900:'CM',
|
||||
1000:'M'}
|
||||
dct_r2n = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
|
||||
|
||||
def itor(num):
|
||||
out = ''
|
||||
thousand = num - num % 1000
|
||||
if num > 0:
|
||||
out += rdic.get(1000) * (thousand / 1000)
|
||||
def r2n(r_num):
|
||||
last = 0
|
||||
tale = 0
|
||||
for digit in reversed(list(map(lambda x: dct_r2n[x], r_num))):
|
||||
if digit < last:
|
||||
tale -= digit
|
||||
else:
|
||||
tale += digit
|
||||
last = digit
|
||||
return tale
|
||||
|
||||
def n2r(num):
|
||||
tale = dct_n2r[1000] * (num // 1000)
|
||||
num %= 1000
|
||||
hundred = num - num % 100
|
||||
if hundred > 0:
|
||||
out += rdic.get(hundred)
|
||||
tale += dct_n2r[num - num % 100]
|
||||
num %= 100
|
||||
teen = num - num % 10
|
||||
if teen > 0:
|
||||
out += rdic.get(teen)
|
||||
tale += dct_n2r[num - num % 10]
|
||||
num %= 10
|
||||
if num > 0:
|
||||
out += rdic.get(num)
|
||||
return out
|
||||
return tale + dct_n2r[num]
|
||||
|
||||
ff = open('../roman.txt', 'r')
|
||||
lis = ff.readlines()
|
||||
ff.close()
|
||||
def get_file(fn):
|
||||
with open(fn, 'r') as f:
|
||||
return list(map(lambda x: x.strip(), f.readlines()))
|
||||
|
||||
count = 0
|
||||
for i in lis:
|
||||
new = itor(rtoi(i.rstrip()))
|
||||
count += abs(len(new) - len(i.rstrip()))
|
||||
print count
|
||||
def item_len(lst):
|
||||
return reduce(lambda x, y: x + len(y), lst, 0)
|
||||
|
||||
def trans(fn):
|
||||
lst = get_file(fn)
|
||||
return item_len(lst) - item_len(map(lambda x: n2r(r2n(x)), lst))
|
||||
|
||||
print(trans('../resource/roman.txt'))
|
||||
|
92
python/93.py
92
python/93.py
@ -1,60 +1,50 @@
|
||||
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
|
||||
out = set([a + b, a * b, a - b, b - a])
|
||||
if a != 0:
|
||||
out.add(b / a)
|
||||
if b != 0:
|
||||
out.add(a / 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
|
||||
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
|
||||
pair = two(c, d)
|
||||
out = set([])
|
||||
for i in two(a, b):
|
||||
out |= three(i, c, d)
|
||||
for j in pair:
|
||||
out |= two(i, j)
|
||||
for j in pair:
|
||||
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)
|
||||
out = list(sorted(list(filter(lambda x: x == int(x) > 0,
|
||||
four(a, b, c, d) | four(a, c, b, d) | four(a, d, b, c)))))
|
||||
for i in range(len(out)):
|
||||
if i + 1 != out[i]:
|
||||
return out[:i]
|
||||
return 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 search():
|
||||
maxi = [0, 0]
|
||||
for m in range(1, 10):
|
||||
for n in range(m + 1, 10):
|
||||
for s in range(n + 1, 10):
|
||||
for t in range(s + 1, 10):
|
||||
count = len(calc(m, n, s, t))
|
||||
if count > maxi[0]:
|
||||
maxi = [count, (m, n, s, t)]
|
||||
return maxi
|
||||
|
||||
print(search())
|
||||
|
47
python/94.py
47
python/94.py
@ -1,31 +1,20 @@
|
||||
maxx = 1000000000
|
||||
|
||||
mul = 3 ** 0.5
|
||||
n = 0
|
||||
C = 0
|
||||
total = 0
|
||||
while 1:
|
||||
n += 1
|
||||
m = int((mul + 2) * n)
|
||||
m += 1 - (m + n) % 2
|
||||
C = 2 * (m + n) ** 2
|
||||
if C > maxx: break
|
||||
if abs((m - n) ** 2 - 2 * m * n) == 1:
|
||||
print 4 * m * n, m ** 2 + n ** 2
|
||||
total += C
|
||||
def calc(limit):
|
||||
return sum(map(lambda x:
|
||||
sum(map(x[0], filter(x[1], map(x[2], range(1, x[3]))))),
|
||||
[
|
||||
(
|
||||
lambda x: 2 * sum(x) ** 2,
|
||||
lambda x: 2 * x[0] * x[1] + 1 == (x[0] - x[1]) ** 2,
|
||||
lambda x: (int((2 + 3 ** 0.5) * x) + 1, x),
|
||||
int((limit / (2 + 3 ** 0.5) / 12) ** 0.5)
|
||||
),
|
||||
(
|
||||
lambda x: 4 * x[0],
|
||||
lambda x: 3 * x[1] + 1 == x[0],
|
||||
lambda x: (int((3 ** 0.5) * x + 1) ** 2, x ** 2),
|
||||
int((limit / 12) ** 0.5)
|
||||
)
|
||||
]))
|
||||
|
||||
n = 0
|
||||
C = 0
|
||||
while 1:
|
||||
n += 1
|
||||
m = int(mul * n)
|
||||
m += 1 - (m + n) % 2
|
||||
xx = m ** 2
|
||||
yy = n ** 2
|
||||
C = 4 * xx
|
||||
if C > maxx: break
|
||||
if abs(xx - 3 * yy) == 1:
|
||||
print 2 * (xx - yy), xx + yy
|
||||
total += C
|
||||
|
||||
print total
|
||||
print(calc(1000000000))
|
||||
|
20
python/97.py
20
python/97.py
@ -1,19 +1,3 @@
|
||||
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
|
||||
from tools import number_theory
|
||||
|
||||
|
||||
a = powmod(2, 7830457, 100000000000)
|
||||
b = a * 28433 + 1
|
||||
b %= 10000000000
|
||||
|
||||
print b
|
||||
print(str(number_theory.pow_mod(2, 7830457, 10000000000) * 28433 + 1)[-10:])
|
||||
|
@ -10,6 +10,15 @@ def lcm(x, y):
|
||||
return x * y // gcd(x, y)
|
||||
|
||||
|
||||
def pow_mod(base, exp, mod):
|
||||
result = 1
|
||||
for digit in bin(exp)[2:]:
|
||||
result = result ** 2 % mod
|
||||
if '1' == digit:
|
||||
result = result * base % mod
|
||||
return result
|
||||
|
||||
|
||||
def make_prime(limit):
|
||||
if limit < 5:
|
||||
if limit < 2: return
|
||||
|
Loading…
x
Reference in New Issue
Block a user