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/2 = 0.5 1/3 = 0.(3) 1/4 = 0.25 1/5 = 0.2 1/6 = 0.1(6) 1/7 = 0.(142857) 1/8 = 0.125 1/9 = 0.(1) 1/10 = 0.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