Project Euler

Progress

Problem 1 Multiples of 3 or 5

# Find the sum of all the multiples of 3 or 5 below 1000.
res = 0
for i in range(1000):
    if i%3 == 0 or i%5==0:
        res += i
print(res)

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

Problem 2 Even Fibonacci numbers

# 通过考虑 Fibonacci 数列中值不超过 400 万的项,找出偶数项的总和。
a, b, s =1, 2, 0

while a < 4000000:   
    if not a % 2:
        s += a
    a, b = b, a + b   

# 4613732
print(s)

Problem 3 Largest prime factor

# What is the largest prime factor of the number 600851475143 ?
x = 600851475143
for i in range(int(x**0.5)+1,1,-1):
    if not x%i and all(i%j for j in range(2, int(i**0.5)+1)):
        print(i)
        # break
            
# 6857, 1471, 839, 71

Problem 4 Largest palindrome product

# 找出由两个 3 位数字的乘积构成的最大回文数。
# 100-999
res = 1
for i in range(999,99,-1):
    for j in range(i-1,99,-1):
        if res < i*j and str(i*j)==str(i*j)[::-1]:
            res = max(res,i*j)
            print(i,j,i*j)
# 995 583 580085
# 993 913 906609
res

Problem 5 Smallest multiple

# 能被 1 到 20 的所有数整除的最小正数是多少?
# Python 最小公倍数算法
# 按部就班法,从大的那个数(理论上,实际可以从任意一个数或者从正整数开始)开始一个个验证是否可以同时整除a和b,如果找到则跳出循环,没找到则加一继续找。
def lcm(x, y): # 很慢
    greater = max(x,y)
    while greater % x or greater % y:
        greater += 1
 
    return greater

# 找两数差距法
def f(a,b): # 比较快
    ma = max(a, b)
    mi = min(a, b)
    for i in range(1, mi+1):
        if ma * i % mi == 0:
            return ma * i 
#             break
def leastCommonMultiple(a, b):
    if a < b:
        a, b = b, a

    for i in range(1, b+1):
        if a * i % b == 0:
            return a * i
#             break

num = list(range(21))
x = 2
for i in range(2, 21):
    # x = lcm(x,i)
    x = f(x, i)
#     print('%d'%(a*b/[x for x in range(1,a+1) if a%x==0 and b%x ==0][-1]))
x
# 232792560

Problem 6 Sum square difference

# 求前一百个自然数的平方和与总和的平方之间的差。
t = s = 0
for i in range(1,101):
    s += i*i
    t += i
print(t*t - s)
# 25164150

Problem 7 10001st prime

x = 2
res = 0
while True:
    if all([x%i for i in range(2, int(x**0.5)+1)]):
        res += 1
        if res == 10001:
            print(x)
            break
    x += 1
    # 104743

Problem 8 Largest product in a series

s   = '''
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
'''
s=s.replace('\n','')

# lst = []  # 用来存放13位数字之积的列表
res = 1
p = 1  # 初始化每个13位数字之积
for i in range(1000-12):
    for j in range(13):
        p = p * int(s[i+j])
#     lst.append(p)
    res = max(res, p)
    p = 1  #计算完每一组‘13位数字之积’之后,重置p为1
# print(max(lst))
print(res)
# 23514624000

Problem 9 Special Pythagorean triplet

def f():
    for a in range(1000):
        for b in range(a+1,999):
            for c in range(b+1,998):
                if a<b<c and a+b+c == 1000 and a**2 + b**2 == c**2:
                    print(a,b,c)
                    return a*b*c
f()
# 200 375 425
# 31875000
c=0
while True:
    for b in range(0,c):
        for a in range(0,b):
            if a**2 + b**2 == c**2:
                if a+b+c == 1000:
                    print("a=%d, b=%d, c=%d a*b*c=%d" % (a,b,c,a*b*c))
    c+=1
Used simple paper and pencil. Found the sum of common triplets:

3+4+5=12 which is not a factor of 1000
5+12+13=30 which is not a factor of 1000
8+15+17=40   40*25 = 1000

therefore answer = 25^3 * 8 * 15 * 17

I guess I got lucky because those are the only 3 Pythagorean triplets I know.
[(x,y,1000-x-y) for x in range(1,1000) for y in range(1,x) if x**2+y**2==(1000-x-y)**2]

Problem 10 Summation of primes

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

Find the sum of all the primes below two million.

# 找出所有小于 200 万的素数之和。
n = 2000000
isPrime = [1] * n
res = 0
s = 0
for i in range(2, n): # 0, 1, 2 => 0 
    if isPrime[i]:
        res += 1
        s += i
        if i*i < n:
            for j in range(i*i, n, i): # 除去 i 外所有 i 的倍数项置为 0
                isPrime[j] = 0

res
# 148933
s
# 142913828922