Project Euler 46~50

Problem 46:

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

9 = 7 + 2×12
15 = 7 + 2×22
21 = 3 + 2×32
25 = 7 + 2×32
27 = 19 + 2×22
33 = 31 + 2×12

It turns out that the conjecture was false.

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

#!/usr/bin/python3
tot=0
f=[0 for i in range(1000000+10)]
p=[0 for i in range(1000000+10)]
f[0]=f[1]=1
for i in range(2,1000000):
	if not f[i]:
		p[tot]=i
		tot+=1
		j=i+i
		while j<1000000:
			f[j]=1
			j+=i
for i in range(2,1000000):
	if f[i] and i%2:
		j=g=0
		while not g:
			j+=1
			if 2*j*j>=i:
				break
			if not f[i-2*j*j]:
				g=1
				break
		if not g:
			print(i)
			break

Answer:5777


Problem 47:

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7
15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors are:

644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.

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

#!/usr/bin/python3
from math import *
for i in range(1,100000000):
    tot1=tot2=tot3=tot4=0
    t=i
    for j in range(2,int(sqrt(t))+1):
        if t%j==0:
            tot1+=1
            while t%j==0:
                t//=j
    if t!=1:
        tot1+=1
    t=i+1
    for j in range(2,int(sqrt(t))+1):
        if t%j==0:
            tot2+=1
            while t%j==0:
                t//=j
    if t!=1:
        tot2+=1
    t=i+2
    for j in range(2,int(sqrt(t))+1):
        if t%j==0:
            tot3+=1
            while t%j==0:
                t//=j
    if t!=1:
        tot3+=1
    t=i+3
    for j in range(2,int(sqrt(t))+1):
        if t%j==0:
            tot4+=1
            while t%j==0:
                t//=j
    if t!=1:
        tot4+=1
    if tot1==4 and tot2==4 and tot3==4 and tot4==4:
        print(i)
        break

Answer:134043


Problem 48:

The series, 11 + 22 + 33 + ... + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.

#!/usr/bin/python3
ans=0
for i in range(1,1000+1):
	ans+=i**i
ans=str(ans)
for i in range(len(ans)-10,len(ans)):
	print(ans[i],end='')
print()

Answer:9110846700


Problem 49:

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.

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

#!/usr/bin/python3
f=[0 for i in range(1000000+10)]
p=[0 for i in range(1000000+10)]
tot=0
for i in range(2,10000):
	if not f[i]:
		p[tot]=i
		tot+=1
		j=i+i
		while j<10000:
			f[j]=1
			j+=i
F=0
for i in range(tot):
	if len(str(p[i]))==4 and p[i]>1487:
		s=str(p[i])
		a=[0 for j in range(4)]
		for j in range(4):
			a[j]=int(s[j])
		for j in range(4):
			for k in range(4):
				for l in range(4):
					for m in range(4):
						if j!=k and j!=l and j!=m and k!=l and k!=m and l!=m:
							t=a[j]*1000+a[k]*100+a[l]*10+a[m]
							if not f[t] and t>p[i]:
								delta=t-p[i]
								t1=t+delta
								if not f[t1] and len(str(t1))==4:
									s1=str(t1)
									G=1
									for n in range(len(s1)):
										if not int(s1[n]) in a:
											G=0
											break
									if G:
										print(str(p[i])+str(t)+str(t1))
										F=1
						if F:
							break
					if F:
						break
				if F:
					break
			if F:
				break

Answer:296962999629


Problem 50:

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

#!/usr/bin/python3
f=[0 for i in range(1000000+10)]
g=[0 for i in range(1000000+10)]
s=[0 for i in range(1000000+10)]
tot=0
for i in range(2,1000000):
	if not f[i]:
		tot+=1
		g[tot]=i
		j=i+i
		while j<1000000:
			f[j]=1
			j+=i
for i in range(1,tot+1):
	s[i]=s[i-1]+g[i]
for mid in range(1000,0,-1):
	F=0
	for i in range(1,tot-mid+2):
		t=s[i+mid-1]-s[i-1]
		if t<1000000:
			if not f[t]:
				F=t
				break
	if F:
		print(F)
		break

Answer:997651


By Charlie Pan

Apr 25,2014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值