欢迎光临散文网 会员登陆 & 注册

Project Euler 006~010

2020-05-22 13:59 作者:加餐勺  | 我要投稿


Leonhard Euler(1707.4.15-1783.9.18)

关于啥是Project Euler 详见 https://projecteuler.net/about

观前声明: 

  1.   这是个人兴趣使然的对自己入坑pe的记录,仅提供思路和部分代码;各个方面肯定都是有着优化与提升空间的,甚至在许多大佬看来这应该是初学者的浅薄而未经剪枝的丑码,如果能帮到有兴趣的人自是最好,也欢迎能醍醐灌顶的深度讨论。

  2. 大佬看到了笑笑就行,还请轻喷。

  3. 带着恶意,抬杠者...俺也喷不过您,也不能拿您咋样...毕竟这只是我个人兴趣使然的行为或者说是学习记录分享。 (说是记录,但因为是早先写的所以其实是在各种意义上公开处刑和吐槽自己 并尝试补救优化)

  4. 语言是c++,用的VS平台

第六题:

Sum square difference

Problem 6

The sum of the squares of the first ten natural numbers is,

1^2+2^2+...+10^2=385

The square of the sum of the first ten natural numbers is,

(1+2+...+10)^2=55^2=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

前100位自然数和的平方减去平方的和。

水题. 手算

和的平方:[n(n+1)]^2/4  平方的和:n(n+1)(2n+1)/6

答案25164150

第七题:

10001st prime

Problem 7

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位质数。

水题;

数据不算大.效率较低的用一个个因子判断质数的方法对每一个数进行判断都能跑出来,姑且给一个未经优化的筛子模板:

void getprime(void)

{

prime[0] = prime[1] = 1;

for (int i = 2; i < maxn; i++)

if (!prime[i])

for (int j = i + i; j < maxn; j += i)

prime[j] = 1;

}

在全局范围用数组prime[n]储存自然数n是否为质数,是则prime[n]=0,否则1;

初始时设置都为0,循环每个数字,如果prime[n]等于0,那么将它的倍数记录为合数;

maxn足够大时便可保证10001位质数在数组内。

答案是104743

第八题:

Largest product in a series

Problem 8

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个数字相乘得到的最大值是5832,那么任意连续的13个数字相乘的最大值是多少。

据说有人肉眼看出来了

俺这暂时没啥好的优化,所幸作为水题暴搜起来也很快,(把这东西当成字符串),遍历i从第1位到第988位算出每个i~i+12位的乘积,不断比较替换最大值即可

for (i = 0; i <=987; i++)

{

sum = 1;

string As = A.substr(i, 13);

for (j = 0; j < 13; j++)

sum *= As[j] - '0';

if (sum > valve)valve = sum;

}

大概是这么个样子.怕溢出的话sum,valve都用long long int定义

答案是23514624000

第九题:

Special Pythagorean triplet

Problem 9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a^2 + b^2 = c^2

For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

只有一组勾股数满足a<b<c,a+b+c=1000,求出abc的值

初中数学题的难度. 暴搜也可;

用有关勾股数生成的公式基本可以猜出来 

如果勾股数a,b,c的最大公因数gcd(a,b,c)=1 (Greatest Common Divisor)

那么则存在m,n, 使a=2mn,b=m^2-n^2,c=m^2+n^2,且m>n,m,n互质,m,n一奇一偶

(相关证明自行百度.)

而所有的勾股数可以由此派生   a=t*2mn,b=t*(m^2-n^2),c=t*(m^2+n^2)

a+b+c=2m(n+m)确定m,n上限后连蒙带猜还挺快

那么将1000分解一下有1,2,4,5,8,10,20,25,40,50,100,125,250,500,1000;其中只有a+b+c=40的时候a=8,b=15,c=17(m=4,n=1)满足条件所以t*[a b c]就是解

答案为a=25*8=200,b=25*15=375,c=25*17=425   abc=3187500

当然 暴搜的话写起来也是秒出:

#include<iostream>

using namespace std;

int main()

{

int a, b;

for (a = 1; a < 1000; a++)

for (b = a + 1; b < 1000; b++)

if (a * a + b * b == (1000-b-a) *(1000-a-b))

{

cout << a * b * (1000-a-b) << endl;

cout << a << " " << b << " " << 1000-a-b;

break;

}

return 0;

}

其实a的上界是333,b的上界是500..懒得改优化了

第十题:

Summation of primes

Problem 10

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

Find the sum of all the primes below two million.

求出2000000以下的所有质数的和

水题. 筛子见第七题.

答案是142913828922

又水了一期 .有缘再见

Project Euler 006~010的评论 (共 条)

分享到微博请遵守国家法律