Google Treasure Hunt 2008 Question:Primes
来源:互联网 发布:分配网络地址 linux 编辑:程序博客网 时间:2024/06/05 19:42
这个问题的题目是:
Find the smallest number that can be expressed as
the sum of 7 consecutive prime numbers,
the sum of 21 consecutive prime numbers,
the sum of 481 consecutive prime numbers,
the sum of 851 consecutive prime numbers,
and is itself a prime number.
For example, 41 is the smallest prime number that can be expressed as
the sum of 3 consecutive primes (11 + 13 + 17 = 41) and
the sum of 6 consecutive primes (2 + 3 + 5 + 7 + 11 + 13 = 41).
我解决这个问题的思路是先生成一个一定数量的质数的集合,我从网上这个地址下载了些质数。然后分别计算出7个,21个,481个,851个连续质数的和的集合,然后它们五个集合的交集中的最小的数就是答案了。我用c来做的。尽管做出了答案5614957,但我发现了许多问题。我到底需要多少个质数,如果数的大小超出整型的表示范围,内存的使用量太大。
网上有许多充满了创意的方法。一个朋友只用一些unix工具就解决了问题。它是这样做的:
第一步:从网上下载质数
$ for i in $(seq 50); do wget "http://primes.utm.edu/lists/small/millions/primes$i.zip"; done
第二步:解压文件
$ for i in $(seq 50); do unzip "primes$i.zip" && rm -f "primes$i.zip"; done
第三步:把质数按一行一个存储到一个文件中
for i in $(seq 50); do (awk 'BEGIN { OFS="/n" } NR > 2 {print $1,$2,$3,$4,$5,$6,$7,$8}' primes$i.txt >> primes.txt) && rm -f primes$i.txt; done
第四步:确定处理过程中没有遗漏
$ wc -l primes.txt
输出:50000000 primes.txt
第五步:创建四个文件,分别存储7,17,41,541个连续质数的和
$ last=$(tail -1 primes.txt)
$ for N in 7 17 41 541
do
awk 'BEGIN { prev[0] = 0 } NR < '$N' {prev[NR] = $1; sum += $1 } NR >= '$N' { psum += prev[NR-'$N']; delete prev[NR-'$N']; prev[NR] = $1; sum += $1; if (sum - psum > '$last') { exit } printf "%d/n", sum - psum }' primes.txt > primes$N.txt
done
第六步:求交集
$ sort -nm primes541.txt primes41.txt | uniq -d | sort -nm primes17.txt - | uniq -d | sort -nm primes7.txt - | uniq -d
第七步:确定结果是质数
$ grep -m1 7830239 primes.txt
我知道这样做要很长时间,但至少应该比我的短。虽然我的程序只用了不到一秒,但编写代码花了很多时间。所以这是个充满创意,还不错的方法。我还发现了一个更好的方法。用python编写,使用了python里面的迭代器。这个问题可以当做使用python迭代器的经典范例。我在这篇文章中来说明这个方法。
- Google Treasure Hunt 2008 Question:Primes
- 用python解决 Google Treasure Hunt 2008 Question:Primes
- Google Treasure Hunt 2008 Question:Robot
- Google Treasure Hunt 2008
- About Google Treasure Hunt 2008
- Google Treasure Hunt 2008--Robot Problem
- Google Treasure Hunt 2008开始啦!
- Google Treasure Hunt 2008 2008/12/20 All Solved
- Google Treasure Hunt 2008---Find the Smallest Prime Number
- pku1066 Treasure Hunt
- poj 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- ceoi 2011 treasure hunt
- zoj 1158 Treasure Hunt
- POJ 1066 Treasure Hunt
- poj 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- 【Stones from other hills】Code Review中的几个提示
- 求助一循环求解的问题,急
- coolite+mvc面临抉择
- 利用 vimtutor 快速学习 gvim
- DataList 控件 和 Repeater 控件 轻松分页小结
- Google Treasure Hunt 2008 Question:Primes
- 如何在WinCE中添加快捷方式
- 使用Matplotlib画洛伦兹吸引子
- 关于使用ADOQUERY查询后报E_FAIL错误
- 在ubuntu 下美化eclipse,myeclipse
- .net弹出提示框无刷新
- 【转】谈谈高速数字系统的滤波电容
- Flash图片处理—颜色变换
- 应用启动时加载XML文档(Digester的用法)