0
点赞
收藏
分享

微信扫一扫

Problem 27 Quadratic primes (暴力枚举)


Quadratic primes


Problem 27

Euler discovered the remarkable quadratic formula:

It turns out that the formula will produce 40 primes for the consecutive integer values . However, when  is divisible by 41, and certainly when  is clearly divisible by 41.

The incredible formula  was discovered, which produces 80 primes for the consecutive values . The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

, where  and 

where is the modulus/absolute value of
e.g. and

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




Answer:

-59231

Completed on Fri, 28 Oct 2016, 17:25


题意:二次质数。欧拉发现了著名的二次方程:n² + n + 41。当n=0~39时,这个方程能够连续产生40个质数。但是,当n=40时,402 + 40 + 41 = 40(40 + 1) + 41可以被41整除,当然,还有当n = 41, 41² + 41 + 41 可以被41整除。

发现了更奇妙的方程: n² – 79n + 1601,当n=1~79时,它能够 连续产生80个质数。方程系数的积为 –79 * 1601 = -126479.

考虑二次方程的形式:

n² + an + b, |a| < 1000 且 |b| < 1000

|n| 是n 的绝对值。

找到系数a和b,当n从0开始连续递增时,可以产生最多的质数。给出a*b的值。

暴力枚举吧....


代码:


#include<bits/stdc++.h> 
using namespace std;
int isprime(int n)
{
if (n<=1)return 0;
for(int i=2;i<=sqrt(n);i++)
if(n%i==0)return 0;
return 1;

}
int gao(int a, int b) //连续素数的个数
{
int cnt = 0;
for (int i=0;;i++)
{
if (isprime(i*i+a*i+b)) //二元产生的是素数
{
cnt++;
}
else break;
}
return cnt;
}

int main()
{
int maxa,maxb,maxprimes=0;
for (int a=-1000;a<1000;a++)
{
for(int b=-1000;b<1000;b++)
{
if(gao(a,b)>maxprimes)
{
maxa = a;
maxb = b;
maxprimes = gao(a,b);
}
}
}
cout<<maxa*maxb<<endl;
return 0;
}




举报

相关推荐

0 条评论