欧拉计划 Project Euler 43-49

Project Euler 43-49

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 43:Sub-string divisibility

Find the sum of all 0 to 9 pandigital numbers with this property.

  • python itertools为高效循环而创建迭代器的函数https://docs.python.org/zh-cn/3/library/itertools.html#itertools.permutations
  • itertools.permutations(iterable, r=None):连续返回由 iterable 元素生成长度为 r 的排列。
from itertools import permutations
primes = [2, 3, 5, 7, 11, 13, 17]

def is_div(a):
    for i in range(1,8):
        if (a[i]*100+a[i+1]*10+a[i+2]) % primes[i-1] !=0:
            return False
    return True

ans=[]
for x in permutations(range(10)):
    if is_div(x):
        ans.append(int(''.join(map(str,x))))
print(sum(ans))
16695334890

Problem 44:Pentagon numbers

Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk − Pj| is minimised; what is the value of D?

  • Pn=n(3n−1)/2
Pn  = set(i*(3*i-1)//2 for i in range(1,3000))
ans = []
for i in Pn:
    for j in Pn:
        if i < j: continue
        if abs(i-j) in Pn and i+j in Pn:
            ans.append(abs((i-j)))
print(min(ans))
5482660

Problem 45:Triangular, pentagonal, and hexagonal

Find the next triangle number that is also pentagonal and hexagonal.

  • It can be verified that T 285 = P 165 = H 143 = 40755 T_{285} = P_{165} = H_{143} = 40755 T285=P165=H143=40755.
t = 285
p = 165
h = 144
def T(n):
    return n*(n-1)//2
def P(n):
    return n*(3*n-1)//2
def H(n):
    return n*(2*n-1)
while(1):
    if H(h)==P(p) and P(p)==T(t):
        print(H(h))
        break
    if T(t+1)>P(p):
        p+=1
        if P(p)>H(h):
            h+=1
    else: t+=1
1533776805

Problem 46:Goldbach’s other conjecture

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

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
odd_composite = []
for i in range(1,5800,2):
    if is_primes(i):
        _
    else: odd_composite.append(i)
        
for num in odd_composite:
    i = 1
    flag = 0
    while(1):
        if 2*i**2>num:
            break
        if is_primes(num-2*i**2):
            flag = 0
            break
        else:
            flag = 1
        i+=1
    if flag == 1:
        print(num)
5777

Problem 47:Distinct primes factors

Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?

  • 埃拉托斯特尼筛法:简称埃氏筛,也称素数筛。所使用的原理是从2开始,将每个素数的各个倍数,标记成合数。一个素数的各个倍数,是一个差为此素数本身的等差数列。
  • 建立一个list每个位置保存该下标上的数字成为不同素数倍数的次数,及为该数字的质因数个数
# 法一 暴力求解
def find_factors(num):
    fac = set()
    i = 1
    while(i < num+1):
        i+=1
        if num%i==0:
            num = num//i
            fac.add(i)
            i = 1 
    return fac

n = 2 * 3 * 5 * 7
i = n
while n - i < 3:
    n += 1
    if len(find_factors(n)) != 4:
        i = n + 1
print(i)
134043
# 法二 利用素数筛
def upgraded_eratosthenes_sieve(max_number, digits):
    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
    for it, elem in enumerate(sieve):
        if elem == digits:
            sieve[it] = '1'
        else:
            sieve[it] = '0'
    return ''.join(sieve)

N = 4
limit = 10000
while True:
    try:
        print(upgraded_eratosthenes_sieve(limit, N).index('1' * N))
    except ValueError:
        limit *= 2
    else:
        break
134043

Problem 48:Self powers

Find the last ten digits of the series, 1 1 + 2 2 + 3 3 + … + 100 0 1000 1^1 + 2^2 + 3^3 + … + 1000^{1000} 11+22+33++10001000.

def fuc(N):
    carry = 0
    for k in range(1,11):
        temp = N[-k] + carry
        if temp>9:
            N[-k] = temp%10
            carry = temp//10
        else:
            carry = 0
            N[-k] = temp
    return N 

SUM = [0]*10
for i in range(1,1001):
    NUM = [0]*10
    NUM[-1] = i
    for j in range(i-1):
        NUM = [n*i for n in NUM]
        NUM = fuc(NUM)
    for s in range(10):
        SUM[s] += NUM[s]
    SUM = fuc(SUM)
SUM
[9, 1, 1, 0, 8, 4, 6, 7, 0, 0]

Problem 49:Prime permutations

What 12-digit number do you form by concatenating the three terms in this sequence?

  • The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

  • There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

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(9999)):
    if elem == 0 and len(str(i))==4 and i>1487: # 从题目示例开始查找
        primes.append(i)
for n in primes:
    term1, term2, term3 = n, n+3330, n+6660 
    if (term2 in primes) and (term3 in primes) and\
        set(list(str(term1))) == set(list(str(term2))) and\
        set(list(str(term1))) == set(list(str(term3))): 
            print(str(term1)+str(term2)+str(term3))
296962999629
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值