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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 11:17:27

Project Euler:Problem 58 Spiral primes的相关文章

Project Euler:Problem 27 Quadratic primes

Euler discovered the remarkable quadratic formula: n2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n

Project Euler:Problem 37 Truncatable primes

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Fi

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 first three consecutive numbers to have three distinct prime factors are: 644 = 22 × 7 × 23 645 = 3 × 5 × 43 646 = 2 × 17 × 19. Find the first four co

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

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 formed as follows: 21 22 23 24 25 20  7  8  9 10 19  6  1  2 11 18  5  4  3 12 17 16 15 14 13 It can be verified that the sum of the numbers on the diagona

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number