Project Euler 26~30

Problem 26:

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

1/20.5
1/30.(3)
1/40.25
1/50.2
1/60.1(6)
1/70.(142857)
1/80.125
1/90.(1)
1/100.1

Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

#!/usr/bin/python3
ans=w=0
mod=[0 for i in range(1000+10)]
for i in range(2,1000):
	for j in range(1000):
		mod[j]=0
	t=1
	cnt=0
	while t!=0:
		t*=10
		t%=i
		cnt+=1
		if mod[t]==0:
			mod[t]=cnt
		else:
			if cnt-mod[t]>ans:
				ans=cnt-mod[t]
				w=i
			break
print(w)

Answer:983


Problem 27:

Euler discovered the remarkable quadratic formula:

n² + n + 41

It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41² + 41 + 41 is clearly divisible by 41.

The incredible formula  n² − 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

n² + an + b, where | a| < 1000 and | b| < 1000

where | n| is the modulus/absolute value of n
e.g. |11| = 11 and | −4| = 4

Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.

#!/usr/bin/python3
from math import *
def f(n,a,b):
	return n*n+a*n+b
def p(n):
	for i in range(2,int(sqrt(n))+1):
		if n%i==0:
			return 0
	return 1
w=ans=0
for i in range(-999,1000):
	for j in range(-999,1000):
		n=0
		while 1:
			t=f(n,i,j)
			if t<2:
				break
			if p(t)==0 :
				break
			n+=1
		if n>ans:
			ans=n
			w=i*j
print(w)

Answer:-59231


Problem 28:

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

#!/usr/bin/python3
ans=ind=1
step=2
while ind<1001*1001:
	for i in range(4):
		ind+=step
		if ind>1001*1001:
			break
		ans+=ind
	step+=2
print(ans)

Answer:669171001


Problem 29:

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

2 2=4, 2 3=8, 2 4=16, 2 5=32
3 2=9, 3 3=27, 3 4=81, 3 5=243
4 2=16, 4 3=64, 4 4=256, 4 5=1024
5 2=25, 5 3=125, 5 4=625, 5 5=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

#!/usr/bin/python3
S=set()
for i in range(2,100+1):
	for j in range(2,100+1):
		S.add(pow(i,j))
print(len(S))

Answer:9183


Problem 30:

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1 4 + 6 4 + 3 4 + 4 4
8208 = 8 4 + 2 4 + 0 4 + 8 4
9474 = 9 4 + 4 4 + 7 4 + 4 4

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

#!/usr/bin/python3
ans=0
for i in range(10,1000000):
	temp=i
	tot=0
	while temp>0:
		digit=temp%10
		temp//=10
		tot+=pow(digit,5)
	if tot==i:
		ans+=i
print(ans)

Answer:443839


By Charlie Pan

Apr 16,2014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值