一个很有意思的解题网站.
第二题:
第三题:
找出指定数的最大质因数
我主要用python来解决.
第一题:
求1000以下的能被3或5整除的整数之和.
- >>> sum([i for i in xrange(1000) if not i%3 or not i%5])
- 233168
求
Fibonacci数列中小于4百万的偶数之和.
- # -*- coding: gb2312 -*-
- from __future__ import generators
- def fibo(n):
- """定义一个Fibonacci数列生成器"""
- nextItem = 0
- beyondItem = 1
- currentN = 0
- while currentN <= n:
- yield nextItem
- nextItem, beyondItem = beyondItem,nextItem + beyondItem
- currentN += 1
- # 找出数列中第一个大于4000000的序号
- i = 1
- while list(fibo(i))[-1]<4000000:
- i += 1
- print sum( [ j for j in fibo(i) if not j%2 ]
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
- # -*- coding: gb2312 -*-
- from __future__ import generators
- import math
- num = 600851475143
- def zhisu(m):
- """分解质因数的生成器"""
- n = m
- start = 2
- c_flag = 1
- while c_flag:
- c_flag = 0 # 本轮是否找出了一个质因数,如果否,则已经剩最后一个质因数了.
- for i in xrange(start,int(math.sqrt(n)+1)):
- if not n%i:
- yield i
- n = n/i
- start = i
- c_flag = 1
- break
- if n<>m:
- yield n
- print list(zhisu(num))