歐拉計劃的Python解法(1-10)

Problem 1. Multiples of 3 and 5

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.python

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

求小於1000的全部天然數中,可被3或5整除的數字之和。編程

#!/usr/bin/env python

sum = 0
for i in xrange(1000):
    if i % 3 == 0 or i % 5 == 0:
        sum += i
print sum

Answer 1: 233168ide

Problem 2. Even Fibonacci numbers

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, ...spa

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.設計

求不大於4000000的斐波那契數列中,全部偶數之和。code

兩個解法:遞歸

1. 建立多個變量,變量循環迭代。效率較高。three

#!/usr/bin/env python

a = 1
b = 2
fib = 3
sum = 2 #這個初始值是關鍵,因爲下面的while循環中,斐波那契值從3開始,所以sum的初始值應爲2
while fib < 4000000:
    fib = a + b
    if not fib % 2:
        sum += fib
    a = b
    b = fib
print sum

2. 使用遞歸函數,遞歸效率較低,對於練習遞歸函數仍是挺有幫助的。

#!/usr/bin/env python

def feb(i):
    if i == 0 or i == 1:
        return 1
    else:
        result = feb(i-1) + feb(i-2)
        return result

sum = 0
i = 0
tmp = 0
while tmp < 4000000:
    i += 1
    tmp = feb(i)
    if not tmp % 2:
        sum += tmp
print sum

Answer 2: 4613732

Problem 3. Largest prime factor

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

What is the largest prime factor of the number 600851475143 ?

求數字600851475143的最大質因子。

思路:將此數字除以2,若能被整除,則取結果繼續除以2,若不能則除以3,依序遞增,直到被本身除,此時的數字即爲所求的最大質因子。

def eula(s):
    i = 2
    while s != 1:
        if not s % i:
            s = s / i
            maxPrime = i
        else:
            i += 1
    return maxPrime

print eula(600851475143)

Answer 3: 6857

Problem 4. Largest palindrome product

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

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

求兩個三位整數乘積中,最大的迴文數。

思路:算出全部的兩個三位整數的乘積,放入一個列表中,取出最大的迴文數。

#!/usr/bin/env python

# 定義迴文數判斷函數
def palindrome(n):
    lenN = len(str(n))
    for i in xrange(lenN/2):
        if str(n)[i] != str(n)[-1-i]:
            return False
    return True

myList = [i * j for i in xrange(999,99,-1) for j in xrange(999,99,-1)]
myList.sort(reverse=True)    #將myList從大到小排列,方便循環找到答案後跳出程序。

for eachItem in myList:
    if palindrome(eachItem):
        print eachItem
        break

Answer 4: 906609

Problem 5. Smallest multiple

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 positive number that is evenly divisible by all of the numbers from 1 to 20?

求能被1~20的每一個數都整除的最小值。

這道題,一開始我想用循環作,後來發現我錯了,因爲答案太大(9位數),等解釋器轉了半天都沒結果,遂放棄。

思路:若是一個數n能被20之內的整數整除,應當知足如下條件:

假設一個質數i,而且i的j次方不大於20,則這個數應當能被i的j次方整除,即:n % (i ** j) == 0

這樣,不用編程的方法能夠求得:n = (2 ** 4) * (3 ** 2) * 5 * 7 * 11 * 13 * 17 * 19

#!/usr/bin/env python

from math import sqrt

#將不大於20的全部質數放入列表myPrime中
myPrime = [n for n in xrange(2,21) if 0 not in [n % i for i in xrange(2,int(sqrt(n))+1)]]

#循環求得每個質數的次方值j,並將結果加乘到result中去
result = 1
for i in myPrime:
    j = 1
    while i ** j <= 20:
        j += 1
    result = result * i ** (j - 1)

print result

Answer 5: 232792560

Problem 6. Sum square difference

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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.

求「100的和的平方」與「100的平方和」的差。

遞歸:

#!/usr/bin/env python

def sum(n):
    if n == 0:
        sumN = 0
    else:
        sumN = sum(n-1) + n
    return sumN

def square(n):
    if n == 0:
        squareN = 0
    else:
        squareN = square(n-1) + n ** 2
    return squareN

print sum(100) ** 2 - square(100)

Answer 6: 25164150

Problem 7. 10001st prime

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

What is the 10 001st prime number?

求第10001個質數。

思路:使用while循環,驗證從2開始的每個正整數是否爲質數,如果,則給計數器+1,直到計數器值爲10001。

#/usr/bin/env python

from math import sqrt

#質數驗證函數
def isPrime(n):
    for i in xrange(2,int(sqrt(n))+1):
        if n % i == 0:
            return False
    return True

i = 1
counter = 0    #計數器初始值爲0
while counter < 10001:    #計數器到10001時跳出循環
    i += 1
    if isPrime(i):
        counter += 1
print i

Answer 7: 104743

Problem 8. Largest product in a series

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

找出這個1000位的整數中連續5個數字的最大乘積。

思路:使用字符串切片符得到連續的5個數字,將全部結果列入一個列表中,取最大值。

#!/usr/bin/env python

str1000 = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'

multilist = [int(str1000[i]) * int(str1000[i+1]) * int(str1000[i+2]) * int(str1000[i+3]) * int(str1000[i+4]) for i in xrange(len(str1000)-4)]

print max(multilist)

Answer 8: 40824

Problem 9. Special Pythagorean triplet

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

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product abc.

找到知足a+b+c=1000的畢達哥拉斯三元組,並求三元組的乘積。

思路:因爲a < b < c,所以a不會大於333,b不會大於500,所以考慮用for循環。

#!/usr/bin/env python

for a in xrange(1, 334):
    for b in xrange(a, 501):  #因爲b大於a,所以這裏設計循環最小值等於a
        c = 1000 - a - b
        if c ** 2 == a ** 2 + b ** 2:
            print a, b, c, a * b * c

Answer 9: 31875000

Problem 10. Summation of primes

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

Find the sum of all the primes below two million.

求2000000如下的素數和。

#/usr/bin/env python

from math import sqrt

def isPrime(n):
    for i in xrange(2,int(sqrt(n))+1):
        if n % i == 0:
            return False
    return True

summa = 0
for i in xrange(2,2000000):
    if isPrime(i):
        summa += i

print summa

Answer 10: 142913828922

相關文章
相關標籤/搜索