Project Euler 29-35
Project Euler: https://projecteuler.net/
Project Euler | 欧拉计划: https://pe-cn.github.io/
Project Euler 1-7
Project Euler 8-14
Project Euler 15-21 & 67
Project Euler 22-28
Project Euler 29-35
Project Euler 36-42
Project Euler 43-49
Project Euler 50-56
Project Euler 57-63
如果有错误,还望指出,欢迎互相交流学习
随缘更新
Problem 29: Distinct powers
How many distinct terms are in the sequence generated by a b a^b ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
- 将 2-100 中每个数化为 最小底数 + 指数 形式
- 将每个组合转换为 底数,指数 组合的形式存到表ANS中对应的位置 实现去重
NUM = 100
NUM_EXP = 600
min_base = []
exp = []
for i in range(NUM+1):
min_base.append(0)
exp.append(0)
# 将 2-100 中每个数化为 最小底数 + 指数 形式
for i in range(2,NUM+1):
if min_base[i]:
continue
min_base[i] = i
p = 0
j = i
while j<=NUM:
min_base[j] = i
p+=1
exp[j] = p
j*=i
ANS = []
for i in range(NUM+1):
ANS.append([])
for j in range(NUM_EXP+1):
ANS[i].append(0)
# 将每个组合转换为 底数,指数 组合的形式存到表ANS中对应的位置 实现去重
for a in range(2,NUM+1):
for b in range(2,NUM+1):
a1 = min_base[a]
b1 = b*exp[a]
if ANS[a1][b1]==0:
ANS[a1][b1]=1
ans = 0
for i in range(NUM+1):
for j in range(NUM_EXP+1):
ans += ANS[i][j]
print(ans)
9183
Problem 30: Digit fifth powers
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
- 语法
[int(str(num)[j])**5 for j in range(len(str(num)))]
- 最大只要检查到 6 × 9 5 6×9^5 6×95, 7 × 9 5 7×9^5 7×95 仍是6位数
Sum = 0
ans = []
for num in range(2,6*9**5):
if Sum==num-1:
ans.append(Sum)
Sum = sum([int(str(num)[j])**5 for j in range(len(str(num)))])
print(sum(ans))
443839
Problem 31: Coin sums
How many different ways can £2 be made using any number of coins?
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).
- 递归 动态规划
- 递归备忘录实现
# coins = [200,100,50,20,10,5,2,1]
coins = [0,1,2,5,10,20,50,100,200]
dp = []
for i in range(9):
dp.append([])
for j in range(2001):
dp[i].append(0)
def f(n, m):
A = 0
B = 0
if n == 1:
return 1
if m == 0:
return 1
if m < 1:
return 0
if dp[n-1][m]:
A = dp[n-1][m]
else:
dp[n-1][m] = f(n-1, m)
A = dp[n-1][m]
if dp[n][m - coins[n]]:
B = f(n, m - coins[n])
else:
dp[n][m - coins[n]] = f(n, m - coins[n])
B = dp[n][m - coins[n]]
return A + B
f(8,300)
471363
Problem 32: Pandigital products
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
ans = []
for a in range(1, 100):
for b in range(100, 9999):
ab = a*b
s = str(a) + str(b) + str(ab) + '0'
if len(s)!=10:
continue
s = list(set(s))
if len(s)==10 and ab not in ans:
ans.append(ab)
print(sum(ans))
45228
Problem 33: Digit cancelling fractions
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
- 求满足此种情况的分数:
i
k
k
j
=
i
j
\frac {ik}{kj}=\frac {i}{j}
kjik=ji,
k
>
i
时
j
>
i
k>i时j>i
k>i时j>i,
k = i 时 j > k k=i时j>k k=i时j>k
def gcd(a, b): # 辗转求余
if b==0:
return a
return gcd(b, a%b)
a, b = 1, 1
for i in range(1,10):
for j in range(i+1,10):
for k in range(i,10):
if (10*i+k)*j == (10*k+j)*i:
a *= 10*i+k
b *= 10*k+j
print(b//gcd(a, b))
100
Problem 34: Digit factorials
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
- 最容易找到的上界 9 ! × 7 : 9999999 9!×7: 9999999 9!×7:9999999
import math
ans = 0
for i in range(3,math.factorial(9)*7):
if i == sum([math.factorial(int(str(i)[j])) for j in range(len(str(i)))]):
ans += i
print(ans)
40730
Problem 35: Circular primes
How many circular primes are there below one million?
- 初始化素数标记
- 检查每个素数
def is_primes(n):
if n<1:
return False
for j in range(2,int(n**0.5)+1):
if n%j==0:
return False
else:
return True
def check(k):
for i in range(len(str(k))):
if primes[k]==False:
return False
else:
k = k//10 + k%10*10**(len(str(k))-1)
return True
primes = [0,0]
for i in range(2, 1000001):
if is_primes(i):
primes.append(1)
else:
primes.append(0)
ans = 0
for i in range(2,1000001):
if check(i):
ans += 1
print(ans)
55