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 ...
随机推荐
- Eclipse安装Web插件
方法/步骤 本次安装教程,我把所有的步骤都写在了图片中,大家仔细查看图片即可,希望能帮到大家 1.选择菜单栏上的“Help” 选择Install New Software 在弹出的 ...
- TP5防sql注入、防xss攻击
框架默认没有设置任何过滤规则 可以配置文件中设置全局的过滤规则 config.php 配置选项 default_filter 添加以下代码即可 // 默认全局过滤方法 用逗号分隔多个 'default ...
- luogu P4062 [Code+#1]Yazid 的新生舞会(线段树+套路)
今天原来是平安夜啊 感觉这题是道好题. 一个套路枚举权值\(x\),把权值等于\(x\)的设为1,不等于的设为-1,然后问题转化为多少个区间权值和大于. 发现并不是很好做,还有一个套路,用前缀和查分来 ...
- 紫书 例题8-11 UVa 10954 (优先队列)
解法和合并果子是一样的, 每次取最小的两个, 更新答案, 加入队列 #include<cstdio> #include<queue> #define REP(i, a, b) ...
- C语言中头文件尖括号和引号的区别
用include 引用头文件时,双引号和尖括号的区别: 1.双引号:引用非标准库的头文件,编译器首先在程序源文件所在目录查找,如果未找到,则去系统默认目录查找,通常用于引用用户自定义的头文件. 2.尖 ...
- LaTeX 写算法伪码
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50054953 LaTeX写算法伪码,需 ...
- POJ——T 3687 Labeling Balls
http://poj.org/problem?id=3687 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14842 ...
- 推断扫描后的内容是否是URL
扫描的明明是Url.竟然当文本给处理了,看来正则没有通过. 扫描二维码后,我參考了QQ的效果.分了三种:网页地址.文件下载地址,文本信息:为了实现这样的效果.我 发现有非常多url非常奇葩.所以就想找 ...
- 三层登录—c#
学习了三层,有一个登录窗口的小练习.是我们第一次接触三层的初战.如今仅仅是简单的了解了一些,须要学习的还有非常多,以下浅谈自己的理解. 我们说的三层就是分层了显示层.业务逻辑层和数据訪问层.当中显示层 ...
- 【每日一记】unity3d 图片置灰shader
项目须要.參考了一下网上的资料,写了一个shader,给button置灰. shader详细实现例如以下: 2016.07.25补充,支持mask剪裁 Shader "Transparent ...