Project Euler 11~20
Project Euler 31~40

Project Euler 21~30

dcy posted @ 2009年6月13日 20:54 in Project Euler , 2061 阅读

Problem 21

05 July 2002

 

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
  1. #!/usr/bin/env python
  2. import math
  3. def Findsum(num):
  4.     sum=1
  5.     for i in range(2,int(math.sqrt(num)+1)):
  6.         if num%i==0:
  7.             sum+=i
  8.             if i!=num/i:
  9.                 sum+=num/i
  10.     return sum
  11.  
  12. def JlFindsum():
  13.     result={}
  14.     for i in range(10000):
  15.         result[i]=Findsum(i)
  16.     return result
  17.  
  18. def main():
  19.     answer=0
  20.     result=JlFindsum()
  21.     for i in range(1,10000):
  22.             if result[i]<10000 and i!=result[i] and i==result[result[i]]:
  23.                 answer+=i
  24.     print answer
  25.  
  26. if __name__=="__main__":
  27.     main()

 

Problem 22

19 July 2002

 

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

 

  1. #!/usr/bin/env python
  2.  
  3. def worth(word):
  4.     return sum(ord(letter) - ord('A') + 1 for letter in word)
  5.  
  6. def main():
  7.     ilist=open("names.txt").read().replace('"','').split(',')
  8.     ilist.sort()
  9.     print sum((i+1) * worth(ilist[i]) for i in xrange(0, len(ilist)))
  10.  
  11. if __name__ == "__main__":
  12.     main()

 

projecteuler.net logo

Problem 23

02 August 2002

 

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
  1. #!/usr/bin/env python
  2.  
  3. import math
  4.  
  5. def sumProperDivisors(n):
  6.     sum = 1
  7.     for i in xrange(2, int(math.sqrt(n)) + 1):
  8.         if n % i ==0:
  9.             sum += i
  10.             if n / i != i:
  11.                 sum += n / i
  12.     return sum
  13.  
  14. def getAbundantList(n):
  15.     list = []
  16.     for i in xrange(2, n):
  17.         if sumProgerDivisors(i) > i:
  18.             list.append(i)
  19.     return list
  20.  
  21. abundants = set()
  22.  
  23. def isSumOfTwoAbundants(n):
  24.     return any((n-a in abundants) for a in abundants)
  25.  
  26. def goodway(n):
  27.     total = 0
  28.     for i in xrange(1, n):
  29.         if sumProperDivisors(i) > i:
  30.             abundants.add(i)
  31.         if not isSumOfTwoAbundants(i):
  32.             total += i
  33.     return total
  34.  
  35. if __name__ == "__main__":
  36.     print goodway(28123)

 

Problem 25

30 August 2002

 

The Fibonacci sequence is defined by the recurrence relation:

F_(n) = F_(n−1) + F_(n−2), where F_(1) = 1 and F_(2) = 1.

Hence the first 12 terms will be:

F_(1) = 1
F_(2) = 1
F_(3) = 2
F_(4) = 3
F_(5) = 5
F_(6) = 8
F_(7) = 13
F_(8) = 21
F_(9) = 34
F_(10) = 55
F_(11) = 89
F_(12) = 144

The 12th term, F_(12), is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

 

Answer:
4782
  1. #!/usr/bin/env python
  2.  
  3. def con_fib(num):
  4.     f1=1
  5.     f2=1
  6.     count=2
  7.     while f2 < 10**(num-1):
  8.         f1,f2 = f2,f1+f2
  9.         count+=1
  10.     return count
  11.  
  12. if __name__ == "__main__":
  13.     print con_fib(1000)

 

Problem 26

13 September 2002

 

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
  1. #!/usr/bin/env python
  2.  
  3. def cycle_length(n):
  4.     i = 1
  5.     if n % 2 == 0:
  6.         return cycle_length(n / 2)
  7.     if n % 5 == 0:
  8.         return cycle_length(n / 5)
  9.     if n == 1:
  10.         return 0
  11.     while True:
  12.         if (pow(10, i) - 1) % n == 0:
  13.             return i
  14.         else:
  15.             i = i + 1
  16.  
  17. m, n=0, 0
  18. for i in xrange(2, 1000):
  19.     c = cycle_length(i)
  20.     if c > m:
  21.         m, n = c, i
  22. print n

 

Problem 27

27 September 2002

 

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, 40^(2) + 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
  1. #!/usr/bin/env python
  2.  
  3. import math
  4.  
  5. def isprime(n):
  6.     if n < 2:
  7.         return False
  8.     for i in range(2, int(math.sqrt(n) + 1)):
  9.         if n % i ==0:
  10.             return False
  11.     return True
  12.  
  13. ilist=[i for i in xrange(2, 1000) if isprime(i)]
  14.  
  15. def max_prime_product():
  16.     max_pair = (0, 0, 0)
  17.     for a in xrange(-999, 1000, 2):
  18.         for b in ilist:
  19.             n, count = 0, 0
  20.             while True:
  21.                 v = n * n + a * n + b
  22.                 if isprime(v):
  23.                     count += 1
  24.                 else:
  25.                     break
  26.                 n += 1
  27.             if count > max_pair[2]:
  28.                 max_pair = (a, b, count)
  29.     return max_pair[0] * max_pair[1]
  30.  
  31. if __name__ == "__main__":
  32.     print max_prime_product()

 

Problem 28

11 October 2002

 

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
  1. #!/usr/bin/env python
  2.  
  3. def sum_spirial(n):
  4.     sum = 1  #here add 1
  5.     for i in xrange(3, n + 1, 2):
  6.         num = i * i
  7.         sum += 4 * num - 6 * (i - 1)  #sum += num + (num -(i -1)) + (num - (i - 1) -(i - 1)) + (num - (i - 1) -(i -1) -(i - 1))
  8.     return sum
  9.  
  10. if __name__ == "__main__":
  11.     print sum_spirial(1001)

 

Problem 29

25 October 2002

 

Consider all integer combinations of a^(b) 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 a^(b) for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

 

Answer:
9183
  1. #!/usr/bin/env python
  2. ilist = [x ** y for x in xrange(2, 101) for y in xrange(2, 101)]
  3. ilist = set(ilist)
  4. print len(ilist)

 

Problem 30

08 November 2002

 

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 = 1^(4) 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
  1. #!/usr/bin/env python
  2.  
  3. def power_digits(n, p):
  4.     s = 0
  5.     while n > 0:
  6.         d = n % 10
  7.         n = n / 10
  8.         s = s + pow(d, p)
  9.     return s
  10. max_num = 9 ** 5 * 6  #m >= 6, p > m * (9 ** 5)
  11. print sum( n for n in xrange(2, max_num) if power_digits(n, 5) == n)

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter