Project Euler 36~40

Problem 36:

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)

#!/usr/bin/python3
ans=0
a=[0 for i in range(100000)]
b=[0 for i in range(100000)]
for i in range(1000000):
	cnt1=cnt2=0
	t=i
	while t>0:
		a[cnt1]=t%10
		cnt1+=1
		t//=10
	t=i
	while t>0:
		b[cnt2]=t%2
		cnt2+=1
		t//=2
	f1=f2=1
	for j in range(cnt1//2):
		if a[j]!=a[cnt1-j-1]:
			f1=0
			break
	for j in range(cnt2//2):
		if b[j]!=b[cnt2-j-1]:
			f2=0
			break
	if f1 and f2:
		ans+=i
print(ans)

Answer:872187


Problem 37:

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

#!/usr/bin/python3
f=[0 for i in range(1000000+10)]
p=[0 for i in range(1000000+10)]
tot=ans=cnt=0
f[0]=f[1]=1
for i in range(2,1000000+1):
	if not f[i]:
		p[tot]=i
		tot+=1
		j=i+i
		while j<=1000000:
			f[j]=1
			j+=i
g=[0 for i in range(20)]
g[0]=1
for i in range(1,20):
	g[i]=g[i-1]*10
for i in range(tot):
	if len(str(p[i]))==1:
		continue
	flag=1
	for j in range(1,len(str(p[i]))):
		t=p[i]%g[j]
		if f[t]:
			flag=0
			break
	if flag:
		t=p[i]//10
		while t>0:
			if f[t]:
				flag=0
				break
			t//=10
	if flag:
		ans+=p[i]
		cnt+=1
		if cnt==11:
			break
print(ans)

Answer:748317


Problem 38:

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192
192 × 2 = 384
192 × 3 = 576

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?

#!/usr/bin/python3
ans=''
for i in range(1,100000):
	s=''
	cnt=0
	while len(s)<9:
		cnt+=1
		s+=str(i*cnt)
	if len(s)>9:
		continue
	f=[0 for i in range(10)]
	for j in range(9):
		f[ord(s[j])-ord('0')]=1
	g=1
	for j in range(1,10):
		if not f[j]:
			g=0
			break
	if g and s>ans:
		ans=s
print(ans)

Answer:932718654


Problem 39:

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

#!/usr/bin/python3
from math import *
m=w=0
for p in range(1,1000+1):
	ans=0
	for i in range(1,p):
		for j in range(i+1,p-i):
			k=p-i-j
			if i*i+j*j==k*k:
				ans+=1
	if ans>m:
		m=ans
		w=p
print(w)

Answer:840


Problem 40:

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

#!/usr/bin/python3
ind=l=0
ans=1
while l<1000000:
	ind+=1
	s=str(ind)
	for i in range(len(s)):
		l+=1
		if l==1 or l==10 or l==100 or l==1000 or l==10000 or l==100000 or l==1000000:
			ans*=ord(s[i])-ord('0')
print(ans)

Answer:210


By Charlie Pan

Apr 23,2014

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值