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 ...
随机推荐
- [SCOI2012]喵星球上的点名(树状数组+后缀数组)
我们把所有的名,姓,询问都拼起来构成一个新的长串,然后跑一边SA.排完序后对于每一个询问,我们可以二分求出它所对应的区间(即满足这个区间的前缀都是这个询问串).然后问题就转化为很多区间问区间出现过的不 ...
- PL SQL Developer使用总结
如果OS为windows 7 64位系统,Oracle版本为 Oracle 11g 64 安装PL SQL Developer 请参考 http://myskynet.blog.51cto.co ...
- kubernetes 项目
1:CI/CD Docker: Harbor Git Jenkins 2:微服务 istio
- pandas 8 画图
from __future__ import print_function import pandas as pd import numpy as np import matplotlib.pyplo ...
- Visual Studio 2013 无法创建MVC项目,系统找不到指定的文件.(Exception from HRESULT:08x0070002)
在Visual Studio 2013中创建新MVC项目,(PS:现在创建个MVC项目,差点都找不到在哪,汗!-) 确定后提示,系统找不到指定的文件.(Exception from HRESULT:0 ...
- "pom.xml" could not be activated because it does not exist.
"pom.xml" could not be activated because it does not exist. 在sts中使用maven build,输入package然后 ...
- android自己定义圆盘时钟
自己定义圆盘时钟的大概流程:由于圆盘时钟的圆盘是不须要动的,所以不必要加在自己定义的view里面,在view里面仅仅须要绘制秒针和分针时针并控其转动就可以. 下面就是自己定义view的主要代码: pa ...
- HDU 3016 Man Down(线段树)
HDU 3016 Man Down 题目链接 题意:是男人就下100层的游戏的简单版,每次仅仅能从两端下落.求落地最大血量 思路:利用线段树能够处理出每一个线段能来自哪几个线段.然后就是dag最长路了 ...
- 51nod-1322: 关于树的函数
[传送门:51nod-1322] 简要题意: 给出n个点的两棵无根树,编号都是从0到n-1 现在每棵树任意选出一条边割断,设第一棵树选出的边为e1,第二棵树选出的边为e2 很显然割断后两棵树各分成了四 ...
- bzoj1433: [ZJOI2009]假期的宿舍(最大二分图匹配)
1433: [ZJOI2009]假期的宿舍 题目:传送门 题解: 这题有点水 跑个二分图匹配就完事了(注意在校生不是一定都互相认识) 代码: #include<cstdio> #inclu ...