24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1281  |  回复: 15
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

holmescn

金虫 (正式写手)

[交流] Euler 工程 第廿七题:系数的积已有4人参与

Euler大牛给出了一个很牛的二次公式:
引用回帖:
n^2 + n + 41

这个很牛的公式, 当n 从0取到39的时候,能给出40个质数. 可是当n=40的时候,就失灵了.

使用计算机, 我们又得到一个更牛的公式
引用回帖:
n^2 - 79n + 1601

这哥们,当n从0取到79的时候,能给出80个质数.

如果我们定义这样的一个二次公式: n^2 + an + b
a 和 b 的绝对值都小于1000, 当这个公式能产生最多的质数的时候, 给出a和b的积.

致歉:
开始译的时候,我理解错了,结果给出错误的表述,让大家产生了误解,在这里说声对不起了。

[ Last edited by holmescn on 2011-6-16 at 19:54 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

★ ★ ★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
余泽成(金币+5): 鼓励交流! 2011-06-18 16:08:02
这个只能暴力解吧,
CODE:
#include
#include
#include

bool isPrime(int n)
{
        int i;
        bool flag = true;
        for(i=2;i         {
                if(n%i==0)
                {
                        flag = false;
                        break;
                }
        }
        return flag;
}

int main(int args,char* argv[])
{
        int a=0,b=0,i,j;
        int maxlen=0, curlen,n;

        for(i=-999;i<1000;i++)
        {
                for(j=-999;j<1000;j++)
                {
                        if(!isPrime(j)) // n^2+a*n+b, b must be a prime while n==0
                                continue;

                        curlen = 1; // n==0
                        for(n=1;n<79;n++)
                        {
                                if(!isPrime(n*n+i*n+j))
                                        break;
                                curlen += 1;
                        }

                        if(curlen>maxlen)
                        {
                                maxlen = curlen;
                                a = i;
                                b = j;
                        }
                }
        }

        printf("While %d*%d=%d, (n^2+(%d)*n+%d) produces %d primes.\n",a,b,a*b,a,b,maxlen);

        return 0;
}

结果
CODE:
% While -61*971=-59231, (n^2+(-61)*n+971) produces 72 primes.
% Elapsed time is 1.578 seconds.

[ Last edited by libralibra on 2011-6-16 at 16:46 ]
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
4楼2011-06-16 16:23:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军


小木虫(金币+0.5):给个红包,谢谢回帖
引用回帖:
Originally posted by fatpig8832 at 2011-06-16 17:35:39:
汗,题目写得太令人费解了...

n^2 + n + 41 能给出前40个质数

我还以为是指从2开始的40个质数呢...后来一看不是,原来是从41开始的40个质数...后来一看又不是,原来,是指 n^2 + n + 41 这条公式给出的前40 ...

哈哈,这个题目英文说明很清楚的,

Euler published the remarkable quadratic formula:

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

Using computers, the incredible formula  n² − 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:

    n² + 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.
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
10楼2011-06-16 17:41:39
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军


小木虫(金币+0.5):给个红包,谢谢回帖
引用回帖:
Originally posted by huycwork at 2011-06-16 18:26:38:
问下,代码里面的测试数据为啥能限制在80个以内?这个上限怎么确定的呃?  

我就是根据提示的第二个牛式子观察的,80的时候,abs(b)大于1000了,所以应该结果要小于80个

[ Last edited by libralibra on 2011-6-16 at 18:52 ]
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
14楼2011-06-16 18:46:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 holmescn 的主题更新
普通表情 高级回复(可上传附件)
信息提示
请填处理意见