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

解题思路

遍历600851475143的所有质因数,然后求其最大。

num = int(input("请输入一个整数"))
temp = []
while num != 1:
    for i in range(2,int(num+1)):
        if num % i == 0:
            temp.append(i)
            num = num/i
            break
print (temp)
max_num = max(temp)
print(max_num)

答案为:6857

 

四,Project Euler. Problem 4:Largest palindrome product

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.

Project Euler第四题,问题描述

回文数(palindromic number)指的是不管从哪边读都是一样的数字,且能够被分解为两个数的乘积。

由两个数字组成的最大回文数(palindromic number)是9009,且9009=91×99.

查找由两个3位数字的乘积组成的最大回文数(palindromic number)。

解题思路

三位数的乘积在10000和998001之间,介于五位数六位数之间,由于要求解的是最大回文数,所以只考虑六位数中的最大回文数,那么要找的这个数具有以下特点:左边第一个数和右边第一个数相等,左边第二个数和右边第二个数相等,中间两个数相等,如abccba这种类型。先计算出100到1000之间的所有的三位数的成绩,并保存。然后找出乘积为六位数的的数,再在六位数的成绩中找出最大回文数。

a = []
b = 100
for i in range(100,1000):
    for j in range(100,1000):
        c = i * j
        a.append(c)
p =[]
for m in a:
    if m >=100001:
        p.append(m)
print(p)
t = []
for j in p:
    j = str(j)
    if j[0] == j[-1] and j[1] == j[-2] and j[2] == j[-3]:
        t.append(j)
print(t)
print(max(t))

答案为:906609

 

五,Project Euler. Problem 5:Smallest multiple

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?

Project Euler第五题,问题描述

2520是可以除以1到10的每个数字而没有任何余数的最小数字。
能被1到20的所有数整除的最小正数是多少?

解题思路

找出1~20的最小公倍数

yushu = 1
beishu = 2
while yushu > 0:
    new_num = beishu * 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19
    yushu = new_num % 20 + new_num % 18  + new_num % 16+ new_num % 15 + new_num % 14  + new_num % 12+ new_num % 10 + new_num % 9 + new_num % 8+ new_num % 7 + new_num % 6+ new_num % 5 + new_num % 4+ new_num % 3 + new_num % 2
    beishu += 1

print(new_num)

答案为:232792560

 

六,Project Euler. Problem 6:Sum square difference

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.

Project Euler第六题,问题描述

前十个自然数的平方和是
12 + 22 + ... + 102 = 385
前十个自然数之和的平方是,
(1 + 2 + ... + 10)2 = 552 = 3025
因此,前十个自然数的平方和与和的平方之差为
3025−385 = 2640
求出前一百个自然数的平方和与和的平方之差。

解题思路

先求1~100的平方和,再求1~100和的平方,最后求差。

import math
s_sum = 0
sum_s = 0
for i in range(1,101):
    s_sum += i**2
    sum_s =math.pow(sum(range(1,101)),2)
print(int(sum_s - s_sum))

答案为:25164150

 

七,Project Euler. Problem 7:10001st prime

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?

Project Euler第七题,问题描述

通过列出前六个质数:2、3、5、7、11和13,我们可以看到第6个质数是13。
第10 001个素数是多少?

解题思路

定义一个函数,判断一个数是否为质数,然后再定义一个函数,找出第10001个。

import math
def is_prime(x):
    for i in range(2, int(math.sqrt(x) + 1)):
        if x % i == 0:
            return False
    return True

def find_prime(n):
    count = 1
    prime = []
    for i in range( 3 , 999999999 , 2 ):
        if count == n:
            print(count)
            return (print(prime[-1]))
        if is_prime(i):
            count += 1
            prime.append(i)


if __name__ == '__main__':
    find_prime(10001)

答案为:104743

 

八,Project Euler. Problem 8:Largest product in a series

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?

Project Euler第八题,问题描述

在1000个数字中,4个相连数字的最大乘积是9*9*8*9=5832,其位置如上标红处所示。

那么请找出13个相连数字的最大乘积。

解题思路

将1000个数字以字符串的形式读取到内存中,然后逐个遍历,并计算相邻12个数的乘积。保存最大的乘积即可。

with open('1000.txt','r') as f :
    data = f.read()
    print(data)
# print(len(data))
s = []
for i in range(988):
    succcc = 1
    for j in range(13):
        p = i + j
        b = int(data[p])
        succcc = succcc * b
    s.append(succcc)
print(max(s))

答案为:23514624000

 

九,Project Euler. Problem 9:Special Pythagorean triplet

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.

Project Euler第九题,问题描述

一组毕达哥拉斯数是由三个数组成,且满足勾股定理,比如3,4,5

由唯一的一组毕达哥拉斯数的和为1000,求这组数的乘积

解题思路

三个数的和为1000 ,根据勾股定理算出三个数并球其积。

for a in range(1,1000):
    for b in range(1,1000):

        c = 1000 - a - b
        if a**2 + b**2 == c**2 and a < b < c:
            print(a, b, c)
            print(a*b*c)

答案为:31875000(三个数分别为:200 375 425)

 

十,Project Euler. Problem 10:Summation of primes

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.

Project Euler第十题,问题描述

小于10的所有质数的和为2+3+5+7=17

那么小于2000000的所有质数的和为多少?

解题思路

先定义一个函数判断是否为质数,然后在定义一个函数找出2000000以内的所有质数,然后再求和。

import math
def is_prime(num):
    for i in range(2, int(math.sqrt(num) + 1)):
        if num % i == 0:
            return False
    return True

def find_prime(x):
    prime = []
    for j in range(3, x+1, 2):
        if is_prime(j):
            prime.append(j)
    return prime

if __name__ == '__main__':
    a = find_prime(2000000)
    print(a)
    sum = 2
    for i in a:
        sum += i
    print(sum)

答案为:142913828922

 

你可能感兴趣的:(Project,Euler)