Project Euler|欧拉计划 1~10 (python 3.6)

一,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的最大质数是多少?

解题思路

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值