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的更多相关文章

  1. Project Euler:Problem 37 Truncatable primes

    The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...

  2. 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 ...

  3. 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 ...

  4. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ZBrush中常用笔刷综合简介

    单击左托盘的笔刷图标,弹出一个笔刷库,其中有许多常用笔刷,这也是许多初学者所头疼的问题,ZBrush的笔刷非常多,而且功能很强大,好多朋友不知道该选择哪一个笔刷进行雕刻.其实,在ZBrush的学习中我 ...

  2. Java线程之Java内存模型(jmm)

    一.Java内存模型(jmm) 线程通信 消息传递 重排序 顺序一致性 Happens-Before As-If-Serial

  3. JAVA jsp page指令的属性 errorPage 和isErrorPage

    >errorPage指定当前页面出现错误的实际响应页面是什么, 其中“/” 表示的是当前WEB应用的根目录 <% page errorPage="/error.jsp" ...

  4. ztree实现根节点右击事件,显示添加删除

    需求,右击树节点,出现编辑和删除的提示框 1:在setting 配置里面,给callback设置,右击事件onRightClick: 2:写一个函数onRightClick function onRi ...

  5. BNUOJ 36005 Chemical Reaction

    Chemical Reaction Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on OpenJudge. ...

  6. (原创)VS2017 C# 运行 Javasrcipt RSA 加密用户名登录 Java开发的服务器

    第一次写博客. 最近想做一个Web的自动登录,用户名和密码是RSA加密过的,后台是用的JAVA,我只会点C#,抓包什么都搞定了(使用的是Fiddler),不过由于C#和RSA的加密方式不同,我搞了N天 ...

  7. POJ 3254 Corn Fields 状态压缩DP (C++/Java)

    id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...

  8. 【Struts2学习笔记(12)】Struts2国际化

    (1)准备资源文件,资源文件的命名格式例如以下: baseName_language_country.properties baseName_language.properties baseName. ...

  9. poj 2154 Color(polya计数 + 欧拉函数优化)

    http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...

  10. poj_2352树状数组

    因为y已经排好序了,用x坐标建立一维树状数组 #include<iostream> #include<cstdio> #include<cstring> using ...