目前使用的是python2,以后有其他学习计划再更新其他语音的代码。
一般情况下,顺序为英文原题——中文翻译——代码——结果。多说一句,直接输入结果就好,我曾经改了两个多小时的代码格式,还以为是代码缩进不符合规则...后来才发现,直接输入结果就可以了,不用输入代码。
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.
中文:
3的倍数和5的倍数
如果我们列出10以内所有3或5的倍数,我们将得到3、5、6和9,这些数的和是23。
求1000以内所有3或5的倍数的和。
--------------------------------------------------------------------
划重点,求1000以内(不包含1000),我最初就把1000也计算在内了。
代码:
print sum(filter(lambda x:x%3==0 or x%5==0, xrange(1000)))
或者用等差数列和排容原理
def sum1toN(n):
return n * (n + 1) / 2
def sumMultiple(limit,d):
return sum1toN((limit - 1) / d) * d
print sumMultiple(1000, 3) + sumMultiple(1000, 5) - sumMultiple(1000, 15)
结果:
233168
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和2开始生成的斐波那契数列前10项为:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
值为四百万以内的项的斐波那契数列,求其中值为偶数的项之和。
--------------------------------------------------------------------
划重点,值不超过400W的斐波那契数列,求其中值为偶数的项之和。
英语不好,我给理解成了有400W项的数列,计算偶数项的和,那个数值巨大...
代码:
def sumEvenFibonacci(n):
a = 1
b = 2
num = 0
sum = 2
for i in xrange(3,n+1):
num = a + b
a = b
b = num
print num,a,b
if num > 4000000:
break
if num % 2 == 0:
sum += num
return sum
print sumEvenFibonacci(3000000)
-------------------------------------------------------
a = 1
b = 2
num = 0
sum = 2
while num < 4000000:
num = a + b
if num % 2 == 0:
sum += num
a = b
b = num
print sum
结果:4613732
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 ?
最大质因数
13195的所有质因数为5、7、13和29。
600851475143最大的质因数是多少?
--------------------------------------------------------------------
划重点:质因数,能整除给定正整数的质数(除了1和被定正整数本身外)。先除以2,如果可以整除,继续除以2,如果不可以整除,则除以3...;如果不可以被2整除,则除以3...
def primeNumList(n):
i = 2
while n != 1:
if not n % i:
n = n / i
maxPrime = i
print maxPrime
else:
i += 1
return maxPrime
print primeNumList(600851475143)
结果:6857
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 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
最大回文乘积
回文数就是从前往后和从后往前读都一样的数。由两个2位数相乘得到的最大回文乘积是 9009 = 91 × 99。
找出由两个3位数相乘得到的最大回文乘积。
-----------------------------------------------------------------------------------
def palindrome(n): #是否回文数
flag = True
for i in range(len(n)/2):
if str(n)[i] != str(n)[len(n)-1-i]:#后面可以替换成[-(i+1)]
flag = False
break
return flag
palindrome_list = [] #可以把所有回文数都存到一个列表里面
for i in range(999,99,-1):
for j in range(999,99,-1):
if palindrome(str(i*j)):
palindrome_list.append(i*j)
print max(palindrome_list)
max_palindrome = 0 #也可以直接判断获取最大的回文数
for i in range(999,99,-1):
for j in range(999,99,-1):
if palindrome(str(i*j)) and i*j > max_palindrome:
max_palindrome = i*j
print max_palindrome
结果:906609
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 isevenly divisibleby all of the numbers from 1 to 20?
最小倍数
2520是最小的能够被1到10整除的数。
最小的能够被1到20整除的正数是多少?
--------------------------------------------------------------------------
解题思路:如果一个数n能被20以内的整数整除,应当满足以下条件:
假设一个质数i,并且i的j次方不大于20,则这个数应当能被i的j次方整除,即:n % (i ** j) == 0
from math import sqrt
primeList = [n for n in range(2,21) if 0 not in [n % i for i in range(2,int(sqrt(n))+1)]]
result = 1
for i in primeList:
j =1
while i ** j <=20:
j +=1
result = result * i ** (j -1)
print result
或
def divided(multiple,n):#判断是否可以被20以内的整数整除
flag = True
if n > 0:
if multiple % n == 0:
if not divided(multiple, n-1):
flag = False
else:
flag = False
return flag
smallestMultiple = 20
while not divided(smallestMultiple,20):#如果不能被整除则加20
smallestMultiple += 20
print smallestMultiple
结果:232792560
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.
平方的和与和的平方之差
前十个自然数的平方的和是
12+ 22+ … + 102= 385
前十个自然数的和的平方是
(1 + 2 + … + 10)2= 552= 3025
因此前十个自然数的平方的和与和的平方之差是 3025 − 385 = 2640。
求前一百个自然数的平方的和与和的平方之差。
-------------------------------------------------------------------------
平方和公式 1²+2²+...+n² =n*(n+1)*(2*n+1)/6
from math import pow
def squareSum(n):
return n*(n+1)*(2*n+1)/6
def sumSquare(n):
return int(pow(sum(xrange(1,n+1)),2))
print sumSquare(100) - squareSum(100)
或不利用公式
def six(n):
return int(pow(sum(xrange(1,n+1)),2) - sum([pow(i,2) for i in xrange(1,n+1)]))
print six(100)
结果:25164150
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?
第10001个素数
列出前6个素数,它们分别是2、3、5、7、11和13。我们可以看出,第6个素数是13。
第10,001个素数是多少?
------------------------------------------------------------------------------------------------
from math import sqrt
def primeNum(n):
i = 1
num = 3
while i < n:
if [num for j in xrange(2, int(sqrt(num))+1) if num % j == 0]:
num += 2
else:
prime = num
num += 2
i += 1
return prime
print primeNum(10001)
结果:104743
8、Largest product in a series
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
连续数字最大乘积
在下面这个1000位正整数中,连续4个数字的最大乘积是 9 × 9 × 8 × 9 = 5832。
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
找出这个1000位正整数中乘积最大的连续13个数字。它们的乘积是多少?
---------------------------------------------------------------
from operator import mul
num = '''73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
'''
num = num.replace('\n','')
def maxProduct(num,adjacent_num):
if isinstance(num,str):
num = [int(i) for i in num]
elif not isinstance(num,int):
return 0
if len(num) < adjacent_num:
return 0
else:
return max([reduce(mul,num[i:i+adjacent_num]) for i in xrange(len(num)+1-adjacent_num)])
print maxProduct(num,13)
结果:23514624000
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.
特殊毕达哥拉斯三元组
毕达哥拉斯三元组是三个自然数a < b < c组成的集合,并满足
a²+b²=c²
例如,3²+ 4²= 9 + 16 = 25 = 5²。
有且只有一个毕达哥拉斯三元组满足 a + b + c = 1000。求这个三元组的乘积abc。
----------------------------------------------------------------------------
a+b+c=100,a < b < c,a小于334;a²+b²=c²,直角三角形斜边小于其他两边的和,c小于500。
from operator import pow
for a in xrange(1,334):
for b in xrange(a+1,500):
c = 1000 - a - b
if pow(c,2) == pow(a,2) + pow(b,2):
print a*b*c
结果:31875000
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.
素数的和
所有小于10的素数的和是2 + 3 + 5 + 7 = 17。
求所有小于两百万的素数的和。
-----------------------------------------------------------
素数,除了2以外,都是2*n+3。
N = 2000000
prime_number = [1 for n in range(N)]
prime_number[0] = 0
prime_number[1] = 0
for n in range(2,len(prime_number)):
if prime_number[n]==1:
for m in range(n,N-n,n):
prime_number[n+m] = 0
for n in range(N):
prime_number[n] *= n
print(sum(prime_number))
或
from time import time
from math import sqrt
s = time()
def printSum(numbers, list_lim):
result = 2
for i in range(list_lim):
if not numbers[i]:
result += 2 * i + 3
print(result)
limit = 2000000
list_lim = (limit % 2) and (int((limit - 2) / 2) + 1) or (int((limit - 2) / 2))
print list_lim
numbers = [0] * list_lim
print len(numbers)
for i in range(int((sqrt(limit) -3) / 2) + 1):
j = 2 * i**2 + 6 * i + 3
while (j < list_lim):
numbers[j] = 1
j += 2 * i + 3
printSum(numbers, list_lim)
print("Time: {}".format(time() - s))
结果:142913828922