Project Euler:Problem 58 Spiral primes
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.
37 36 35 34 33 32 31
38 17 16 15 14 13 30
39 18 5 4 3 12 29
40 19 6 1 2 11 28
41 20 7 8 9 10 27
42 21 22 23 24 25 26
43 44 45 46 47 48 49
It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime;
that is, a ratio of 8/13 ≈ 62%.
If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square
spiral for which the ratio of primes along both diagonals first falls below 10%?
这题是28题的一个扩展,相同找规律,然后推断质数即可了
#include <iostream>
#include <string>
using namespace std; int cp[100000000]; bool isPrime(int n)
{
for (int i = 2; i*i < n; i++)
{
if (n%i == 0)
return false;
}
return true;
} void count_prime(unsigned long long n)
{
cp[n] = cp[n - 1];
int a[3];
a[0] = (2 * n + 1)*(2 * n + 1) - 4 * n;
a[1] = (2 * n + 1)*(2 * n + 1) - (2 * n + 1) + 1;
a[2] = (2 * n + 1)*(2 * n + 1) - 6 * n;
for (int i = 0; i < 3; i++)
{
if (isPrime(a[i]))
cp[n]++;
}
} int main()
{
memset(cp, 0, sizeof(cp));
cp[0] = 0;
unsigned long long ans;
double a, b, res;
for (unsigned long long i = 1; i < 100000000; i++)
{
count_prime(i);
a = cp[i] * 1.0;
b = (4 * i + 1)*1.0;
res = a / b*1.0;
cout << res << endl;
if (res < 0.10)
{
ans = 2 * i + 1;
break;
}
}
cout << ans << endl;
system("pause");
return 0;
}
Project Euler:Problem 58 Spiral primes的更多相关文章
- Project Euler:Problem 37 Truncatable primes
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...
- Project Euler:Problem 47 Distinct primes factors
The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...
- Project Euler:Problem 28 Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- Project Euler:Problem 63 Powerful digit counts
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...
- Project Euler:Problem 86 Cuboid route
A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...
- Project Euler:Problem 76 Counting summations
It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...
- Project Euler:Problem 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...
- Project Euler:Problem 89 Roman numerals
For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...
随机推荐
- 搭建ss总结
今天晚上做的事情: 1. https://www.vultr.com/ 购买vps 2. ssh连接到服务器 参照网上帖子安装 https://blog.csdn.net/littlepig19930 ...
- go语言简单的执行shell命令
package main import( "fmt" "os/exec" "os" "string ...
- CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...
- 【Tool】Linux下的Spark安装及使用
1. 确保自己的电脑安装了JAVA Development Kit JDK, 用来编译Java应用, 如 Apache Ant, Apache Maven, Eclipse. 这里是我们安装Spark ...
- camke 参数
cmake -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \ -DMYSQL_DATADIR=/application/mysql-5.5.32 ...
- Ubuntu 16.04 Chrome浏览器安装flash player插件
1:官网下载插件 flash palyer lash_player_npapi_linux_debug.x86_64.tar.gz 2:解压 提取 libpepflashplayer.so 3:手动 ...
- 紫书 例题8-8 UVa 1471 (用set实现动态二分)
设切割的区间为(j, i), 注意两边都是开区间. 然后可以预处理出以i为起点的最长连续递增的长度和以j为终点的最长连续递增的长度. 大致思路就是枚举i,右边这一侧的最优值就知道了, 然后这道题的关键 ...
- 【codeforces 816A】Karen and Morning
[题目链接]:http://codeforces.com/contest/816/problem/A [题意] 让你一分钟一分钟地累加时间; 问多长时间以后是个回文串; [题解] reverse之后如 ...
- BNUOJ 34990 Justice String
Justice String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java cla ...
- CentOS6.5安装redis(3.0.3)
如果没有安装gcc需要安装gcc 才能编译成功 yum install gcc 离线安装gcc的方法 # rpm -ivh mpfr-2.4.1-6.el6.x86_64.rpm # rpm -i ...