欧拉计划 Project Euler 57-63

Project Euler 57-63

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 57:Square root convergents

In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

def func(n):
    a = 1
    b = 2
    for i in range(n-1):
        a, b = b, a+2*b
    return a+b, b
ans = 0
for i in range(1, 1001):
    a, b = func(i)
    if len(str(a))> len(str(b)):
        ans += 1
ans
153

Problem 58: Spiral primes

If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

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
    
gap = 2
flag = 0
num = 1
num_p = 0
counter = 1
while True:
    counter += 1
    if is_primes(num):
        num_p += 1
    if num_p/counter < 0.1 and num_p != 0:
        print(gap+1)
        break
    if flag == 4:
        gap += 2
        flag = 0
    num += gap
    flag += 1

26241

Problem 59: XOR decryption

Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher.txt (right click and ‘Save Link/Target As…’), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.

  • 假设出现频率最高的字符为空格
  • 每个密钥字符加密最多的字符为空格,反解出密钥
f = open('data/p059_cipher.txt')
cipher = f.readlines()
f.close()

cipher = cipher[0].split(',')
for i in range(len(cipher)):
    cipher[i] = int(cipher[i])
    
ciphers = [cipher[::3], cipher[1::3],cipher[2::3]]
key = [int(max(c, key=c.count))^ord(' ') for c in ciphers]

for i in range(len(ciphers)):
    ciphers[i] = [key[i]^c for c in ciphers[i]]
    
sum([sum(cipher) for cipher in ciphers])
129448

Problem 60: Prime pair sets

Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

  • 新判断素数的方式, 基于由素数筛生成的素数集合
def is_primes(n):
    for i in primes:
        if i*i > n:
            return True
        if n % i == 0:
            return False
    return True
def f(a, b):
    x = int(str(a) + str(b))
    y = int(str(b) + str(a))
    for i in primes:
        if i*i > max(x, y):
            return True
        if x % i == 0 or y % i == 0:
            return False
    return True

def find():
    for a in range(len(primes)):
        A = primes[a]
        for b in range(a + 1, len(primes)):
            B = primes[b]
            if f(A, B):
                for c in range(b + 1, len(primes)):
                    C = primes[c]
                    if f(A, C) and f(B, C):
                        for d in range(c + 1, len(primes)):
                            D = primes[d]
                            if f(A, D) and f(B, D) and f(C, D):
                                for e in range(d + 1, len(primes)):
                                    E = primes[e]
                                    if f(A, E) and f(B, E) and f(C, E) and f(D, E):
                                        return A, B, C, D, E
sum(find())
26033

Problem 61:Cyclical figurate numbers

Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.

def Triangle(n):
    return n*(n+1)//2
def Square(n):
    return n*n
def Pentagonal(n):
    return n*(3*n-1)//2
def Hexagonal(n):
    return n*(2*n-1)
def Heptagonal(n):
    return n*(5*n-3)//2
def Octagonal(n):
    return n*(3*n-2)
nums = [[],[],[],[],[],[]]
i=0
while len(str(Triangle(i)))<=4:
    if len(str(Triangle(i)))==4:
        nums[0].append(Triangle(i))
    i+=1
i=0
while len(str(Square(i)))<=4:
    if len(str(Square(i)))==4:
        nums[1].append(Square(i))
    i+=1
i=0
while len(str(Pentagonal(i)))<=4:
    if len(str(Pentagonal(i)))==4:
        nums[2].append(Pentagonal(i))
    i+=1
i=0
while len(str(Hexagonal(i)))<=4:
    if len(str(Hexagonal(i)))==4:
        nums[3].append(Hexagonal(i))
    i+=1
i=0
while len(str(Heptagonal(i)))<=4:
    if len(str(Heptagonal(i)))==4:
        nums[4].append(Heptagonal(i))
    i+=1
i=0
while len(str(Octagonal(i)))<=4:
    if len(str(Octagonal(i)))==4:
        nums[5].append(Octagonal(i))
    i+=1
ans = [0 for i in range(6)]
vis = [False for i in range(6)]
ans = []
vis = []
for i in range(6):
    vis.append(False)
    ans.append(0)
found = False
def dfs(deep):
    global found
    if found == True: return
    if deep == 6:
        if ans[5]%100 == ans[0]//100:
            found = True
            print(sum(ans))
            return
    for i in range(6):
        if vis[i] == False:
            for j in range(len(nums[i])):
                tmp = nums[i][j]
                if deep==0 or tmp//100==ans[deep-1]%100:
                    vis[i] = True
                    ans[deep] = tmp
                    dfs(deep+1)
                    vis[i] = False
dfs(0)
28684

Problem 62: Cubic permutations

Find the smallest cube for which exactly five permutations of its digits are cube.

nums = [[],[],[],[],[],[],[],[],[],[],[],[],[]]
for i in range(1,9999):
    num = pow(i,3)
    nums[len(str(num))-1].append(num)

dic = {}
for ns in nums[5:]:
    for n in ns:
        if ''.join(sorted(str(n))) in dic:
            dic[''.join(sorted(str(n)))] += 1
        else:
            dic[''.join(sorted(str(n)))] = 1
            
for ns in nums[5:]:
    for n in ns:
        if dic.get(''.join(sorted(str(n)))) == 5:
            print(n)
            break
127035954683

Problem 63: Powerful digit counts

How many n-digit positive integers exist which are also an nth power?

ans = 0
for i in range(1,1000):
    for j in range(1, 1000):
        l = len(str(pow(j, i)))
        if l>i:break
        if l==i:
            ans += 1
print(cnt)
49
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值