Project Euler Python解题汇总 005 ~ 010

What is Project Euler?
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.

这些解基本上都是用Python暴力解出来的,没有经过优化,只求答案不起速度,但是为了知道运算在什么位置了还加了一些额外的附加代码的,哇咔咔。

问题5:
What is the smallest number divisible by each of the numbers 1 to 20?
求1,2,..,19,20的最小公倍数。

i=2520
while(True):
    flag=True
    for j in range(11,21):
        if i%j<>0:
            flag=False
        #else:
        #    print i,j
    if flag:
        print i
        break
    i=i+2520
    if i%1000000==0:
        print "now is",i

问题6:
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
求1至100和的平方与1至100的平方和之差。

a = 0
b = 0
for i in range(1,101):
    a=a+i*i
    b=b+i
print b*b-a

问题7:
Find the 10001st prime.
求第10001个素数(质数)。

from itertools import count
def ispre(n):
    flag = True
    for x in range(2,n-1):
        if n%x==0:
            flag = False
            break
    return flag
 
t=0  
for i in count(2):
    if ispre(i):
        t+=1
    if t>10000:
        print i
        break

问题8:
Discover the largest product of five consecutive digits in the 1000-digit number.
在给定的1000个数中,求出连续5个数的最大乘积。

nums ='73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'
m = 0
for i in range(0,996):
    n = 1
    for x in nums[i:i+5]:
        n *= int(x)
    if n > m:
        m = n
print m

问题9:
Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.
求满足a+b+c = 1000的勾股数的积。(注:勾股数指满足a^2+b^2=c^2的自然数a,b,c)

flag = False
for x in range(1,1000):
    for y in range(1,1000):
        z = 1000 - x - y
        if x*x + y*y == z*z and x

问题10:
Calculate the sum of all the primes below two million.
求2,000,000以内所有素数的和。

from math import sqrt 
def ispre(n):
    flag = True
    for x in range(2,int(sqrt(n)+1)):
        if n%x==0:
            flag = False
            break
    return flag
 
t=0
n=1
for i in range(2,2000000):
    if ispre(i):
        t+=i
    if i>20000*n:
        n=n+1
        print i
print t

-EOF-

发表评论

电子邮件地址不会被公开。 必填项已用*标注