Project Euler 11~20

Project Euler 1~10

dcy posted @ 2009年5月30日 22:49 in Project Euler , 4296 阅读

 

Problem 1

05 October 2001

 

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

 

Answer:
233168

代码:

 

#!/usr/bin/env python
print sum(a for a in range(1000) if a%3==0 or a%5==0)

 

 

Problem 2

19 October 2001

 

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

 

Answer:
4613732
 

代码:

 

#!/usr/bin/env python

def firstThink():
    list=[1,2]
    for i in range(2,4000001):
        r=list[i-1]+list[i-2]
        if r<4000000:
            list.append(r)
        else:
            break
    print sum(i for i in list if i %2==0)

def secondThink():
    a=1
    b=1
    c=a+b
    sum=0
    while c<=4000000:
        sum=sum+c
        a=b+c
        b=c+a
        c=a+b
    print sum

def thirdThink():
    'E(n)=4*E(n-1)+E(n-2)'
    a=2
    b=8
    sum=2
    while b<=4000000:
        h=4*b+a
        a=b
        b=h
        sum=sum+a
    print sum

firstThink()
secondThink()
thirdThink()
 

 

Problem 3

02 November 2001

 

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

 

Answer:
6857

代码:

 

#!/usr/bin env python
import math
def primer(n):
    m=int(math.sqrt(n))
    for i in range(2,m):
        if n%i ==0:
            return 0
    return 1

b=600851475143
a=int(math.sqrt(b))
maximum=0
while(a):
    if b%a==0 and primer(a)==1:
        maximum=a
        break
    a=a-1

print maximum
 

 

Problem 4

16 November 2001

 

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

 

Answer:
906609

一句话代码:

 

#!/usr/bin/env python
print max(m*n for m in range(100,1000) for n in range(m,1000) if str(m*n)==str(m*n)[::-1])

 

 

代码:

 

#!/usr/bin/env python

def ispalmindron2(num):
    num=str(num)
    return num==num[::-1]

def func(length):
    print max(m*n for m in range(10**(length-1),10**length) for n in range(m,10**length) if ispalmindron2(m*n))

import time
start=time.time()
if __name__=='__main__':
    func(3)
print time.time()-start
 

 

Problem 5

30 November 2001

 

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

 

Answer:
232792560
 

 

代码:

 

#!/usr/bin/env python
def func(num):
    result=1
    list=range(1,num+1)
    for i in range(1,num):
        for j in range(i+1,num):
            if list[j]%list[i]==0:
                list[j]/=list[i]
        result=result*list[i]
    print result

func(20)
 

 

Problem 6

14 December 2001

 

The sum of the squares of the first ten natural numbers is,

1^(2) + 2^(2) + ... + 10^(2) = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^(2) = 55^(2) = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

 

Answer:
25164150

 

#!/usr/bin/env python
print sum(range(101))**2-sum(i*i for i in range(101))

 

Problem 7

28 December 2001

 

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.

What is the 10001^(st) prime number?

 

Answer:
104743
 

 

 

 

#!/usr/bin/env python
import math
def isprime(num):
    for i in range(2,int(math.sqrt(num)+1)):
        if num%i==0:
            return False
    return True

def func1(num):
    flag=1
    flagnum=3
    while flag<num:
        if isprime(flagnum):
            flag=flag+1
##            print '%d---%d' %(flag,flagnum)
        flagnum=flagnum+2
    print '*'*20
    print flagnum-2

if __name__=='__main__':
    func1(10001)

 

Problem 8

11 January 2002

 

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

 

Answer:
40824
 

代码:

 

#!/usr/bin/env python
str='73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'


def func1():
    max=0
    for i in range(996):
        t=1
        for x in str[i:i+5]:
            t*=int(x)
            if t>max:
                max=t
    print max

func1()
 

 

Problem 9

25 January 2002

 

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a^(2) + b^(2) = c^(2)

For example, 3^(2) + 4^(2) = 9 + 16 = 25 = 5^(2).

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

 

Answer:
31875000
 

 

代码:

 

#!/usr/bin/env python
print[a*b*(1000-a-b) for a in range(1,1000) for b in range(a,1000) if a*a+b*b==(1000-a-b)**2]

 

Problem 10

08 February 2002

 

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

 

 

筛选法代码:

 

  1. #!/usr/bin/env python
  2. import math
  3.  
  4. def sieve_method(num):
  5.     prime_data = [x for x in xrange(num + 1)]
  6.     for i in xrange(2,int(math.sqrt(num) + 1)):
  7.         if prime_data[i]:
  8.             start = i**2
  9.             step  = i
  10.             prime_data[start::step]=((num-start)/step + 1) * [0]
  11.     print sum(prime_data) -1#1 is not prime number
  12.  
  13. if __name__ == "__main__":
  14.     sieve_method(2000000)

 

 

 

 

 

 

 

 

   

 

 

 

 

 

Avatar_small
MasterLuo 说:
2009年6月04日 18:08

没想到一上programmer就找到一个玩过project euler的朋友。

Avatar_small
dcy 说:
2009年6月05日 01:15

@MasterLuo: 玩过一点,不多,用来练习Python,呵呵……

Beth Dutton Blue Coa 说:
2023年10月07日 19:04

I ordered this coat from America Jackets and I must say I am amazed with the high quality and fit. You guys can also try it.


登录 *


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