码迷,mamicode.com
首页 > 其他好文 > 详细

Problem 1~4

时间:2015-02-11 16:19:01      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

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

seq = [x for x in range(1,1000) if x % 3 == 0 or x % 5 == 0]
print sum(seq)

 

Problem 2:

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.

1 def fib():
2     a , b = 1 , 2
3     sum = 0
4     while b < 4000000:
5         if b % 2 == 0:
6             sum += b
7         a , b = b , a + b
8     return sum
9 print fib()

Problem 3:

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

What is the largest prime factor of the number 600851475143 ?

 1 import math
 2 def showfactors(N):
 3     seq = [x for x in range(1,int(math.sqrt(N))+1) if N % x == 0]
 4     return seq
 5 def isprime(num):
 6     for i in range(2,num):
 7         if num % i == 0:
 8             return 0
 9     return 1
10 N = 600851475143
11 result = showfactors(N)
12 print max([y for y in result if isprime(y)])

Problem 4:

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.

1 result = []
2 for i in range(100,1000):
3     for j in range(100,1000):
4         mul = i * j
5         seq = str(mul)
6         if seq[:] == seq[::-1]:
7             result.append(int(seq))
8 print max(result)

 

Problem 1~4

标签:

原文地址:http://www.cnblogs.com/Gklee2014/p/4286290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!