Project Euler 13解题报告

题目: Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 74324986199524741059474233309513058123726617309629 91942213363574161572522430563301811072406154908250 23067588207539346171171980310421047513778063246676 89261670696623633820136378418383684178734361726757 28112879812849979408065481931592621691275889832738 44274228917432520321923589422876796487670272189318 47451445736001306439091167216856844588711603153276 70386486105843025439939619828917593665686757934951 62176457141856560629502157223196586755079324193331 64906352462741904929101432445813822663347944758178 92575867718337217661963751590579239728245598838407 58203565325359399008402633568948830189458628227828 80181199384826282014278194139940567587151170094390 35398664372827112653829987240784473053190104293586 86515506006295864861532075273371959191420517255829 71693888707715466499115593487603532921714970056938 54370070576826684624621495650076471787294438377604 53282654108756828443191190634694037855217779295145 36123272525000296071075082563815656710885258350721 45876576172410976447339110607218265236877223636045 17423706905851860660448207621209813287860733969412 81142660418086830619328460811191061556940512689692 51934325451728388641918047049293215058642563049483 62467221648435076201727918039944693004732956340691 15732444386908125794514089057706229429197107928209 55037687525678773091862540744969844508330393682126 18336384825330154686196124348767681297534375946515 80386287592878490201521685554828717201219257766954 78182833757993103614740356856449095527097864797581 16726320100436897842553539920931837441497806860984 48403098129077791799088218795327364475675590848030 87086987551392711854517078544161852424320693150332 59959406895756536782107074926966537676326235447210 69793950679652694742597709739166693763042633987085 41052684708299085211399427365734116182760315001271 65378607361501080857009149939512557028198746004375 35829035317434717326932123578154982629742552737307 94953759765105305946966067683156574377167401875275 88902802571733229619176668713819931811048770190271 25267680276078003013678680992525463401061632866526 36270218540497705585629946580636237993140746255962 24074486908231174977792365466257246923322810917141 91430288197103288597806669760892938638285025333403 34413065578016127815921815005561868836468420090470 23053081172816430487623791969842487255036638784583 11487696932154902810424020138335124462181441773470 63783299490636259666498587618221225225512486764533 67720186971698544312419572409913959008952310058822 95548255300263520781532296796249481641953868218774 76085327132285723110424803456124867697064507995236 37774242535411291684276865538926205024910326572967 23701913275725675285653248258265463092207058596522 29798860272258331913126375147341994889534765745501 18495701454879288984856827726077713721403798879715 38298203783031473527721580348144513491373226651381 ...

阅读全文 »

Project Euler 12解题报告

题目: The sequence of triangle numbers is generated by adding the natural numbers. So the 7 th 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? 中文题目: 三角形数序列是由对自然数的连加构造而成的。所以第七个三角形数是1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. 那么三角形数序列中的前十个是: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... 下面我们列出前七个三角形数的约数: 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 可以看出28是第一个拥有超过5个约数的三角形数。 那么第一个拥有超过500个约数的三角形数是多少? 解题报告: m = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279] n = 10000 li = [] while True: x = (n+1)*n/2 for i in m: t = 0 if x i: break if x % i != 0: continue while x % i == 0: x /= i t += 1 li.append(t) s = 1 for j in li: s *= (j+1) if s = 500: print (n+1)*n/2 break else: n += 1 li = [] ...

阅读全文 »

Project Euler 11解题报告

题目: In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 The product of these numbers is 26 63 78 14 = 1788696. What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid? 中文题目: 在以下这个2020的网格中,四个处于同一对角线上的相邻数字用红色标了出来: 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 这四个数字的乘积是:26 63 78 14 = 1788696. 在这个2020网格中,处于任何方向上(上,下,左,右或者对角线)的四个相邻数字的乘积的最大值是多少? 解题分析: grid = [ 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 , 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 , 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 1 ...

阅读全文 »

Project Euler 10解题报告

题目:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

中文题目:

10以下的质数的和是2 + 3 + 5 + 7 = 17.

找出两百万以下所有质数的和。

解题分析: 前面有个题目已经有了好的处理素数的方式,这次直接用前面的代码来实现。

limit = 1000000 
arr = [True] * limit
def sieve(x): 
    global arr,limit 
    for i in range(x*2,limit,x):
        arr[i] = False 
map(sieve, range(2,limit**1/2))
primes = []
for i in range(2,limit):
    if arr[i]: 
        primes.append(i) 
sum = 0 
for p in primes: 
    sum += p 
print sum 

阅读全文 »

Project Euler 9解题报告

题目: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 . 中文: 一个毕达哥拉斯三元组是一个包含三个自然数的集合,a b c,满足条件: a 2 + b 2 = c 2 例如:3 2 + 4 2 = 9 + 16 = 25 = 5 2 . 已知存在并且只存在一个毕达哥拉斯三元组满足条件 a + b + c = 1000。 找出该三元组中abc的乘积。 分析: 从 a b c以及 a + b + c = 1000的条件可以看出来a最大不会超过333,b不会超过500,因此代码如下: for x in range(1,332): for y in range(x,499): z=(x*x+y*y)**0.5 if x+y+z==1000: print x*y*z ...

阅读全文 »

project euler 7解题报告

problem 7: 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 10001st prime number? 版本一: 最原始的解法,花费的时间大概要20分钟左右。 import time i=3 num=1 start=time.time() while 1: for j in range(2,i): if i%j==0: break elif j==i-1: num=num+1 break else: continue if num==10001: print i break i=i+1 end=time.time() print end-start 版本二: 优化后的解法,花费时间大概是55秒左右。 import time i=3 num=1 S=[2] length=len(S) start=time.time() while 1: for j in S[0:length/2+1]: if i%j==0: break elif j==S[length/2]: S.append(i) length=length+1 break else: continue if length==10001: print S[10000] break i=i+2 end=time.time() print end-start 版本三: 进一步优化的解法,减少每次需要相除的个数,时间可以优化到不到3秒。 import time, math i=3 num=1 S=[2] length=len(S) start=time.time() while 1: l=int(math.ceil(math.sqrt(length))) for j in S[0:l]: if i%j==0: break elif j==S[l-1]: S.append(i) length=length+1 break else: continue if length==10001: print S[10000] break i=i+2 end=time.time() print end-start import time, math def findprimefactor(num): primefactor=[2,3,5,7] i=9 while len(primefactor) num: flag=True q=int(math.sqrt(i)) k=0 while(primefactor[k] =q): p=primefactor[k] if not(i%p): flag=True break else: flag=False k+=1 if not flag: primefactor.append(i) i+=2 return primefactor if __name__=='__main__': start=time.time() result=findprimefactor(10001) print result[-1],"sec:",time.time()-start 版本四:利用埃拉托色尼选筛法来进行计算,花费的时间大概是0.8秒左右。 import math , time limit=200000 L=[True]*limit start=time.time() def seive(x): for i in xrange(x*2,limit,x): L[i]=False map(seive,[2]) map(seive,xrange(3,int(math.ceil(math.sqrt(limit))),2)) primer=[] for i in xrange(2,limit): if L[i]: primer.append(i) print primer[10000] print time.time()-start ...

阅读全文 »

Project Euler 3解题报告

题目:The prime factors of 13195 are 5, 7, 13 and 29.


What is the largest prime factor of the number 600851475143 ?


题目3:找出一个合数的最大质数因子
13195的质数因子有5,7,13和29.


600851475143的最大质数因子是多少?


<a href="http://projecteuler.net/problem=3" target="_blank">Project Euler problem 3</a>


版本一:


先找出素数,然后确定最大的素因数。时间大概为37秒左右,属于很自然的但是效率很低的方法



import time, math
def findprimefactor(maxnum):
primefactor=[2,3,5,7]
i=9
while i flag=True
q=int(math.sqrt(i))
k=0
while(primefactor[k] p=primefactor[k]
if not(i%p):
flag=True
break
else:
flag=False
k+=1
if not flag:
primefactor.append(i)
i+=2
return primefactor


if __name__=='__main__':
strart=time.time()
L=findprimefactor(775147)
for num in L[::-1]:
if 600851475143%num==0:
print num
break
else:
print 600851475143
print time.time()-strart



版本二:


这个方法是利用除2外所有的素因数都是奇数的特点来完成,简单而且效率很高,时间为0.002秒



import time
strart=time.time()
d, n = 3, 600851475143
while (d * d &amp;lt; n):
if n % d == 0:
n /= d
else: d += 2
print n
print time.time()-strart

阅读全文 »

Project Euler 4解题报告

题目4:找出由两个三位数乘积构成的回文。 一个回文数指的是从左向右和左右向左读都一样的数字。最大的由两个两位数乘积构成的回文数是9009 = 91 * 99. 找出最大的有由个三位数乘积构成的回文数。 project euler problem 4   代码: [python] #!/usr/bin/env python # -*- coding: utf-8 -*- import time strart=time.time() result=0 for num1 in range(999,99,-1): for num2 in range(num1,99,-1): n=num1*num2 if ''.join(reversed(str(n))) is str(n): if n>result: result=n print result print time.time()-strart [/python]

阅读全文 »

Project Euler 2解题报告

题目2:在斐波那契数列中,找出4百万以下的项中偶数项之和。
斐波那契数列中的每一项被定义为前两项之和。从1和2开始,斐波那契数列的前十项为:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
考虑斐波那契数列中数值不超过4百万的项,找出这些项中偶数项之和。
Project Euler Problem 2   代码:
a=[1,2,3,5,8,13,21,34,55,89]
while a[-1]+a[-2]&lt;4000000:
a.append(a[-1]+a[-2])
sum([i for i in a if not i%2])

阅读全文 »

Project Euler 1解题报告

题目1:找出1000以下自然数中3和5的倍数之和。


10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是23.


找出1000以下的自然数中,属于3和5的倍数的数字之和。


project euler problem 1:



代码:


sum(list(set([i for i in range(1000) if not i%3 or not i%5 ])))




阅读全文 »