一,Project Euler. Problem 1: Multiples of 3 and 5
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.
Find the sum of all the multiples of 3 or 5 below 1000.
Project Euler第一题,问题描述
如果我们列出所有低于10的自然数,它们是3或5的倍数,则得到3、5、6和9。这些倍数的总和为23。
找出1000以下3或5的所有倍数的总和。
解题思路
直接遍历1000以内能被3或5整除的数,然后累加这些数即可。
sum = 0
for i in range(1,1000):
if (i % 3 == 0 or i % 5 == 0):
print(i)
sum += i
print(sum)
答案为:233168
二,Project Euler.Problem 2:Even Fibonacci numbers
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, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Project Euler第二题,问题描述
斐波那契数列中的每个新项都是通过将前两个项相加而生成的。 从1和2开始,前10个项将是:
1,2,3,5,8,13,21,34,55,89,...
通过考虑斐波那契数列中值不超过400万的项,找到偶值项的总和。
解题思路
找到斐波那契值不超过4000000的项,将其保存,然后求其偶数项之和。
fib = [1,2]
new_fib = fib[-1]+fib[-2]
while new_fib < 4000000:
fib.append(new_fib)
new_fib = fib[-1]+fib[-2]
sum = 0
for i in fib:
if i%2 == 0:
sum += i
else:
sum += 0
print(fib)
print(sum)
答案为:4613732
三,Project Euler. Problem 3:Largest prime factor
Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Project Euler第三题,问题描述
13195的质因数是5、7、13和29。
600851475143的最大质数是多少?