Project Euler 22-28
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 22: Names score
What is the total of all the name scores in the file?
- ord(): 返回对应的 ASCII 数值
f = open('data/name.txt')
names = f.read()
f.close()
names = names[1:len(names)-1]
names = names.split('","')
names = sorted(names)
score = 0
for i in range(len(names)):
Sum = 0
for j in range(len(names[i])):
Sum += ord(names[i][j]) - 64
score += (i+1) * Sum
print(score)
871198282
Problem 23: Non-abundant sums
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
- 生成小于28123的所有盈数
- 生成1-28123所有的非盈数flag
Non_Abundant = []
for i in range(28124):
Non_Abundant.append(True)
Abundant = []
for i in range(28124):
temp = []
for j in range(1,int(i*0.5)+1):
if i%j==0:
temp.append(j)
if sum(temp)>i:
Abundant.append(i)
# print(Abundant)
Sum = 0
for numa in Abundant:
for numb in Abundant:
Sum = numa+numb
if Sum<28124:
Non_Abundant[Sum] = False
else:
break
Sum = 0
for i in range(len(Non_Abundant)):
if Non_Abundant[i]:
Sum += i
print(Sum)
4179871
Problem 24: Lexicographic permutations
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
- 第0位排列为0123456789
import math
def A(n,m): #排列数
return math.factorial(n)//math.factorial(n-m)
n = 1000000-1
nums = [0,1,2,3,4,5,6,7,8,9]
ans = ''
for i in range(9,0,-1):
num = n//A(i,i)
n = n%A(i,i)
ans += str(nums[num])
del nums[num]
ans += str(nums[0])
print(ans)
2783915460
Problem 25: 1000-digit Fibonacci number
What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
- 利用数组保存大数,定义大数加法add()
def add(n1, n2):
Sum = []
carry = 0
ln1, ln2 = len(n1),len(n2)
if (ln2>ln1):
n1, n2 = n2, n1
for i in range(1, min(len(n1),len(n2))+1):
temp = n1[-i] + n2[-i] + carry
Sum.insert(0,temp%10)
carry = temp//10
if ln1>ln2:
n1 = n1[:ln1-ln2]
for i in range(1, ln1-ln2+1):
temp = n1[-i] + carry
Sum.insert(0,temp%10)
carry = temp//10
if carry>0:
Sum.insert(0,carry)
return Sum
F = [[1],
[1]]
while len(F[-1])<1000:
F.append(add(F[-1],F[-2]))
print(len(F))
4782
Problem 26: Reciprocal cycles
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
- 寻找循环结;比较长度
def find_recurring_cycle(n):
num = []
remainder = []
temp = 10//n
num.append(temp)
remainder.append(10%n*10)
while 1:
num.append(remainder[-1]//n)
temp = remainder[-1]%n*10
if temp in remainder:
break
remainder.append(temp)
remainder.append(temp)
for i in range(2, len(remainder)+1):
if remainder[-i] == remainder[-1]:
num = num[-i+1:]
return num
find_recurring_cycle(31)
[3, 2, 2, 5, 8, 0, 6, 4, 5, 1, 6, 1, 2, 9, 0]
ans = 1
MAX = []
for i in range(1,1000):
temp = find_recurring_cycle(i)
if len(temp)>len(MAX):
ans = i
MAX = temp
ans
983
Problem 27: Quadratic primes
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.
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
is_primes(4)
False
def f_quadratic(n,a,b):
return n**2+a*n+b
f_quadratic(41,1,41)
1763
ans = (0,0,0)
for a in range(-1000, 1000):
for b in range(-1000, 1000):
n = 0
while 1:
if is_primes(f_quadratic(n,a,b)):
ans = (n,a,b) if n>ans[0] else ans
else:
break
n+=1
print(ans)
print(ans[2]*ans[1])
(70, -61, 971)
-59231
Problem 28: Number spiral diagonals
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
gap = 2
flag = 0
nums = [1]
while nums[-1] < 1001*1001:
nums.append(nums[-1] + gap)
flag += 1
if flag == 4:
gap += 2
flag = 0
print(sum(nums))
669171001