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 = 41, 412 + 41 + 41 is clearly divisible by 41.

The incredible formula  n2 ? 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients,
?79 and 1601, is ?126479.

Considering quadratics of the form:

n2 + an + b, where |a| < 1000 and |b| < 1000

where |n| is the modulus/absolute value of n

e.g. |11| = 11 and |?4| = 4

Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting
with n = 0.

a,b分别从[-999,999]之间取值,找到满足条件的a,b使得n按顺序取0,1,2,3,4,...的情况下连续生成的质数序列最长。

#include <iostream>
using namespace std;

bool prim(int a)
{
	if (a < 2)
		return false;
	for (int i = 2; i*i <= a; i++)
	{
		if (a%i == 0)
			return false;
	}
	return true;

}

int main()
{
	int maxcount = 0;
	int res = 0;
	for (int a = -999; a <= 999; a++)
	{
		for (int b = -999; b <= 999; b++)
		{
			int n = 0;
			while (true)
			{
				int tmp = n*n + a*n + b;
				if (prim(tmp))
					n++;
				else
					break;
			}
			if (n > maxcount)
			{
				maxcount = n;
				res = a*b;
			}
		}
	}
	cout << res << endl;
	system("pause");
	return 0;
}
时间: 2024-10-08 01:09:41

Project Euler:Problem 27 Quadratic primes的相关文章

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

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

Project Euler:Problem 77 Prime summations

It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + 2 3 + 3 + 2 + 2 2 + 2 + 2 + 2 + 2 What is the first value which can be written as the sum of primes in over five thousand different ways? #include <i