Project Euler 21~30
Problem 21
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
Answer:
|
31626 |
-
#!/usr/bin/env python
-
import math
-
def Findsum(num):
-
sum=1
-
for i in range(2,int(math.sqrt(num)+1)):
-
if num%i==0:
-
sum+=i
-
if i!=num/i:
-
sum+=num/i
-
return sum
-
-
def JlFindsum():
-
result={}
-
for i in range(10000):
-
result[i]=Findsum(i)
-
return result
-
-
def main():
-
answer=0
-
result=JlFindsum()
-
for i in range(1,10000):
-
if result[i]<10000 and i!=result[i] and i==result[result[i]]:
-
answer+=i
-
print answer
-
-
if __name__=="__main__":
-
main()
Problem 22
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 53 = 49714.
What is the total of all the name scores in the file?
Answer:
|
871198282 |
-
#!/usr/bin/env python
-
-
def worth(word):
-
return sum(ord(letter) - ord('A') + 1 for letter in word)
-
-
def main():
-
ilist=open("names.txt").read().replace('"','').split(',')
-
ilist.sort()
-
print sum((i+1) * worth(ilist[i]) for i in xrange(0, len(ilist)))
-
-
if __name__ == "__main__":
-
main()
Problem 23
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
Answer:
|
4179871 |
-
#!/usr/bin/env python
-
-
import math
-
-
def sumProperDivisors(n):
-
sum = 1
-
for i in xrange(2, int(math.sqrt(n)) + 1):
-
if n % i ==0:
-
sum += i
-
if n / i != i:
-
sum += n / i
-
return sum
-
-
def getAbundantList(n):
-
list = []
-
for i in xrange(2, n):
-
if sumProgerDivisors(i) > i:
-
list.append(i)
-
return list
-
-
abundants = set()
-
-
def isSumOfTwoAbundants(n):
-
return any((n-a in abundants) for a in abundants)
-
-
def goodway(n):
-
total = 0
-
for i in xrange(1, n):
-
if sumProperDivisors(i) > i:
-
abundants.add(i)
-
if not isSumOfTwoAbundants(i):
-
total += i
-
return total
-
-
if __name__ == "__main__":
-
print goodway(28123)
Problem 25
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain 1000 digits?
Answer:
|
4782 |
-
#!/usr/bin/env python
-
-
def con_fib(num):
-
f1=1
-
f2=1
-
count=2
-
while f2 < 10**(num-1):
-
f1,f2 = f2,f1+f2
-
count+=1
-
return count
-
-
if __name__ == "__main__":
-
print con_fib(1000)
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.
Answer:
|
983 |
-
#!/usr/bin/env python
-
-
def cycle_length(n):
-
i = 1
-
if n % 2 == 0:
-
return cycle_length(n / 2)
-
if n % 5 == 0:
-
return cycle_length(n / 5)
-
if n == 1:
-
return 0
-
while True:
-
if (pow(10, i) - 1) % n == 0:
-
return i
-
else:
-
i = i + 1
-
-
m, n=0, 0
-
for i in xrange(2, 1000):
-
c = cycle_length(i)
-
if c > m:
-
m, n = c, i
-
print n
Problem 27
Euler published 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.
Using computers, 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.
Answer:
|
-59231 |
-
#!/usr/bin/env python
-
-
import math
-
-
def isprime(n):
-
if n < 2:
-
return False
-
for i in range(2, int(math.sqrt(n) + 1)):
-
if n % i ==0:
-
return False
-
return True
-
-
ilist=[i for i in xrange(2, 1000) if isprime(i)]
-
-
def max_prime_product():
-
max_pair = (0, 0, 0)
-
for a in xrange(-999, 1000, 2):
-
for b in ilist:
-
n, count = 0, 0
-
while True:
-
v = n * n + a * n + b
-
if isprime(v):
-
count += 1
-
else:
-
break
-
n += 1
-
if count > max_pair[2]:
-
max_pair = (a, b, count)
-
return max_pair[0] * max_pair[1]
-
-
if __name__ == "__main__":
-
print max_prime_product()
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 both diagonals is 101.
What is the sum of both diagonals in a 1001 by 1001 spiral formed in the same way?
Answer:
|
669171001 |
-
#!/usr/bin/env python
-
-
def sum_spirial(n):
-
sum = 1 #here add 1
-
for i in xrange(3, n + 1, 2):
-
num = i * i
-
sum += 4 * num - 6 * (i - 1) #sum += num + (num -(i -1)) + (num - (i - 1) -(i - 1)) + (num - (i - 1) -(i -1) -(i - 1))
-
return sum
-
-
if __name__ == "__main__":
-
print sum_spirial(1001)
Problem 29
Consider all integer combinations of ab for 2 a 5 and 2 b 5:
22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=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?
Answer:
|
9183 |
-
#!/usr/bin/env python
-
ilist = [x ** y for x in xrange(2, 101) for y in xrange(2, 101)]
-
ilist = set(ilist)
-
print len(ilist)
Problem 30
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
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.
Answer:
|
443839 |
-
#!/usr/bin/env python
-
-
def power_digits(n, p):
-
s = 0
-
while n > 0:
-
d = n % 10
-
n = n / 10
-
s = s + pow(d, p)
-
return s
-
max_num = 9 ** 5 * 6 #m >= 6, p > m * (9 ** 5)
-
print sum( n for n in xrange(2, max_num) if power_digits(n, 5) == n)