Problem 27 Quadratic primes (暴力枚举)

本文探讨了如何通过枚举方法找到一个特定形式的二次多项式,该多项式能为连续的自然数输入生成最大数量的质数。文章提供了一个C++实现示例,用于找出系数a和b的最佳组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Quadratic primes

Problem 27

Euler discovered the remarkable quadratic formula:

n2+n+41n2+n+41

It turns out that the formula will produce 40 primes for the consecutive integer values 0n390≤n≤39. However, when n=40,402+40+41=40(40+1)+41n=40,402+40+41=40(40+1)+41 is divisible by 41, and certainly when n=41,412+41+41n=41,412+41+41 is clearly divisible by 41.

The incredible formula n279n+1601n2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0n790≤n≤79. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

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

where |n||n| is the modulus/absolute value of nn
e.g. |11|=11|11|=11 and |4|=4|−4|=4

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


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值