prpr

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

``````#!/usr/bin/env python

sum = 0
for i in xrange(1000):
if i % 3 == 0 or i % 5 == 0:
sum += i
print sum``````

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

1. 创建多个变量，变量循环迭代。效率较高。

``````#!/usr/bin/env python

a = 1
b = 2
fib = 3
sum = 2 #这个初始值是关键，由于下面的while循环中，斐波那契值从3开始，因此sum的初始值应为2
while fib < 4000000:
fib = a + b
if not fib % 2:
sum += fib
a = b
b = fib
print sum``````

2. 使用递归函数，递归效率较低，对于练习递归函数还是挺有帮助的。

``````#!/usr/bin/env python

def feb(i):
if i == 0 or i == 1:
return 1
else:
result = feb(i-1) + feb(i-2)
return result

sum = 0
i = 0
tmp = 0
while tmp < 4000000:
i += 1
tmp = feb(i)
if not tmp % 2:
sum += tmp
print sum``````

# Problem 3. Largest prime factor

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

What is the largest prime factor of the number 600851475143 ?

``````def eula(s):
i = 2
while s != 1:
if not s % i:
s = s / i
maxPrime = i
else:
i += 1
return maxPrime

print eula(600851475143)``````

# Problem 4. Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 &times; 99.

Find the largest palindrome made from the product of two 3-digit numbers.

``````#!/usr/bin/env python

# 定义回文数判断函数
def palindrome(n):
lenN = len(str(n))
for i in xrange(lenN/2):
if str(n)[i] != str(n)[-1-i]:
return False
return True

myList = [i * j for i in xrange(999,99,-1) for j in xrange(999,99,-1)]
myList.sort(reverse=True)    #将myList从大到小排列，方便循环找到答案后跳出程序。

for eachItem in myList:
if palindrome(eachItem):
print eachItem
break``````

# Problem 5. Smallest multiple

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?

``````#!/usr/bin/env python

from math import sqrt

#将不大于20的所有质数放入列表myPrime中
myPrime = [n for n in xrange(2,21) if 0 not in [n % i for i in xrange(2,int(sqrt(n))+1)]]

#循环求得每一个质数的次方值j，并将结果加乘到result中去
result = 1
for i in myPrime:
j = 1
while i ** j <= 20:
j += 1
result = result * i ** (j - 1)

print result``````

# Problem 6. Sum square difference

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.

``````#!/usr/bin/env python

def sum(n):
if n == 0:
sumN = 0
else:
sumN = sum(n-1) + n
return sumN

def square(n):
if n == 0:
squareN = 0
else:
squareN = square(n-1) + n ** 2
return squareN

print sum(100) ** 2 - square(100)``````

# Problem 7. 10001st prime

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?

``````#/usr/bin/env python

from math import sqrt

#质数验证函数
def isPrime(n):
for i in xrange(2,int(sqrt(n))+1):
if n % i == 0:
return False
return True

i = 1
counter = 0    #计数器初始值为0
while counter < 10001:    #计数器到10001时跳出循环
i += 1
if isPrime(i):
counter += 1
print i``````

# Problem 8. Largest product in a series

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934

96983520312774506326239578318016984801869478851843

85861560789112949495459501737958331952853208805511

12540698747158523863050715693290963295227443043557

66896648950445244523161731856403098711121722383113

62229893423380308135336276614282806444486645238749

30358907296290491560440772390713810515859307960866

70172427121883998797908792274921901699720888093776

65727333001053367881220235421809751254540594752243

52584907711670556013604839586446706324415722155397

53697817977846174064955149290862569321978468622482

83972241375657056057490261407972968652414535100474

82166370484403199890008895243450658541227588666881

16427171479924442928230863465674813919123162824586

17866458359124566529476545682848912883142607690042

24219022671055626321111109370544217506941658960408

07198403850962455444362981230987879927244284909188

84580156166097919133875499200524063689912560717606

05886116467109405077541002256983155200055935729725

71636269561882670428252483600823257530420752963450

``````#!/usr/bin/env python

str1000 = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450'

multilist = [int(str1000[i]) * int(str1000[i+1]) * int(str1000[i+2]) * int(str1000[i+3]) * int(str1000[i+4]) for i in xrange(len(str1000)-4)]

print max(multilist)``````

# Problem 9. Special Pythagorean triplet

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.

``````#!/usr/bin/env python

for a in xrange(1, 334):
for b in xrange(a, 501):  #由于b大于a，因此这里设计循环最小值等于a
c = 1000 - a - b
if c ** 2 == a ** 2 + b ** 2:
print a, b, c, a * b * c``````

# Problem 10. Summation of primes

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

``````#/usr/bin/env python

from math import sqrt

def isPrime(n):
for i in xrange(2,int(sqrt(n))+1):
if n % i == 0:
return False
return True

summa = 0
for i in xrange(2,2000000):
if isPrime(i):
summa += i

print summa``````

### 评论(3)

- -看官网的成就榜，是有一些人五百多题都做完的。刚新出的题能有几十人做完。

#### 引用来自“幻视Vision”的评论

GITHUB上最多的应该是做了三百题。

GITHUB上最多的应该是做了三百题。

Python新世界
2018/07/21
0
0

Problem 11. Largest product in a grid In the 20x20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 ......

prpr
2014/03/14
1K
0
3个Python编程中的难题，我给九种解法，你能想出多少种？

1.字母转换问题 题目要求： 你得到一个可能混合大小写字母的字符串，你的任务是把该字符串转为仅使用小写字母或者大写字母，为了尽可能少的改变： 如果字符串包含的大字母数小于等于小写字母...

Python新世界
2018/07/25
0
0
Python3 欧拉计划 问题1-5

AiFan
2017/11/06
0
0
Linux Shell学习简单小结

shelllinuxperformancefunctionfile if-fi [cpp] view plaincopyprint? #! /bin/bash # 删除文件 和 新建文件 file=readme function delFile(){ if [ -e ./\$file ];then rm -f ./\$file echo ......

Zero零_度
2015/04/08
91
0

IDEA Maven project: 'xxx/pom.xml' already exists in VFS

Failed to create a Maven project: ‘xxx/pom.xml‘ already exists in VFS idea创建项目后，发现项目有问题，删除后重新创建，提示错误如下。 解决办法 1.通过idea打开任意一个项目 2.File...

50分钟前
7
0
Flutter之 State 生命周期

State 的生命周期，指的是在用户参与的情况下，其关联的 Widget 所经历的，从创建到显示，再到更新最后到停止，直至销毁等各个阶段 不同的阶段涉及到特定的任务处理 State 的生命周期流程如下...

57分钟前
11
0

gzc426

6
0

StupidZhe

38
0
java内存泄漏问题

Bb进阶

10
0