Project Euler 50-56
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 50: Consecutive prime sum
Which prime, below one-million, can be written as the sum of the most consecutive primes?
def eratosthenes_sieve(max_number):
sieve = [0] * (max_number + 1)
sieve[0], sieve[1] = -1, -1
for i in range(2, max_number + 1):
if sieve[i] == 0:
for j in range(2 * i, max_number + 1, i):
sieve[j] = 1
return sieve
primes = []
for i, elem in enumerate(eratosthenes_sieve(1000001)):
if elem == 0: # 从题目示例开始查找
primes.append(i)
length = 1
s = primes[0]
while True:
s += primes[length]
if s >= 1000000: break
length += 1
while length > 1:
Sum = sum(primes[:length])
if Sum in primes: break
i = 0
found = False
while not found:
Sum += primes[length + i] - primes[i]
if Sum >= 1000000: break
found = Sum in primes
i += 1
if found: break
length -= 1
print(Sum)
997651
Problem 51: Prime digit replacements
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
- counter: 一个计数器工具提供快速和方便的计数。
https://docs.python.org/zh-cn/3/library/collections.html#collections.Counter
-(1) 至少要是一个四位数,我们可以从数字1111开始搜寻;
-(2) 重复的数位只能是三或三的倍数;
-(3) 重复的数字只能是0, 1, 2三个数。 - 参考https://www.cnblogs.com/metaquant/p/11820821.html
def eratosthenes_sieve(max_number):
sieve = [0] * (max_number + 1)
sieve[0], sieve[1] = -1, -1
for i in range(2, max_number + 1):
if sieve[i] == 0:
for j in range(2 * i, max_number + 1, i):
sieve[j] = 1
return sieve
primes = []
for i, elem in enumerate(eratosthenes_sieve(1000001)):
if elem == 0 and i>1111:
primes.append(i)
from collections import Counter
def is_replacable_prime(n):
s = str(n)
count = Counter(s)
num,d = count.most_common(1)[0]
if d % 3 == 0 and num in set('012'):
k = 1
for j in range(int(num)+1,10):
new = s.replace(num,str(j))
if int(new) in primes:
k += 1
if k == 8:
return True
return False
def main():
for p in primes:
if is_replacable_prime(p):
return p
main()
121313
Problem 52: Permuted multiples
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
for a in range(1,1000000):
b = sorted(str(a))
for i in range(2, 7):
if len(str(a*i)) > len(b):
break
if b != sorted(str(a*i)):
break
else:
print(a)
break
142857
Problem 53: Combinatoric selections
How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million?
from itertools import combinations
import math
def C(n,m): # 计算组合数
return math.factorial(n)//(math.factorial(m)*math.factorial(n-m))
ans = 0
for i in range(23,101):
for j in range(1,i):
if C(i,j)>1000000:
ans += 1
print(ans)
4075
Problem 54:Poker hands
How many hands does Player 1 win?
- 参考j123 https://projecteuler.net/best_posts=054
T=(([1],[3,1.5]),([3,1.7],[5]))# 烂牌,顺子,同花,同花顺
def rank(H, v=dict(zip(b'23456789TJQKA', range(13)))):
C,V,S = zip(*reversed(sorted([(H.count(c), v[c], s) for c, s in H.split()])))
return ([C[0], C[C[0]]] if C[0]>1 else T[len(set(S))==1][V[0]-V[4]==4]), V
print(sum([rank(s[:14]) > rank(s[15:]) for s in open('data/p054_poker.txt', 'rb')]))
376
Problem 55:Lychrel numbers
How many Lychrel numbers are there below ten-thousand?
def is_palindrome(n):
n = str(n)
if n == n[::-1]:
return True
return False
def is_lychrel(n):
for i in range(50):
n += int(str(n)[::-1])
if is_palindrome(n):
return False
return True
ans = 0
for i in range(1, 10001):
if is_lychrel(i):
ans += 1
print(ans)
249
Problem 56: Powerful Digit Sum
Considering natural numbers of the form, a b a^b ab, where a, b < 100, what is the maximum digital sum?
- 大数乘法
ans = []
for a in range(2,100):
NUM = [1]
carry = 0
for b in range(2,100):
carry = 0
for j in range(len(NUM)):
temp = NUM[j] * a + carry
if temp>9:
NUM[j] = temp%10
carry = temp//10
else:
carry = 0
NUM[j] = temp
while carry>0:
temp = carry
NUM.append(temp%10)
carry = temp//10
ans.append(sum(NUM))
print(max(ans))
972