Project Euler 31~35

本文详细解答了LeetCode中的三个数学与编程挑战题,包括如何使用多种硬币组合成指定金额,寻找满足特定条件的九位数拼接,以及计算满足特定条件的圆周率。通过Python代码实现,深入理解算法与逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 31:

In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).

It is possible to make £2 in the following way:

1 ×£1 + 1 ×50p + 2 ×20p + 1 ×5p + 1 ×2p + 3 ×1p

How many different ways can £2 be made using any number of coins?

#!/usr/bin/python3
ans=0
for i in range(200//1+1):
	for j in range((200-i)//2+1):
		for k in range((200-i-j*2)//5+1):
			for l in range((200-i-j*2-k*5)//10+1):
				for m in range((200-i-j*2-k*5-l*10)//20+1):
					for n in range((200-i-j*2-k*5-l*10-m*20)//50+1):
						for o in range((200-i-j*2-k*5-l*10-m*20-n*50)//100+1):
							for p in range((200-i-j*2-k*5-l*10-m*20-n*50-o*100)//200+1):
								if i+j*2+k*5+l*10+m*20+n*50+o*100+p*200==200:
									ans+=1
print(ans)

Answer:73682


Problem 32:

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
#!/usr/bin/python3
S=set()
for i in range(1,10):
	for j in range(1234,10000):
		k=i*j
		if k>=1234 and k<10000:
			T=set()
			ii,jj,kk=i,j,k
			f=1
			while ii>0 and f:
				if ii%10 in T:
					f=0
					break
				T.add(ii%10)
				ii//=10
			while jj>0 and f:
				if jj%10 in T:
					f=0
					break
				T.add(jj%10)
				jj//=10
			while kk>0 and f:
				if kk%10 in T:
					f=0
					break
				T.add(kk%10)
				kk//=10
			for l in range(1,10):
				if l not in T:
					f=0
					break
			if f:
				S.add(k)
for i in range(12,100):
	for j in range(123,1000):
		k=i*j
		if k>=1234 and k<10000:
			T=set()
			ii,jj,kk=i,j,k
			f=1
			while ii>0 and f:
				if ii%10 in T:
					f=0
					break
				T.add(ii%10)
				ii//=10
			while jj>0 and f:
				if jj%10 in T:
					f=0
					break
				T.add(jj%10)
				jj//=10
			while kk>0 and f:
				if kk%10 in T:
					f=0
					break
				T.add(kk%10)
				kk//=10
			for l in range(1,10):
				if not l in T:
					f=0
					break
			if f:
				S.add(k)
ans=0
for i in S:
	ans+=i
print(ans)
Answer:45228

Problem 33:

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.

#!/usr/bin/python3
from fractions import *
ans=Fraction(1,1)
for i in range(10,100):
	for j in range(i+1,100):
		a,b,c,d=i//10,i%10,j//10,j%10
		if b==0 and d==0:
			continue
		f=0
		A=Fraction(i,j)
		if a==c and d!=0:
			if Fraction(b,d)==A:
				f=1
		if a==d and c!=0:
			if Fraction(b,c)==A:
				f=1
		if b==c and d!=0:
			if Fraction(a,d)==A:
				f=1
		if b==d and c!=0:
			if Fraction(a,c)==A:
				f=1
		if f:
			ans*=A
print(ans.denominator)
Answer:100

Problem 34:

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

#!/usr/bin/python3
f=[0 for i in range(10)]
f[0]=1
ans=0
for i in range(1,10):
	f[i]=f[i-1]*i
for i in range(10,10000000):
	t,tot=i,0
	while t>0:
		tot+=f[t%10]
		t//=10
	if tot==i:
		ans+=i
print(ans)
Answer:40730

Problem 35:

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

#!/usr/bin/python3
f=[0 for i in range(1000000+10)]
p=[0 for i in range(1000000+10)]
tot,ans=0,0
for i in range(2,1000000):
	if f[i]==0:
		p[tot]=i
		tot+=1
		j=i+i
		while j<=1000000:
			f[j]=1
			j+=i
for i in range(tot):
	t=p[i]
	g=1
	l=len(str(t))
	for j in range(l):
		k=t%10
		t//=10
		t=k*pow(10,l-1)+t
		if f[t]:
			g=0
			break
	if g:
		ans+=1
print(ans)
Answer:55

By Charlie Pan
Apr 17,2014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值