update with new python skill

This commit is contained in:
xw_y_am@rmbp 2017-08-15 21:18:39 +08:00
parent 0a01cc0ac8
commit 1b1c91e4e0
68 changed files with 1236 additions and 1537 deletions

View File

@ -1,25 +1,9 @@
# coding=utf-8
''' If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000. '''
def multi_in_num(limit, base):
count = limit // base
return (count + 1) * count * base // 2
def calc(n, a): def dual_multi_in_num(limit, a, b):
tmp = n / a return multi_in_num(limit, a) + multi_in_num(limit, b) - multi_in_num(limit, a * b)
return (tmp + 1) * tmp * a / 2
x = 3 print(dual_multi_in_num(1000 - 1, 3, 5))
y = 5
maxx = 1000
out = calc(maxx, x) + calc(maxx, y) - calc(maxx, x * y)
print out
'''
total = 0
for i in range(1000):
if i % 3 == 0 or i % 5 == 0: # 条件为能被 3 或 5 整除
total += i # 足条件的数字加入到 total 中
print total
'''

View File

@ -1,42 +1,3 @@
''' The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million. '''
def makeP(x): from tools import number_theory
P = [3] print(sum(number_theory.make_prime(2000000)))
p = [3]
n = 5
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
maxx = 2000000
maxxx = int(maxx ** 0.5)
prime = makeP(maxxx)
total = 2 + 3 + 5 + 7
for i in xrange(len(prime) - 1):
for x in xrange(prime[i] ** 2 + 2, prime[i + 1] ** 2, 2):
for p in prime[:i + 1]:
if x % p == 0:
break
else:
total += x
for x in xrange(prime[-1] ** 2 + 2, maxx, 2):
for p in prime:
if x % p == 0:
break
else:
total += x
print total

View File

@ -1,4 +1,34 @@
a = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
from functools import reduce
def get_block(matrix, x, y, size):
block = []
for i in range(x, x + size):
block += matrix[i][y : y + size]
return block
def calc_max(cmpr, l):
return max(cmpr, reduce(lambda x, y: x * y, l, 1))
def block_max(block, size):
maxi = 0
for i in range(0, size * size, size):
maxi = calc_max(maxi, block[i : i + size])
for i in range(size):
maxi = calc_max(maxi, block[i : size * size : size])
maxi = calc_max(maxi, block[0 : size * size : size + 1])
maxi = calc_max(maxi, block[size - 1 : size * size - 1: size - 1])
return maxi
def seek_matrix(size, matrix):
maxi = 0
for x in range(len(matrix) - size):
for y in range(len(matrix[0]) - size):
maxi = max(maxi, block_max(get_block(matrix, x, y, size), size))
return maxi
print(seek_matrix(4, [
[ 8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65], [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91], [52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
@ -12,34 +42,10 @@ a = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57], [16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58], [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40], [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
[04, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66], [ 4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69], [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36], [ 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16], [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54], [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]] [ 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
]))
def summ(x, y, flag):
vector = ((1, 0), (0, 1), (1, 1), (1, -1))
total = 1
for i in xrange(4):
total *= a[x][y]
x += vector[flag][0]
y += vector[flag][1]
return total
maxx = [0,]
for i in xrange(20 - 4):
for j in xrange(20 - 4):
for k in xrange(3):
tmp = summ(i, j, k)
if tmp > maxx[0]:
maxx = [tmp, i, j, k]
for j in xrange(3, 20):
tmp = summ(i, j, 3)
if tmp > maxx[0]:
maxx = [tmp, i, j, 3]
print maxx

View File

@ -1,30 +1,45 @@
''' The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors? '''
def play(stop): from functools import reduce
num = 3 from tools import number_theory
while 1:
num += 1 g_prime = list(number_theory.make_prime(1000000))
n = num * (num + 1) / 2
sqr = int(n ** 0.5) def find_a_factor(num, prime):
while prime:
p = prime.pop(0)
if not (num % p):
return p, prime
return 0, []
def factor_num(num):
global g_prime
for i, value in enumerate(g_prime):
if value ** 2 > num:
break
prime = g_prime[:i]
factors = {}
while True:
factor, prime = find_a_factor(num, prime)
if factor:
count = 0 count = 0
if sqr * sqr == n: count = 1 while not (num % factor):
for tmp in xrange(2, sqr): num //= factor
if n % tmp == 0:
count += 1 count += 1
if count >= stop // 2: factors[factor] = count
print n else:
return 1 if 1 != num:
factors[num] = 1
return factors
if __name__ == '__main__': def tri_num():
play(500) x = 2
while True:
yield x * (x + 1) // 2
x += 1
for x in tri_num():
factor_count = reduce(lambda x, y: x * (y + 1), factor_num(x).values(), 1)
if factor_count > 500:
print(x)
break

View File

@ -1,4 +1,5 @@
num = [
nums = [
37107287533902102798797998220837590246510135740250, 37107287533902102798797998220837590246510135740250,
46376937677490009712648124896970078050417018260538, 46376937677490009712648124896970078050417018260538,
74324986199524741059474233309513058123726617309629, 74324986199524741059474233309513058123726617309629,
@ -98,11 +99,6 @@ num = [
77158542502016545090413245809786882778948721859617, 77158542502016545090413245809786882778948721859617,
72107838435069186155435662884062257473692284509516, 72107838435069186155435662884062257473692284509516,
20849603980134001723930671666823555245252804609722, 20849603980134001723930671666823555245252804609722,
53503534226472524250874054075591789781264330331690,] 53503534226472524250874054075591789781264330331690]
total = 0 print(str(sum(nums))[:10])
for i in num:
total += i
#print total
print str(total)[:10]

View File

@ -1,49 +1,52 @@
def sq(x):
out = 0
while x != 1:
out += 1
if x % 2 == 1:
x = x * 3 + 1
else:
x /= 2
return out
maxx = [0, 0] class Step:
l = []
d = {}
limit = 0
def __init__(self, num):
self.limit = 5 * num
self.l = [0] * self.limit
self.l[0] = 1
def __getitem__(self, key):
if self.limit > key:
return self.l[key]
else:
return self.d.setdefault(key, 0)
def __setitem__(self, key, value):
if self.limit > key:
self.l[key] = value
else:
self.d[key] = value
def collatz_step(x, path):
global steps
if steps[x - 1]:
foot = steps[x - 1] + 1
while path:
steps[path.pop() - 1] = foot
foot += 1
return
else:
path.append(x)
if x % 2:
collatz_step(3 * x + 1, path)
else:
collatz_step(x // 2, path)
def collatz_max(num):
global steps
for x in range(1, num + 1):
if steps[x - 1]:
continue
collatz_step(x, [])
return steps.l.index(max(steps.l[:num])) + 1
limit = 1000000 limit = 1000000
steps = Step(limit)
for i in xrange(1, limit + 1): print(collatz_max(limit))
if i % 100000 == 0:
print i
tmp = sq(i)
if tmp > maxx[1]:
maxx[1] = tmp
maxx[0] = i
print maxx
'''
limit = 1000000
num = {1:1}
maxx = [0, 0]
tmp = []
for i in xrange(2, limit + 1):
x = i
while 1:
if num.has_key(x):
tmp.reverse()
bak = x
while len(tmp) > 0:
num.update({tmp[0]: num.get(bak) + 1})
bak = tmp[0]
tmp.pop(0)
break
else:
tmp.append(x)
if x % 2 == 0:
x /= 2
else:
x = 3 * x + 1
if num.get(i) > maxx[1]:
maxx = [i, num.get(i)]
print maxx
'''

View File

@ -1,9 +1,6 @@
''' Starting in the top left corner of a 2*2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20*20 grid? '''
total = 1 from functools import reduce
for i in xrange(40, 20, -1):
total *= i total = reduce(lambda x, y: x * y, range(40, 20, -1), 1)
for i in xrange(1, 21): total = reduce(lambda x, y: x // y, range(1, 21), total)
total /= i print(total)
print total

View File

@ -1,7 +1,4 @@
''' What is the sum of the digits of the number 2^1000 ? '''
stri = str(2 ** 1000) from functools import reduce
total = 0
for i in xrange(len(stri)): print(reduce(lambda x, y: x + int(y), str(2 ** 1000), 0))
total += ord(stri[i]) - ord('0')
print total

View File

@ -1,66 +0,0 @@
dic = {
0:'',
1:'one',
2:'two',
3:'three',
4:'four',
5:'five',
6:'six',
7:'seven',
8:'eight',
9:'nine',
10:'ten',
11:'eleven',
12:'twelve',
13:'thirteen',
14:'fourteen',
15:'fifteen',
16:'sixteen',
17:'seventeen',
18:'eighteen',
19:'nineteen',
20:'twenty',
30:'thirty',
40:'forty',
50:'fifty',
60:'sixty',
70:'seventy',
80:'eighty',
90:'nithty',
100:'hundred',
1000:'thousand'
}
def analyse(x, p = 0):
if p: print x,
if x == 1000:
if p: print dic.get(x)
return len(dic.get(x))
out = 0
tmp = x / 100
if tmp:
out += len(dic.get(tmp))
if p: print dic.get(tmp),
out += len(dic.get(100))
if p: print dic.get(100),
if x % 100 == 0:
if p: print
return out
out += 3
if p: print 'and',
tmp = x % 100
if tmp < 20:
out += len(dic.get(tmp))
if p: print dic.get(tmp)
return out
out += len(dic.get(tmp / 10 * 10))
if p: print dic.get(tmp / 10 * 10),
out += len(dic.get(tmp % 10))
if p: print dic.get(tmp % 10)
return out
total = 0
for i in xrange(1, 1001):
total += analyse(i)
print total

View File

@ -1,4 +1,13 @@
a = [[75],
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], [95, 64],
[17, 47, 82], [17, 47, 82],
[18, 35, 87, 10], [18, 35, 87, 10],
@ -12,39 +21,4 @@ a = [[75],
[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], [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], [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], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
[4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]] [ 4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]]))
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
'''

View File

@ -1,33 +1,18 @@
month = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
weekday = [5]
month = (31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30)
def testyear(x): def leap_day(year):
if x % 400 == 0: if not (year % 400):
return True return 1
if x % 100 != 0 and x % 4 == 0: if (year % 100) and (not (year % 4)):
return True return 1
return False return 0
for y in range(1900, 2001):
for m in month:
weekday.append((weekday[-1] + m) % 7)
if 28 == m:
weekday[-1] = (weekday[-1] + leap_day(y)) % 7
pair = [[1900, 1], 1] print(weekday[13:].count(0))
def next():
pair[1] += month[pair[0][1]]
if testyear(pair[0][0]) and pair[0][1] == 2:
pair[1] += 1
pair[1] %= 7
pair[0][1] += 1
if pair[0][1] > 12:
pair[0][1] = 1
pair[0][0] += 1
total = 0
for i in xrange(12):
next()
while pair[0][0] < 2001:
if pair[1] == 0:
total += 1
next()
print total

View File

@ -1,24 +1,17 @@
# coding=utf-8
''' Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: def fibonacci():
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... a, b = 1, 1
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. ''' while True:
yield a
a, b = b, a + b
fib = [1, 1, 0] # 设置 fib 数列循环的数组 def target_sum(limit, match):
i = 1 # fib 数列的项计数器 total = 0
total = 0 # 满足条件的数的和 for x in fibonacci():
while fib[i] <= 4000000: # fib 数列小于要求值时不断循环 if x >= limit:
if fib[i] % 2 == 0: break
print fib[i] if match(x):
total += fib[i] # 满足条件的项计入总和 total += x
i = (i + 1) % 3 # 项计数器 return total
fib[i] = fib[(i + 1) % 3] + fib[(i + 2) % 3] #
print total #
a, b = 2, 8
total = 0
while a < 4000000:
total += a
a, b = b, a + b * 4
print total
print(target_sum(4000000, lambda x: not (x & 1)))

View File

@ -1,13 +1,9 @@
''' n! means n * (n - 1) * ... * 3 * 2 * 1
For example, 10! = 10 * 9 * ... * 3 * 2 * 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100! '''
a = 1 from functools import reduce
sum = 0
for i in xrange(1, 101): def factorial(n):
a *= i if 1 == n:
while a > 0: return 1
sum += a % 10 return n * factorial(n - 1)
a /= 10
print sum print(reduce(lambda x, y: x + int(y), str(factorial(100)), 0))

View File

@ -1,19 +1,23 @@
''' Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000. '''
import math def all_factor(num):
factors = [1]
sqrt = int(num ** 0.5)
if sqrt ** 2 == num:
factors.append(sqrt)
sqrt -= 1
for x in range(2, sqrt + 1):
if not (num % x):
factors += [x, num // x]
return factors
def ami(x): def count_amicable(begin, end):
test = 1 total = 0
sqr = int(math.ceil(math.sqrt(x))) while begin < end:
if sqr * sqr == x: test += sqr factor_sum = sum(all_factor(begin))
for i in xrange(2, sqr + 1): if (sum(all_factor(factor_sum)) == begin) and (factor_sum != begin):
if x % i == 0: test += i + x / i total += begin + factor_sum
return test begin = factor_sum
begin += 1
return total
for j in xrange(2, 10000): print(count_amicable(200, 10000))
tmp = ami(j)
if j == ami(tmp) and j != tmp:
print j, '\t', tmp

View File

@ -1,21 +1,11 @@
''' Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 * 53 = 49714.
What is the total of all the name scores in the file? '''
def namescore(nn): from functools import reduce
mark = 0
for i in nn:
mark += ord(i) - ord('A') + 1
return mark
def get_file():
names = open('../resource/names.txt', 'r').read().split(',')
return sorted(names)
filein = open('names.txt', 'r') def name_score(name):
names = filein.read().split(',') return reduce(lambda x, y: x + ord(y) - ord('A') + 1, name, 0)
names.sort()
test = names[:]
print(reduce(lambda x, y: x + (y[0] + 1) * y[1], enumerate(map(lambda x: name_score(x), get_file())), 0))
for xx in xrange(len(names)):
test[xx] = (xx + 1) * namescore(names[xx])
print sum(test)

View File

@ -1,49 +1,31 @@
# coding=utf-8
''' A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. '''
_limit = 20161 def all_factor(num):
factors = [1]
sqrt = int(num ** 0.5)
if sqrt ** 2 == num:
factors.append(sqrt)
sqrt -= 1
for x in range(2, sqrt + 1):
if not (num % x):
factors += [x, num // x]
return factors
def factor(n): def get_abundant(limit):
ll = [1] gets = [12]
i = 2 for x in range(13, limit):
while i <= int(n ** 0.5): if sum(all_factor(x)) > x:
if n % i == 0: gets.append(x)
ll.append(i) return gets
if n // i != i:
ll.append(n / i)
i += 1
return ll
def test(x): def get_none_dule_abundant(limit):
sum = 0 factor = get_abundant(limit)
for i in factor(x): flags = [1] * (limit + 1)
sum += i for x in range(len(factor)):
if sum > x: for y in range(x, len(factor)):
return 1 s = factor[x] + factor[y]
else: return 0 if s > limit:
def ablist(max):
all = []
for i in xrange(10, max + 1):
if test(i):
all.append(i)
return all
abnum = ablist(_limit)
if __name__ == '__main__':
num = range(_limit + 1)
for xx in abnum:
for yy in abnum:
tmp = xx + yy
if tmp < _limit:
num[tmp] = 0
else:
break break
sum = 0 flags[s] = 0
for i in num: return [x[0] for x in enumerate(flags) if x[1]]
sum += i
print sum print(sum(get_none_dule_abundant(28123)))

View File

@ -1,25 +1,19 @@
_max = 10
_end = 1000000
_last = 3628800 def make_permutation(n):
p = [1]
for i in range(2, n):
p = [p[0] * i] + p
return p
locale = _end - 1 def get_order(n, order):
permutation = make_permutation(n)
number = list(range(n))
sequence = []
order -= 1
for p in permutation:
q = order // p
sequence.append(number.pop(q))
order %= p
return sequence + number
num = [1, 1] print(''.join(map(lambda x: str(x), get_order(10, 1000000))))
count = [1,1,1,1,1,1,1,1,1,1]
ch = '0123456789'
out = ''
i = 2
while len(num) < 10:
num.append(num[-1] * i)
i += 1
for i in xrange(_max - 1, 0, -1):
count[i] = locale / num[i]
locale %= num[i]
out += ch[count[i]]
ch = ch[:count[i]] + ch[count[i] + 1:]
print out + ch

View File

@ -1,7 +1,13 @@
fib = [1, 1, 0]
num = 2 def fibonacci():
while 1: a, b = 1, 1
fib[num % 2] = fib[(num + 1) % 2] + fib[(num + 2) % 2] while True:
if len(str(fib[num % 2])) == 1000: break yield a
num += 1 a, b = b, a + b
print num + 1
def fib_digit(digit):
for i, value in enumerate(fibonacci()):
if len(str(value)) == digit:
return i + 1
print(fib_digit(1000))

View File

@ -1,39 +1,25 @@
# coding=utf-8
def divnum(a):
mod = []
div = 1
while 1:
tmp = div % a
if tmp == 0:
return len(mod)
elif mod.count(tmp): break
else:
mod.append(tmp)
div *= 10
return len(mod) - mod.index(tmp)
def sum_mod(m):
mod = 9 % m
while True:
yield mod
mod = ((mod * 10) + 9) % m
def divnum1(a): def exact_div(m):
while a % 2 == 0: if not m:
a /= 2 return 0
while a % 5 == 0: while not (m % 2):
a /= 5 m //= 2
j = 1 while not (m % 5):
while 1: m //= 5
tmp = int('9' * j) if 1 == m:
if tmp % a == 0: return 0
return str(tmp / a) for i, value in enumerate(sum_mod(m)):
j += 1 if not value:
return i + 1
def max_cycle(limit):
cycles = [exact_div(x) for x in range(limit + 1)]
return cycles.index(max(cycles))
print(max_cycle(1000))
maxnum = [0, 0]
maxx = 1000
for i in xrange(1, maxx + 1):
temp = divnum(i)
#temp = len(divnum1(i))
if temp > maxnum[1]:
maxnum[0] = i
maxnum[1] = temp
print maxnum

View File

@ -1,33 +1,37 @@
def isprime(x):
if x <= 0: from tools import number_theory
def test_prime(x, prime):
for p in prime:
if p ** 2 > x:
return True
if not (x % p):
return False
return False return False
if x == 2:
return True
temp = 3
while temp <= int(x ** 0.5) + 1:
if x % temp == 0: return False
else: temp += 2
return True
def gen_value(a, b):
delta = lambda x, y: 2 * x + y + 1
a = [0, 0, 0]
for j in xrange(1001):
if isprime(j):
for i in xrange(-1000, 1001):
n = 0 n = 0
tmp = j x = b
while 1: prime = list(number_theory.make_prime(10000))
tmp += delta(n, i) while True:
if isprime(tmp): if prime[-1] ** 2 < x:
#print j, '\t', i, '\t', tmp prime = list(number_theory.make_prime(prime[-1] * 2 + 1))
if 0 > x:
return
if not test_prime(x, prime):
return
yield x
x += 2 * n + a + 1
n += 1 n += 1
else:
break def test_polynomial(limit):
if n > a[0]: maxi = [0, (0, 0)]
a[0] = n for b in number_theory.make_prime(limit):
a[1] = i for a in range(-b, 0, 2):
a[2] = j l = len(list(gen_value(a, b)))
print a[1] * a[2], '=', a[1], '*', a[2] if l > maxi[0]:
maxi = [l, (a, b)]
print(maxi)
return maxi[1][0] * maxi[1][1]
print(test_polynomial(1000))

View File

@ -1,8 +1 @@
total = 0 print(sum([4 * (x ** 2 + x // 2 + 1) for x in range(0, 1001, 2)]) - 3)
for i in xrange(1, 1002):
tmp = i * i
total += tmp + (1 - i % 2)
total += tmp + i + 1
print total - 1001 * 1002 - 1

View File

@ -1,8 +1,9 @@
lis = []
for i in xrange(2, 101):
for j in xrange(2, 101):
tmp = i ** j
if lis.count(tmp) == 0:
lis.append(tmp)
print len(lis) def mk_uniqe(limit):
s = set()
for i in range(2, limit + 1):
for j in range(2, limit + 1):
s.add(i ** j)
return s
print(len(mk_uniqe(100)))

View File

@ -1,20 +1,25 @@
# coding=utf-8
''' The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ? '''
'''分解因数,如果是素数返回''' from tools import number_theory
def factor(x, min = 2):
temp = min
while temp <= int(x ** 0.5) + 1: #从最小值到上界开始尝试
if x % temp == 0: return temp # 如果 a 能分解则返回最小因子
else: temp += 1
return 1 # 如果 a 是素数就返回 1此处也可以设置为返回 x 本身
n = 600851475143 def find_a_factor(num, prime):
i = 2 # 尝试循环分解 n 的因子 while prime:
while i <= int(math.sqrt(n)) + 1: p = prime.pop(0)
if n % i == 0 : # 如果满足 i 整除 n if not (num % p):
if factor(n / i) == 1: break # 同时 n / i 是素数则返回 return p, prime
else: n /= i # 如果 n / i 不为素数,就缩小 n 以减小运算量 return 0, []
i += 1
print n / i # 输出结果 def factor_num(num):
prime = list(number_theory.make_prime(int(num ** 0.5)))
factors = []
while True:
factor, prime = find_a_factor(num, prime)
if factor:
factors.append(factor)
while not (num % factor):
num //= factor
else:
if 1 != num:
factors.append(num)
return factors
print(factor_num(600851475143))

View File

@ -1,15 +1,21 @@
def ala(x, n = 5):
ss = 0
while x != 0:
ss += (x % 10) ** n
x /= 10
return ss
total = 0 def get_limit(num):
for i in xrange(1000000): n = 1
if i == ala(i): while (10 ** n) < n * (9 ** num):
print i n += 1
total += i return n
def eq(x, num):
ori_number = x
sum_power = 0
while x:
sum_power += (x % 10) ** num
x //= 10
return sum_power == ori_number
print '\n\n', total - 1 def get(num):
for x in range(2, 10 ** get_limit(num)):
if eq(x, num):
yield x
print(sum(get(5)))

View File

@ -1,17 +1,14 @@
cash = (200, 100, 50, 20, 10, 5, 2, 1)
#cash = (5, 2, 1)
total = []
def im(lis, x, n, a = 0): def count_money_iter(total, cash, count):
if a == len(cash) - 1: if not len(cash):
x.append(n) count[0] += 1
lis.append(x[:]) else:
x.pop() for x in range(total // cash[0] + 1):
return count_money_iter(total - x * cash[0], cash[1:], count)
for i in xrange(int(n / cash[a]) + 1):
x.append(i)
im(lis, x, n - i * cash[a], a + 1)
x.pop()
im(total, [], 200) def count_money(total, cash):
print len(total) count = [0]
count_money_iter(total, cash, count)
return count[0]
print(count_money(200, [200, 100, 50, 20, 10, 5, 2]))

View File

@ -1,53 +1,23 @@
from math import log10
def pick(x, lis, out, a = 0): def make_permutation_iter(num, l, s, result):
if x == 0: if not num:
out.append([a, lis]) result.append((s, l))
return
a *= 10
for i in xrange(len(lis)):
tmp = lis[:]
tmpa = a + lis[i]
tmp.pop(i)
pick(x - 1, tmp, out, tmpa)
def test(x, n):
tmp = x[:]
while n > 0:
if tmp.count(n % 10):
tmp.remove(n % 10)
n /= 10
else: else:
return False for i in range(len(l)):
if len(tmp) > 0: make_permutation_iter(num - 1, l[:i] + l[i + 1:], s + l[i], result)
return False
return True
total = [] def make_permutation(num, l):
result = []
make_permutation_iter(num, l, '', result)
return result
tt = [] def add_type(a, b):
pick(1, [1,2,3,4,5,6,7,8,9], tt) s = set()
for i in tt: for x, left in make_permutation(a, '123456789'):
yy = [] for y, cmpr in make_permutation(b, left):
pick(4, i[1], yy) multi = int(x) * int(y)
for j in yy: if str(sorted(cmpr)) == str(sorted(str(multi))):
if test(j[1], i[0] * j[0]): s.add(multi)
tmp = i[0] * j[0] return sum(s)
if total.count(tmp) == 0:
total.append(tmp)
tt = [] print(add_type(1, 4) + add_type(2, 3))
pick(2, [1,2,3,4,5,6,7,8,9], tt)
for i in tt:
yy = []
pick(3, i[1], yy)
for j in yy:
if test(j[1], i[0] * j[0]):
tmp = i[0] * j[0]
if total.count(tmp) == 0:
total.append(tmp)
print sum(total)

View File

@ -1,39 +1,15 @@
gcd = lambda x, y: y == 0 and x or gcd(y, x % y)
from tools import number_theory
def common(x, y): def multi():
a = [] numerator, denominator = 1, 1
b = [] for a in range(1, 10):
while x > 0: for b in range(1, 10):
a.append(x % 10) if a != b:
x /= 10 if not ((10 * a * b) % (9 * a + b)):
while y > 0: c = 10 * a * b // (9 * a + b)
b.append(y % 10) numerator *= 10 * a + b
y /= 10 denominator *= 10 * b + c
outa = 0 return denominator // number_theory.gcd(numerator, denominator)
outb = 0
tmp = list(set(a) & set(b))
if tmp.count(0) != 0:
tmp.remove(0)
if len(tmp) > 0:
for i in tmp:
a.remove(i)
b.remove(i)
if len(a) == 0 or len(b) == 0:
return (False, 0)
a.reverse()
for j in a: outa = outa * 10 + j
b.reverse()
for j in b: outb = outb * 10 + j
return (True, outa, outb)
else:
return (False, 0)
for i in xrange(11, 100):
for j in xrange(i + 1, 100):
tmp = common(i, j)
if tmp[0]:
if tmp[1] * j == tmp[2] * i:
print i, j
print(multi())

View File

@ -1,25 +1,21 @@
def mul(x):
out = 1
for i in xrange(2, x + 1):
out *= i
return out
def ala(x): from functools import reduce
tt = x
xx = 0
while tt > 0:
xx += mul(tt % 10)
tt /= 10
if xx == x:
return True
else:
return False
total = 0 def gen_fac(n):
i = 3 base = 1
while i < 100000: for n in range(1, n + 2):
if ala(i): yield base
print i base *= n
total += i
i += 1 def find_sum():
print total s = set()
fac = list(gen_fac(9))
for value in fac:
if 3 > value:
continue
for x in range(value, value + 1000):
if reduce(lambda x, y: x + fac[y], list(map(lambda x: int(x), str(x))), 0) == x:
s.add(x)
return s
print(sum(find_sum()))

View File

@ -1,42 +1,21 @@
''' The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million? '''
from math import log10 from tools import number_theory
pp = [2] def roll(num):
for i in xrange(3, 1000, 2): s = str(num)
for x in pp: for i in range(len(s)):
if i % x == 0: yield int(s[i:] + s[:i])
def find_roll_prime(limit):
count = 13
prime = list(number_theory.make_prime(limit))[25:]
prime = list(filter(lambda x: ('5' not in str(x)), prime))
for p in prime:
for x in roll(p):
if x not in prime:
break break
else: else:
pp.append(i) count += 1
return count
def isp(a):
for i in pp:
if a % i == 0:
if a == i:
return True
return False
return True
def loop(x):
length = int(log10(x))
return (x % 10) * 10 ** length + x / 10
def lote(n):
tt = n
while 1:
if not isp(tt):
return False
tt = loop(tt)
if tt == n:
return True
out = [2]
for ii in xrange(3, 1000000, 2):
if lote(ii):
out.append(ii)
print len(out)
print(find_roll_prime(1000000))

View File

@ -1,45 +1,23 @@
def rev(x):
out = ''
for i in xrange(len(x)):
out += x[-1 - i]
return out
def gen_bit(bit_len):
for i in range(2 ** ((bit_len + 1) // 2)):
def make(x): unit = bin(i)[2:]
if x == 1: half = '0' * ((bit_len + 1) // 2 - len(unit)) + unit
return [1,2,3,4,5,6,7,8,9] if bit_len % 2:
lenn = 10 ** (x / 2) yield half + half[-2::-1]
out = []
for i in xrange(lenn / 10, lenn):
a = str(i)
b = rev(a)
if x % 2:
for i in xrange(10):
out.append(int(a + str(i) + b))
else: else:
out.append(int(a + b)) yield half + half[::-1]
return out
def count_palindromic(limit):
bit_len = 1
result = []
while True:
for s in gen_bit(bit_len):
num = eval('0b1' + s + '1')
if num > limit:
return [1, 3] + result
if str(num) == str(num)[::-1]:
result.append(num)
bit_len += 1
print(sum(count_palindromic(1000000)))
def test(x):
bi = []
while x > 0:
bi.append(x % 2)
x /= 2
bb = bi[:]
bb.reverse()
if bi == bb:
#print bi,
return True
else:
return False
total = 0
for i in xrange(1, 7):
for j in make(i):
if test(j):
#print j
total += j
print total

View File

@ -1,35 +1,45 @@
from math import sqrt, log10
def isp(x): from tools import number_theory
if x == 2:
return True def shift(num):
if x <= 1 or x & 1 == 0: 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 return False
for i in xrange(3, int(sqrt(x)) + 1, 2): if ('1' == x[0]) or ('1' == x[-1]):
if x % i == 0: 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 False
return True return True
def search():
def ananum(x): find = []
if isp(x): prime = list(number_theory.make_prime(1000000))
for i in xrange(1, int(log10(x)) + 1): for p in filter(lambda x: check_prime(x), prime):
if isp(x / (10 ** i)) and isp(x % (10 ** i)): for s in shift(p):
continue if s not in prime:
break
else: else:
return False find.append(p)
return True return find[4:]
return False
print(sum(search()))
count = 0
total = []
n = 11
while count < 11:
if ananum(n):
count += 1
total.append(n)
n += 1
print count, sum(total)
print total

View File

@ -1,31 +1,18 @@
def pick(x, lis, out, a = 0):
if x == 0:
out.append(a)
return
a *= 10
for i in xrange(len(lis)):
tmp = lis[:]
tmpa = a + lis[i]
tmp.pop(i)
pick(x - 1, tmp, out, tmpa)
def test(x): def seek(prefix, l):
mm = '932718654' for a in l[::-1]:
ss = '' for b in l[::-1]:
i = 0 if a != b:
while len(ss) < 9: yield int(prefix + a + b)
i += 1
ss += str(i * x)
tt = []
for j in xrange(len(ss)):
tt.append(ss[j])
if len(tt) == len(set(tt)) and ss >= mm:
if not tt.count('0'):
print ss
num = [1,2,3,4,5,6,7,8,9] def test():
tt = [] for x in seek('93', '24567'):
pick(4, num, tt) num = str(x) + str(x * 2)
if '123456789' == ''.join(sorted(list(num))):
return num
for x in seek('92', '34567'):
num = str(x) + str(x * 2)
if '123456789' == ''.join(sorted(list(num))):
return num
for i in tt: print(test())
test(i)

View File

@ -1,21 +1,12 @@
a = {}
for i in xrange(1, 1000): def count(side):
for j in xrange(i, 1000): sides = {}
tmp = i * i + j * j for a in range(1, side // 3 + 1):
sqr = int(tmp ** 0.5) for b in range(a + 1, (side - a) // 2):
if tmp == sqr * sqr and i + j + sqr <= 1000: c = int((a ** 2 + b ** 2) ** 0.5)
tt = i + j + sqr if (a ** 2 + b ** 2) == c ** 2:
if a.keys().count(tt): s = a + b + c
a.update({tt: a.get(tt) + 1}) sides[s] = sides.setdefault(s, 0) + 1
else: return max(sides, key=lambda x: sides[x])
a.update({tt: 1})
print(count(1000))
mm = [0, 0]
for i in a.keys():
if a.get(i) > mm[1]:
mm[0] = i
mm[1] = a.get(i)
print mm

View File

@ -1,4 +1,19 @@
# coding=utf-8
''' A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.
Find the largest palindrome made from the product of two 3-digit numbers.. '''
def find_factor(num):
for x in range(999, 99, -1):
if not (num % x):
return x
else:
return 0
def make_palindrome():
for x in range(999, 99, -1):
s = str(x)
yield int(s + s[::-1])
for x in make_palindrome():
p = find_factor(x)
if p:
if 3 == len(str(x // p)):
print(x)
exit()

View File

@ -1,32 +1,22 @@
''' An irrational decimal fraction is created by concatenating the positive
integers:
0.123456789101112131415161718192021...
It can be seen that the 12th digit of the fractional part is 1.
If dn represents the nth digit of the fractional part, find the value of the following expression.
d1 * d10 * d100 * d1000 * d10000 * d100000 * d1000000 '''
from math import log10 def get_digit(n):
n -= 1
def num(x, i): i = 0
if i > int(log10(x)): while True:
raise IOError count_digit = (i + 1) * 9 * (10 ** i)
else: if n < count_digit:
i = int(log10(x)) - i
while i > 0:
x /= 10
i -= 1
return x % 10
def d(x):
elem = [0, 9, 189, 2889, 38889, 488889, 5888889, 68888889]
for i in xrange(len(elem)):
if elem[i] >= x:
break break
x -= elem[i - 1] + 1 n -= count_digit
return num(10 ** (i - 1) + x / i, x % i) i += 1
num, index = divmod(n, i + 1)
return int(str(num + 10 ** i)[index])
multi = 1 def multi_digit(limit):
for i in xrange(7): multi = 1
multi *= d(10 ** i) n = 1
#print d(10 ** i) while n < limit + 1:
print multi multi *= get_digit(n)
n *= 10
return multi
print(multi_digit(1000000))

View File

@ -1,33 +1,33 @@
def pick(x, lis, out, a = 0):
if x == 0:
out.append(a)
return
a *= 10
for i in xrange(len(lis)):
tmp = lis[:]
tmpa = a + lis[i]
tmp.pop(i)
pick(x - 1, tmp, out, tmpa)
from tools import number_theory
def isprime(x): def is_prime(num, prime):
if x == 2: for p in prime:
if p ** 2 > num:
return True return True
if x % 2 == 0: if not (num % p):
return False return False
temp = 3
while temp <= int(x ** 0.5) + 1:
if x % temp == 0: return False
else: temp += 2
return True
def gen_p(digit):
a = [1,2,3,4,5,6,7] l = list(reversed(range(1, digit + 1)))
tt = [] not_ordered = True
pick(len(a), a, tt) while not_ordered:
tt.reverse() yield int(''.join(map(lambda x: str(x), l)))
for i in tt: for i in range(len(l) - 1, 0, -1):
if isprime(i): if l[i - 1] > l[i]:
print i post = list(sorted(l[i - 1:]))
get = post.pop(post.index(l[i - 1]) - 1)
l = l[:i - 1] + [get] + list(reversed(post))
break break
else:
not_ordered = False
def find():
prime = list(number_theory.make_prime(10000))
for digit in range(8, 3, -1):
if digit % 3:
for x in gen_p(digit):
if is_prime(x, prime):
return x
print(find())

View File

@ -1,29 +1,18 @@
def trinum(x):
if x == 1:
return True
x *= 2
sqr = int(x ** 0.5)
if x == sqr * (sqr + 1):
return True
else:
return False
filein = open('words.txt', 'r') from functools import reduce
names = filein.read().split(',')
for ii in xrange(len(names)):
names[ii] = names[ii][1:-1]
def score(nn): def word_num(word):
mark = 0 return reduce(lambda x, y: x + ord(y) - ord('A') + 1, word, 0)
for i in nn:
mark += ord(i) - ord('A') + 1
return mark
def is_tri_num(num):
sqrt = int((2 * num) ** 0.5)
return not ((sqrt + 1) * sqrt // 2 - num)
count = 0 def file_get():
for i in names: with open('../resource/words.txt', 'r') as f:
if trinum(score(i)): context = f.read().replace('"', '').split(',')
#print '%3d\t' % score(i), i return list(filter(lambda x: is_tri_num(x), map(lambda x: word_num(x), context)))
count += 1
print count open('../resource/words')
print(len(file_get()))

View File

@ -1,31 +1,45 @@
def picksort(x, lis, out, a = 0): import time
if x == 0: time.clock()
out.append(a)
return
a *= 10
for i in xrange(len(lis)):
tmp = lis[:]
tmpa = a + lis[i]
tmp.pop(i)
picksort(x - 1, tmp, out, tmpa)
def check_3(result, num, num_set):
for p in num_set:
if 0 == int(p + num[:2]) % 3:
last_num = ''.join(list(num_set - set(p)))
result.append(int(last_num + p + num))
result.append(int(last_num[::-1] + p + num))
def check_5(result, num, num_set):
for p in num_set:
if 0 == int(p) % 2:
check_3(result, p + num, num_set - set(p))
def check_7(result, num, num_set):
for p in num_set:
if 0 == int(p + num[:2]) % 7:
check_5(result, p + num, num_set - set(p))
a = [0,1,2,3,4,6,7,8,9] def check_11(result, num, num_set):
tt = [] for p in '05':
picksort(len(a), a, tt) if 0 == int(p + num[:2]) % 11:
check_7(result, p + num, num_set - set(p))
def check_13(result, num, num_set):
for p in num_set:
if '0' == p or '5' == p:
continue
if 0 == int(p + num[:2]) % 13:
check_11(result, p + num, num_set - set(p))
total = 0 def check_17():
for i in tt: result = []
tttt = str(i) for n in range(136, 1000, 17):
if tttt[0] != '0' and int(tttt[3]) % 2 == 0: num = str(n)
if int(tttt[2:5]) % 3 == 0 and int(tttt[5:8]) % 13 == 0 and int(tttt[6:]) % 17 == 0: if '0' in num or '5' in num:
tmp = i % 10000 + (i / 10000 * 10 + 5) * 10000 continue
if int(str(tmp)[4:7]) % 7 == 0 and int(str(tmp)[5:8]) % 11 == 0: if len(num) == len(set(num)):
print tmp check_13(result, num, set('1234567890') - set(num))
total += tmp return result
print total print(sum(check_17()))
print(time.clock())

View File

@ -7,15 +7,14 @@ def test(x):
return False return False
return True return True
def main(): def main(limit):
max = 3000 for d in xrange(1, limit):
for n in xrange(4, max): for n in xrange(5, limit):
for m in xrange(n + 1, max): m = n + d
a = 3 * (m * m + n * n) - m - n a = 3 * (m * m + n * n) - m - n
b = (m - n) * (3 * (m + n) - 1) b = d * (3 * (m + n) - 1)
if test(a) and test(b): if test(a) and test(b):
print a / 2, b / 2, m, n print a / 2, b / 2, m, n
return return
main(1300)
main()

View File

@ -5,4 +5,4 @@ while 1:
if five == n * (n + 1) and n % 2 == 1: if five == n * (n + 1) and n % 2 == 1:
break break
m += 1 m += 1
print m * (3 * m - 1) / 2 print m, (n + 1) / 2, m * (3 * m - 1) / 2

View File

@ -1,31 +1,21 @@
from math import sqrt, log10
def isp(x): from tools import number_theory
if x == 2: import time
return True time.clock()
if x <= 1 or x & 1 == 0:
return False
for i in xrange(3, int(sqrt(x)) + 1, 2):
if x % i == 0:
return False
return True
def match(x, prime):
def test(x): for base in range(1, int(((x - 3) // 2) ** 0.5) + 1):
sqr = int(sqrt((x - 1) / 2)) if x - 2 * base * base in prime:
for i in xrange(1, sqr + 1):
tt = x - 2 * i * i
if isp(tt):
return True return True
return False 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
n = 9 print(search(10000))
while 1: print(time.clock())
if not isp(n):
if not test(n):
print n
break
n += 2
#kkkk = input('end')

View File

@ -1,37 +1,40 @@
def factor(x):
out = []
if x % 2 == 0:
out.append(2)
while x % 2 == 0:
x /= 2
i = 3
while 1:
if x % i == 0:
out.append(i)
while x % i == 0:
x /= i
i += 2
if i ** 2 > x:
out.append(x)
break
while out.count(1):
out.remove(1)
return out
from tools import number_theory
def main(same): def num_factor(num):
n = 6 factor = []
num = 0 if not num % 2:
while num != same: factor.append(2)
if len(factor(n)) == same: while not num % 2:
num += 1 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: else:
num = 0 seq = 0
n += 1 if seq == count:
return n 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
maxx = 4 print(search(4, 200000))
a = main(maxx)
for i in xrange(1, maxx + 1):
print a - i, factor(a - i)

View File

@ -1,6 +1,3 @@
total = 0
for i in xrange(1, 1001): from functools import reduce
total += i ** i print(str(reduce(lambda x, y: x + y ** y, range(1, 1001), 0))[-10:])
print total % (10 ** 10)

View File

@ -10,7 +10,7 @@ def pick(x, lis, out, a = 0):
out.append([a, lis]) out.append([a, lis])
return return
a *= 10 a *= 10
for i in xrange(len(lis)): for i in range(len(lis)):
tmp = lis[:] tmp = lis[:]
tmpa = a + lis[i] tmpa = a + lis[i]
tmp.pop(i) tmp.pop(i)
@ -21,7 +21,7 @@ def isp(x):
return True return True
if x <= 1 or x & 1 == 0: if x <= 1 or x & 1 == 0:
return False return False
for i in xrange(3, int(x ** 0.5) + 1, 2): for i in range(3, int(x ** 0.5) + 1, 2):
if x % i == 0: if x % i == 0:
return False return False
return True return True
@ -45,4 +45,4 @@ def main():
return str(n) + str(ii) + str(jj) #(n, ii, jj) return str(n) + str(ii) + str(jj) #(n, ii, jj)
n += 1 n += 1
print main() print(main())

View File

@ -1,36 +1,8 @@
# coding=utf-8
''' 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? '''
from time import time from functools import reduce
from tools import number_theory
gcd = lambda x, y: (y == 0) and x or gcd(y, x % y) def multu_lcm(l):
return reduce(number_theory.lcm, l)
print(multu_lcm(list(range(1, 21))))
def p1(maxx = 20):
maxx += 1
num = []
for i in range(maxx): num.append(i)
for i in range(2, maxx):
if num[i] > 1:
for j in range(i + 1, maxx):
if num[j] % num[i] == 0: num[j] /= num[i]
total = 1
for i in num[1 : maxx]: total *= num[i]
#print num
print total
def p2(maxx = 20):
n = 2
for i in xrange(3, maxx + 1):
if n % i != 0:
n = n * i / gcd(n, i)
return n
#p1(20)
print p2(50000)

View File

@ -1,34 +1,49 @@
# coding=utf8
prime = [] from tools import number_theory
total = 0
n = 2
def factor(x): def is_prime(num):
x = int(x) if not num % 2:
if x <= 1: return 0 return False
else: for p in range(3, int(num ** 0.5) + 1, 2):
for i in xrange(2, int(x ** 0.5) + 1): if not num % p:
if x % i == 0: break return False
else: return x return True
return i
while total <= 1000000: def longest_slice(limit, prime):
if factor(n) == n: for l in range(len(prime)):
total += n if sum(prime[:l]) > limit:
prime.append(n) return (l - 1, sum(prime[:l - 1]))
n += 1
def search(): def reduce_slice(limit, prime, start, length, tale):
for length in xrange(len(prime) - 1, 2, -1): tale -= prime[start]
for start in xrange(0, len(prime) - length + 1): start += 1
sump = 0 t = tale
for tmp in prime[start: start + length]: while True:
sump += tmp t = tale - prime[start] + prime[start + length]
if factor(sump) == sump: if t > limit:
print sump break
print prime[start: start + length] tale = t
return 0 return (start, tale)
if __name__ == '__main__': def shift_slice(limit, prime, start, length, tale):
search() for s in range(start - 1, -1, -1):
tale = tale + prime[s] - prime[s + length]
if is_prime(tale):
return (tale, length)
def search(limit):
prime = list(number_theory.make_prime(4000))
length, tale = longest_slice(limit, prime)
if is_prime(tale):
return (tale, length)
start = 0
while length > 1:
length -= 1
start, tale = reduce_slice(limit, prime, start, length, tale)
if is_prime(tale):
return (tale, length)
get = shift_slice(limit, prime, start, length, tale)
if get:
return get
print(search(1000000))

View File

@ -1,3 +1,4 @@
'''
from string import maketrans, translate from string import maketrans, translate
def numbreak(x): def numbreak(x):
@ -9,7 +10,7 @@ def numbreak(x):
def numloop(x, a, lis): def numloop(x, a, lis):
out = [] out = []
for i in xrange(10 - a): for i in range(10 - a):
tt = maketrans(str(a), str(i + a)) tt = maketrans(str(a), str(i + a))
tmp = int(translate(str(x), tt)) tmp = int(translate(str(x), tt))
if isp(tmp, lis): if isp(tmp, lis):
@ -45,13 +46,19 @@ def main():
xx = 56003 xx = 56003
while 1: while 1:
ss = numbreak(xx) ss = numbreak(xx)
for syn in xrange(3): for syn in range(3):
if syn in ss: if syn in ss:
tmp = numloop(xx, syn, prime) tmp = numloop(xx, syn, prime)
if len(tmp) >= 8: if len(tmp) >= 8:
print xx, tmp print(xx, tmp)
return return
xx += 2 xx += 2
while not isp(xx, prime): xx += 2 while not isp(xx, prime): xx += 2
main() main()
'''
from tools import number_theory
prime = list(number_theory.make_prime(1000))
print(prime)

View File

@ -1,24 +1,15 @@
def divnum(x):
out = []
while x > 0:
out.append(x % 10)
x /= 10
return out
def multi_same(num, multi):
def testnum(x, n): s = set(str(num))
a = divnum(x) for m in range(multi, 1, -1):
for i in xrange(2, n + 1): if set(str(num * m)) != s:
b = divnum(x * i)
if set(a) != set(b):
return False return False
return True return True
def search(multi):
num = 124847
while not multi_same(num, multi):
num += 1
return num
n = 1 print(search(6))
while n < 1000000:
if testnum(n, 6):
break
n += 1
for i in xrange(1, 7):
print n * i

View File

@ -1,26 +1,14 @@
def C(x, y):
if x * 2 > y:
return C(y - x, y)
out = 1
for i in xrange(y, y - x, -1):
out *= i
for i in xrange(1, x + 1):
out /= i
return out
total = [] import time
for i in xrange(1, 101): time.clock()
j = 0
tmp = 0
for j in xrange(i + 1):
tmp = C(j, i)
if tmp < 1000000:
total.append((j, i, tmp))
else:
break
if 2 * j < i:
for j in xrange(j - 1, -1, -1):
total.append((i - j, i))
def search(limit, count):
l = [1, 1]
tale = 0
while len(l) < count + 2:
tale += len(list(filter(lambda x: x > limit - 1, l)))
l = list(map(lambda x: x[0] + x[1], zip(l + [0], [0] + l)))
return tale
print 103 * 50 - len(total) print(search(1000000, 100))
print(time.clock())

View File

@ -16,7 +16,7 @@ def calc(x):
step = 1 step = 1
dic = {4:[], 3:[], 2:[], 1:[]} dic = {4:[], 3:[], 2:[], 1:[]}
same = 1 same = 1
for i in xrange(1, len(num)): for i in range(1, len(num)):
diff = num[i] - num[i - 1] diff = num[i] - num[i - 1]
if diff == 0: if diff == 0:
same += 1 same += 1
@ -48,7 +48,7 @@ def calc(x):
return (flag, 0, dic.get(1)) return (flag, 0, dic.get(1))
def main(): def main():
ff = open('poker.txt', 'r') ff = open('../resource/poker.txt', 'r')
out = 0 out = 0
for line in ff.readlines(): for line in ff.readlines():
strlis = line.split(' ') strlis = line.split(' ')
@ -58,4 +58,4 @@ def main():
return out return out
#print calc(['7C','4C','4C','4C','7C']) #print calc(['7C','4C','4C','4C','7C'])
print main() print(main())

View File

@ -1,39 +1,34 @@
def revnum(x):
out = 0
while x != 0:
out *= 10
out += x % 10
x /= 10
return out
def isL(x_ori):
x = x_ori
x += revnum(x)
n = 0
while n < 50:
x_ = revnum(x)
if x_ == x:
return False
x += x_
n += 1
return True
def test(x):
n = 0
while n < 50:
print x
x_ = revnum(x)
if x == x_: break
x += x_
n += 1
import time import time
a0 = time.clock() time.clock()
be = []
for i in xrange(1, 10001): def is_sync(num):
if isL(i): s = str(num)
be.append(i) return s == s[::-1]
a1 = time.clock()
print be def is_lic(num, lic_set, non_set):
print a1 - a0 step = set([num])
for i in range(50):
num = num + int(str(num)[::-1])
step.add(num)
if num in lic_set:
return (True, step)
if num in non_set:
return (False, step)
if is_sync(num):
return (False, step)
return (True, step)
def search(limit):
lic_set = set()
non_set = set()
for x in range(1, limit + 1):
judge, step = is_lic(x, lic_set, non_set)
if judge:
lic_set |= set(filter(lambda x: x <= limit, step))
else:
non_set |= set(filter(lambda x: x <= limit, step))
return lic_set
print(len(search(10000)))
print(time.clock())

View File

@ -1,15 +1,13 @@
def numsum(x):
out = 0
while x > 0:
out += x % 10
x /= 10
return out
def search(limit):
maxi = [0, 0, 0]
for x in range(1, limit + 1):
num = 1
for y in range(limit):
num *= x
s = sum(map(lambda x: int(x), str(num)))
if s > maxi[0]:
maxi = [s, x, y + 1]
return maxi
mmax = [0, 0, 0] print(search(100))
for i in xrange(1, 101):
for j in xrange(1, 101):
tmp = numsum(i ** j)
if tmp > mmax[0]:
mmax = [tmp, i, j]
print mmax

View File

@ -1,10 +1,8 @@
from math import log10
sq2 = [(1, 1)] def iter_sqrt(limit):
count = 0 a, b = 2, 1
for i in xrange(1, 1001): for i in range(limit):
last = sq2[len(sq2) - 1] a, b = b + 2 * a, a
if int(log10(last[0])) > int(log10(last[1])): yield a - b, b
print last
count += 1 print(len(list(filter(lambda x: len(str(x[0])) > len(str(x[1])), iter_sqrt(1000)))))
sq2.append((last[0] + 2 * last[1], last[0] + last[1]))
print count

View File

@ -1,28 +1,20 @@
laymax = lambda x: (2 * x + 1) ** 2
def isp(x): def is_prime(x):
if x == 2: for p in range(3, int(x ** 0.5) + 1, 2):
return True if x % p == 0:
if x <= 1 or x & 1 == 0:
return False
for i in xrange(3, int(x ** 0.5) + 1, 2):
if x % i == 0:
return False return False
return True return True
be = 3 def search(percent):
non = 2 n = 7
prime = 8
total = 13
while prime / total > percent:
n += 2
total += 4
for num in range((n - 3) * n + 3, n * n - 1, n - 1):
if is_prime(num):
prime += 1
return n
i = 2 print(search(0.1))
while 9 * be >= non:
tmp = laymax(i)
for j in xrange(4):
#print float(be) / (be + non)
if isp(tmp):
be += 1
else:
non += 1
tmp -= 2 * i
i += 1
print 2 * i - 1

View File

@ -1,16 +1,14 @@
# coding=utf-8
''' The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. '''
total = 0 from functools import reduce
num = []
for i in range(100): num.append(i + 1) def product(p, q):
for i in range(100): multi = 0
for j in range(i + 1, 100): while True:
total += num[i] * num[j] q.pop(0)
total *= 2 if not q:
print total break
multi += reduce(lambda x, y: x + y[0] * y[1], zip(p, q), 0)
return multi
limit = 10000 + 1
print(2 * product(list(range(1, limit)), list(range(1, limit))))

42
python/68.py Normal file
View File

@ -0,0 +1,42 @@
m = 0
def eq(l):
if l[0] + l[1] - l[2] - l[5]:
return False
if l[2] + l[3] - l[4] - l[7]:
return False
if l[4] + l[5] - l[6] - l[9]:
return False
if l[6] + l[7] - l[8] - l[1]:
return False
return True
def ext(l):
o = l[1::2]
e = l[::2]
while e[0] != min(e):
o = o[1:] + o[0:1]
e = e[1:] + e[0:1]
o = list(map(lambda x: str(x), o))
e = list(map(lambda x: str(x), e))
s = o[1:] + o[0:1]
return ''.join([''.join(x) for x in zip(e, o, s)])
def num(l):
bn = ext(l)
if len(bn) != 16:
return 0
return int(bn)
def mksq(sq, pl):
if not pl:
if eq(sq):
n = num(sq)
global m
if n > m:
m = n
print(m)
for it in pl:
mksq(sq + [it], list(filter(lambda x: x != it, pl)))
mksq([], list(range(1, 11)))

View File

@ -1,23 +1,14 @@
# coding=utf-8
''' By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number? '''
import math import math
from tools import number_theory
def countp(count): def mkp(count):
if count == 1: return 2 limit = int(count / math.log(count))
prime = [2] limit = count * count // limit * 11 // 10
x = 1 while True:
while 1: prime = list(number_theory.make_prime(limit))
for i in xrange(x ** 2 + (x + 1) % 2, (x + 1) ** 2, 2): if count < len(prime):
for p in prime: return prime[count - 1]
if i % p == 0: break limit = limit * 11 // 10
else: count -= 1
if count == 0: return i
x += 1
for p in prime:
if x % p == 0: break
else: prime.append(x)
if __name__ == '__main__': print(mkp(10001))
print countp(10001)

View File

@ -1,15 +1,16 @@
f = lambda x: 3 * x / 7
gcd = lambda x, y: (y == 0) and x or gcd(y, x % y)
maxx = [1] * 3 def gcd(a, b):
return 0 == b and a or gcd(b, a % b)
for i in xrange(1, 1000001): def t(limit, a, b):
if i % 7 == 0: l = []
continue for m in range(limit + 1, 1, -1):
tmpi = f(i) n, d = divmod(m * a, b)
if gcd(i, tmpi) == 1: if gcd(n, m) == 1:
tmp = 3.0 / 7 - float(tmpi) / i l.append((d / m, n, m))
if tmp < maxx[0]: if 1 == d:
maxx = [tmp, tmpi, i] break
for x in sorted(l, key=lambda x: x[0]):
return x[1:]
print maxx print(t(1000000, 3, 7))

View File

@ -1,19 +1,12 @@
# coding=utf-8
''' Discover the largest product of five consecutive digits in the 1000-digit number. '''
def va(string): from functools import reduce
x = 1
for i in range(len(string)):
x *= ord(string[i]) - ord('0')
return x
ch = '731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450' def seek(maxi, length, context):
max = [0, 0, ''] if length > len(context):
for i in range(len(ch) - 5): return maxi
temp = va(ch[i : i + 5]) multi = reduce(lambda x, y: x * int(y), context[:length], 1)
if temp > max[1]: return seek(max(maxi, multi), length, context[1:])
max[0] = i
max[1] = temp
max[2] = ch[i : i + 5]
print max print(seek(0, 13,
'731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'
))

View File

@ -1,37 +1,25 @@
from math import sqrt, log10
def fiter(a, finit, fcntn, ftrns):
def newton(a, n = 2): x = finit(a)
x = 0 while fcntn(a, x):
x_ = int(sqrt(a)) x = ftrns(a, x)
if x_ ** n == a:
return x_
while abs(x - x_) > 1:
x = x_
tmp = x ** (n - 1)
x_ = x - (tmp * x - a) / tmp / n
x_ = int(x_)
while x ** n > a:
x -= 1
return x return x
def numsum(x): def newton_root(a):
total = 0 return fiter(a,
while x != 0: lambda x: x // 2,
total += x % 10 lambda p, x: not (0 < p - x ** 2 < 2 * x + 1),
x /= 10 lambda p, x: (x + p // x) // 2)
return total
def main(maxx): def all_root(limit, digit):
ss = 0 root_sum = []
for i in xrange(maxx + 1): digit -= 1
if int(sqrt(i)) ** 2 == i: for x in range(2, limit + 1):
continue if int(x ** 0.5) ** 2 != x:
tmp = newton(i * 100 ** 99) root_sum.append(sum(map(lambda x: int(x), str(newton_root(100 ** digit * x)))))
ss += numsum(tmp) return root_sum
print ss
print(sum(all_root(100, 100)))

View File

@ -1,43 +1,39 @@
a = []
ff = open('matrix.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[0][0], [a[0][0]]]] m = [
n = len(a) [131, 673, 234, 103, 18],
[201, 96, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 956],
[805, 732, 524, 37, 331]
]
for k in xrange(1, n): def getm(file):
pathtmp = [] matrix = []
tmp = [a[k][0] + path[0][0], path[0][1][:]] for line in open(file, 'r'):
tmp[1].append(a[k][0]) matrix.append(list(map(lambda x: int(x), line.split(','))))
pathtmp.append(tmp) return matrix
for i in xrange(1, k):
if path[i - 1][0] < path[i][0]:
flag = i - 1
else:
flag = i
tmp = [path[flag][0] + a[k - i][i], path[flag][1][:]]
tmp[1].append(a[k - i][i])
pathtmp.append(tmp)
tmp = [a[0][k] + path[k - 1][0], path[k - 1][1][:]]
tmp[1].append(a[0][k])
pathtmp.append(tmp)
path = pathtmp
for k in xrange(n, 2 * n - 1): def calc_short(short, nl):
pathtmp = [] for i in range(len(nl)):
for i in xrange(2 * n - 1 - k): nl[i] += min(short[i], short[i + 1])
if path[i][0] < path[i + 1][0]: return nl
flag = i
else:
flag = i + 1
tmp = [path[flag][0] + a[n - 1 - i][k + i + 1 - n], path[flag][1][:]]
tmp[1].append(a[n - 1 - i][k + i + 1 - n])
pathtmp.append(tmp)
path = pathtmp
print path def short_lu(matrix):
short = [0]
for x in range(len(matrix)):
nl = []
for i in range(x + 1):
nl.append(matrix[x - i][i])
short = calc_short(short[:1] + short + short[-1:], nl)
return short
def short_rd(matrix, short):
for x in range(len(matrix), (len(matrix) - 1) * 2 + 1):
nl = []
for i in range(x - len(matrix) + 1, len(matrix)):
nl.append(matrix[x - i][i])
short = calc_short(short, nl)
return short[0]
m = getm('../resource/matrix.txt')
print(short_rd(m, short_lu(m)))

View File

@ -1,88 +1,45 @@
a = []
ff = open('../matrix.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()
n = len(a) m = [
[131, 673, 234, 103, 18],
[201, 96, 342, 965, 150],
[630, 803, 746, 422, 111],
[537, 699, 497, 121, 956],
[805, 732, 524, 37, 331]
]
path = [] def getm(file):
for i in xrange(n): matrix = []
path.append([a[i][-1] + a[i][-2], []]) for line in open(file, 'r'):
matrix.append(list(map(lambda x: int(x), line.split(','))))
return matrix
for j in xrange(n - 3, -1, -1): def rev(matrix):
#newpath = [a[0][j] + min(path[0][0], a[1][j] + path[1][0])] n = []
if path[0][0] <= a[1][j] + path[1][0]: scale = len(matrix)
tmp = path[0][1] + [(0, j)] for j in range(scale):
newpath.append([path[0][0] + a[0][j], tmp]) line = []
else: for i in range(scale):
tmp = path[1][1] + [(1, j), (0, j)] line.append(matrix[i][j])
newpath.append([path[1][0] + a[1][j] + a[0][j], tmp]) n.append(line)
for i in xrange(1, n - 1): return n
better = min(a[i - 1][j] + path[i - 1][0], path[i][0], a[i + 1][j] + path[i + 1][0])
#newpath.append(a[i][j] + better)
if better == path[i][0]:
tmp = path[i][1] + [(i, j)]
newpath.append([path[i][0] + a[i][j], tmp])
elif better == a[i - 1][j] + path[i - 1][0]:
tmp = path[i - 1][1] + [(i - 1, j), (i, j)]
newpath.append([])
#newpath.append(a[-1][j] + min(path[-1], a[-2][j] + path[-2]))
if path[-1][0] <= a[-2][j] + path[-2][0]:
tmp = path[-1][1] + [(n - 1, j)]
newpath.append([a[-1][j] + path[-1][0], tmp])
else:
tmp = path[-1][1] + [(n - 2, j), (n - 1, j)]
newpath.append([a[-1][j] + a[-2][j] + path[-2][0], tmp])
path = newpath
def trace(matrix):
short = matrix.pop(0)
for line in matrix:
ns = []
for i in range(len(line)):
vs = 0
vl = [short[i]]
for k in range(i - 1, -1, -1):
vs += line[k]
vl.append(vs + short[k])
vs = 0
for k in range(i + 1, len(line)):
vs += line[k]
vl.append(vs + short[k])
ns.append(min(vl) + line[i])
short = ns
return short
print sorted(path) #print(min(trace(rev(m))))
print(min(trace(rev(getm('../resource/matrix.txt')))))
'''
path = []
for i in xrange(n):
path.append([a[i][0], [[i, 0]]])
for y in xrange(1, n):
pathtmp = []
for x in xrange(n):
tmp = [[0, 0]]
#papapa = 0
if x - 1 >= 0:
if a[x - 1][y] < a[x][y - 1]:
tmp.append([a[x - 1][y], -1, [x - 1, y]])
path[x] =
else:
if not [x, y - 1] in path[x - 1][1]:
tmp.append([a[x][y - 1], -1, [x, y - 1]])
#else:
#papapa += 1
if x + 1 < n:
if a[x + 1][y] < a[x][y - 1]:
tmp.append([a[x + 1][y], 1, [x + 1, y]])
else:
if not [x, y - 1] in path[x + 1][1]:
tmp.append([a[x][y - 1], 1, [x, y - 1]])
#else:
#papapa += 2
for item in tmp:
item[0] += path[x + item[1]][0]
tmp.sort()
#if papapa != 0:
#print papapa, "**", tmp
if len(tmp[0]) > 2:
last = [tmp[0][-1], [x, y]]
else:
last = [[x, y]]
pathtmp.append([tmp[0][0] + a[x][y], path[x + tmp[0][1]][1] + last])
path = pathtmp
path.sort()
print path[0]
'''

View File

@ -1,24 +1,31 @@
def sum(m, n):
return m * n * (m + 1) * (n + 1) / 4
maxx = 2000000 def gen_tri(limit):
x = int((8 * maxx + 1) ** 0.5 / 2) tale = 1
while sum(x, 1) < maxx: x += 1 base = 1
while tale < limit:
yield (base, tale)
base += 1
tale += base
orisub = abs(sum(x, 1) - maxx) def tri_pair(num):
near = [orisub] seq = int(((8 * num - 1) ** 0.5 - 1) / 2)
y = 1 return ((seq , (seq + 1) * seq // 2),
(seq + 1, (seq + 1) * (seq + 2) // 2))
while x > y: def renew(result, limit, t_n, t_o):
tmp = maxx cmpr = t_n[1] * t_o[1]
y_lst = y new_result = [max(limit, cmpr) - min(limit, cmpr), t_n[0] * t_o[0]]
while tmp > maxx - orisub and y > 0: if new_result[0] < result[0]:
tmp = sum(x, y) return new_result
if abs(tmp - maxx) < near[0]: else:
near = [abs(tmp - maxx), x, y] return result
y -= 1
x -= 1
y = y_lst
while sum(x, y) < maxx: y += 1
print near[1] * near[2] 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))

View File

@ -1,31 +1,28 @@
def test_ori(a, b):
tmp = a ** 2 + b ** 2
sqr = int(tmp ** 0.5)
if tmp == sqr ** 2: return True
return False
def test(a, b, c): import time
if test_ori(b + c, a): return 1
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 return 0
def test1(a, b): def count(m):
if test_ori(b + b, a): return 1 count = 0
return 0 for a in range(1, m * 2 + 1):
count += is_sqrt(a, m)
return count
total = 1975 def gen(limit):
n = 100 m = 1
while 1: tale = 0
tmp = 0 while tale < limit:
for i in xrange(2, 2 * n + 1): tale += count(m)
if test_ori(n, i): m += 1
#print i return m - 1, tale
tmp += i / 2
if i > n:
tmp -= i - n - 1
total += tmp
if total > 1000000:
break
#print n, tmp, total
n += 1
print n print(gen(1000000))
print(time.process_time())

View File

@ -1,16 +1,50 @@
def log2(x):
out = 0
while x > 0:
x /= 2
out += 1
return out - 1
def break(x): from functools import reduce
x -= 1
n = int(x ** 0.5)
while x % n != 0:
n -= 1
return (n, x / n)
def A(k): class Num:
dd 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())

View File

@ -1,8 +1,7 @@
# coding=utf-8
''' A Pythagorean triplet is a set of three natural numbers, a b c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc. '''
import math from math import sqrt
for x in range(int(5 * sqrt(10)) + 1, int(5 * sqrt(20)) + 1):
if not (500 % x):
y = 500 // x - x
print(2 * x * y * (x ** 4 - y ** 4))

View File

@ -0,0 +1,47 @@
def gcd(x, y):
if 0 == y:
return x
else:
return gcd(y, x % y)
def lcm(x, y):
return x * y // gcd(x, y)
def make_prime(limit):
if limit < 5:
if limit < 2: return
yield 2
if limit < 3: return
yield 3
return
n = (limit + 1) // 6
a = [True] * n
b = [True] * n
for i in range((int(limit ** 0.5) + 1) // 6 + 1):
if a[i]:
p = 6 * i + 7
f = 7 * i + 7
g = 5 * i + 5
a[f::p] = [False] * ((n - f - 1) // p + 1)
b[g::p] = [False] * ((n - g - 1) // p + 1)
if b[i]:
p = 6 * i + 5
f = 5 * i + 3
g = 7 * i + 5
a[f::p] = [False] * ((n - f - 1) // p + 1)
b[g::p] = [False] * ((n - g - 1) // p + 1)
yield 2
yield 3
for i in range(n):
if b[i]:
yield 6 * i + 5
if a[i]:
yield 6 * i + 7