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 ...
随机推荐
- kali(Ubuntu)右键添加idle打开方式
IDLE可以说是Unix平台下Python的第一个集成开发环境(IDE) 命名行输入idle看idle是否已安装,没有则先安装 安装idle:apt-get install idle 安装完成后,命名 ...
- 关于Python的装饰器
false 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {m ...
- (2016北京集训十四)【xsy1556】股神小D - LCT
题解: 题解居然是LCT……受教了 把所有区间按照端点排序,动态维护目前有重叠的区间,用LCT维护即可. 代码: #include<algorithm> #include<iostr ...
- WebKit.NET-0.5简单应用(2)——音量解决方案
查找WebKit.NET相关文档,没有找到音量控制解决方法.换思路进行解决,尝试用Win32 API进行解决 [DllImport("winmm.dll")] public sta ...
- 洛谷2114 bzoj3668[NOI2014]起床困难综合症
题目描述 21世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm一直坚持与起床困难综合症作斗争.通过研究相关文献,他找到了该病的发病原因 ...
- windows下安装redis以及测试 Window 下安装
下载地址:https://github.com/dmajkic/redis/downloads. 下载到的Redis支持32bit和64bit.根据自己实际情况选择,将64bit的内容cp到自定义盘符 ...
- js 函数基础(方便复习使用)
// 函数声明: function bbq(){ // ..... } // 函数表达式: // 1.命名函数表达式 var test = function abc(){ document.write ...
- Java基础学习总结(30)——Java 内存溢出问题总结
Java中OutOfMemoryError(内存溢出)的三种情况及解决办法 相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各 ...
- JVM分代通俗解释
JVM分代通俗解释 学习了:https://www.cnblogs.com/zgghb/p/6428395.html
- hdoj Let the Balloon Rise
/*Let the Balloon Rise Problem Description Contest time again! How excited it is to see balloons ...