Project Euler: Problem 1 to 10
1 问题1:Multiples of 3 and 5
1.1 问题描述
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.
1.2 代码
def problem_1():
list = [x for x in range(1, 1000) if x % 3 == 0 or x % 5 == 0]
return_sum = sum(list)
print("The sum of multiples less than 1000 of 3 or 5:",return_sum)
if __name__ == '__main__':
problem_1()
1.3 运行结果
The sum of multiples less than 1000 of 3 or 5: 233168
2 问题2:Even Fibonacci numbers
2.1 问题描述
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:
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
2.2 代码
def problem_2():
fibonacci_list = [1, 1]
return_sum = 0
while fibonacci_list[-1] < 4000000:
fibonacci_list.append(fibonacci_list[-1] + fibonacci_list[-2])
if fibonacci_list[-1] % 2 == 0:
return_sum += fibonacci_list[-1]
print("The sum of even terms of a Fibonacci sequence whose median value is not more than 4 million is:",
return_sum)
if __name__ == '__main__':
problem_2()
2.3 运行结果
The sum of even terms of a Fibonacci sequence whose median value is not more than 4 million is: 4613732
3 问题3:Largest prime factor
3.1 问题描述
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
3.2 代码
def problem_3():
num = 600851475143
i = 2
list = []
while i <= num:
if num % i == 0:
list.append(i)
num /= i
i += 1
print("The largest prime factor of the number 600851475143 is:",list[-1])
if __name__ == '__main__':
problem_3()
3.3 运行结果
The largest prime factor of the number 600851475143 is: 6857
4 问题4:Largest palindrome product
4.1 问题描述
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.
4.2 代码
def problem_4():
num_list = []
for i in range(999, 99, -1):
for j in range(i, 99, -1):
if check(i * j):
num_list.append(i * j)
print("The largest palindrome made from the product of two 3-digit numbers is:", max(num_list))
def check(num): #判断一个数是否为回文数
num_list = []
num_list.extend(str(num)) #将该数转化为单子字符列表
n = int(len(num_list)/2.0) #循环判断次数仅需列表总长一半
for i in range(n):
if num_list[i] != num_list[-i-1]:
return False
return True
if __name__ == '__main__':
problem_4()
4.3 运行结果
The largest palindrome made from the product of two 3-digit numbers is: 906609
5 问题5:Smallest multiple
5.1 问题描述
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?
5.2 代码
import math
def problem_5(n):
num = 1 #数初始化为1
num_list1 = list(range(1, n + 1)) #创建1-n的列表
for i in num_list1: #1-10依次相乘,但是每次新得的数除以当前i能除尽,则跳过该数
if num % i == 0:
continue
else:
num *= i
num_list2 = list(range(int(n / 2) + 1, n + 1)) #创建n/2-n的列表
for value in num_list2:
if math.sqrt(value) in num_list1: #如果列表中的数的平方于num_list1中出现,则除以出现的那个数
num /= math.sqrt(value)
num = int(num)
print("The smallest positive number that is evenly divisible by all of the numbers from 1 to %d is: %d" % (n, num))
print("And this is ", check(num, n))
def check(num, n): #检测找到的数是否能对1-n相除无余数
for i in range(1, n + 1):
if num % i != 0:
return False
return True
if __name__ == '__main__':
problem_5(20)
5.3 运行结果
The smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is: 1396755360
And this is True
6 问题6:Sum square difference
6.1 问题描述
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.
6.2 代码
import functools
def problem_6(n):
num_list = list(range(1, n + 1)) #创建1-n的列表
num1 = map(lambda x :x * x, num_list) #对应位置相乘
num1 = sum(num1) #求和
num2 = functools.reduce(lambda x, y : x + y, num_list) #列表元素相加
num2 = num2**2 #平方
print("The sum of the squares of the %d natural numbers and the square of the sum is:" % n)
print(num2 - num1)
if __name__ == '__main__':
problem_6(100)
6.3 运行结果
The sum of the squares of the 100 natural numbers and the square of the sum is:
25164150
7 问题7:10001st prime
7.1 问题描述
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?
7.2 代码
主体思路:除2外的所有素数都不是偶数。
import time
def problem_7(n):
num, i = 3, 1 #除2外的所有素数都不是偶数,故从2开始
while i < n:
if is_prime(num):
i += 1 #统计已找到的素数个数
num += 2
if num <5:
num = 4
print("The 10 001st prime number is:",num - 2)
def is_prime(num): #判断
i = 2
while i ** 2 <= num:
if num % i == 0:
return False
i += 1
return True
if __name__ == '__main__':
start_time = time.time()
problem_7(10001)
end_time = time.time()
print("The consumed time is:",end_time - start_time)
7.3 运行结果
The 10 001st prime number is: 104743
The consumed time is: 1.1668810844421387
8 问题8:Largest product in a series
8.1 问题描述
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
8.2 代码
主体思路:依次比较,将含有0的子串跳过。
import time
import functools
from numpy import *
def problem_8(n):
num = "73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450"
temp_index = 0 #找索引
a, b = 0, 0
for i in range(len(num) - n): #循环次数为总长度减去给定数
temp_list = num[i : i + n]
if '0' in temp_list: #包含0则跳过
continue
b = multiply(temp_list)
if a < b:
a = b
temp_index = i
print("The index is:", temp_index)
print("The value is", multiply(num[temp_index: temp_index + n]))
def multiply(num_list):
temp_list = [int(i) for i in num_list]
return functools.reduce(lambda x,y:x * y,temp_list)
if __name__ == '__main__':
start_time = time.time()
problem_8(13)
end_time = time.time()
print("The consumed time is:",end_time - start_time)
8.3 运行结果
The index is: 197
The value is 23514624000
The consumed time is: 0.001994609832763672
9 问题9:Special Pythagorean triplet
9.1 问题描述
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.
9.2 代码
主体思路:时间与空间复杂度不高时,直接暴力循环。
import time
def problem_9():
for a in range(334): #由于a<b<c,且a+b+c = 1000,故a不可能超过333,b不可能超过500
for b in range(a + 1, 500):
if a**2 + b**2 == (1000 - a -b)**2:
print("The numbers is:",a ,b, (1000 - a - b))
if __name__ == '__main__':
start_time = time.time()
problem_9()
end_time = time.time()
print("The consumed time is:",end_time - start_time)
9.3 运行结果
The numbers is: 200 375 425
The consumed time is: 0.1296532154083252
10 问题10:Summation of primes
10.1 问题描述
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
10.2 代码
主体思路:从2开始对每一个数进行判断,如当前数是素数,则将其所有对其取余为0的数都添加至集合
import time
from functools import reduce
def problem_10():
num_list = []
num_set = set()
for i in range(2, 2000001):
if i not in num_set:
num_list.append(i)
num_set.update(range(i ** 2, 2000001, i)) #如果2是素数,那么所有对于取余为0的数都不是素数
print("The sum of numbers is:",reduce(lambda x, y: x + y, num_list))
if __name__ == '__main__':
start_time = time.time()
problem_10()
end_time = time.time()
print("The consumed time is:",end_time - start_time)
10.3 运行结果
The sum of numbers is: 142913828922
The consumed time is: 0.8367629051208496