| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 2538 人关注过本帖
标题:在google treasure hunt 上看到的关于素数的问题,不是怎样找素数的问题!
取消只看楼主 加入收藏
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
收藏
已结贴  问题点数:100 回复次数:6 
在google treasure hunt 上看到的关于素数的问题,不是怎样找素数的问题!
承认自己很难得的开始对素数开始感兴趣了!!!

于是就在网上找各种关于素数的理论,还尼玛跑到亚马逊上买了本叫什么

 “ 对素数感兴趣的人们,费玛定理神码神马的。。。。。”

后来在 偶然进入Google treasure hunt之后

给我出了一道题是关于一个素数可以用那些连续的素数表示出来的问题,具体问题如下


Find the smallest number that can be expressed as

    the sum of 19 consecutive prime numbers,

    the sum of 21 consecutive prime numbers,

    the sum of 405 consecutive prime numbers,

    the sum of 781 consecutive prime numbers,

    and is itself a prime number.
For example, 41 is the smallest prime number that can be expressed as

the sum of 3 consecutive primes (11 + 13 + 17 = 41) and

the sum of 6 consecutive primes (2 + 3 + 5 + 7 + 11 + 13 = 41).


让我来找这个最小的素数

自己不知道为什么特想知道这题的解题方法,因为很明显

这道题在让人思考怎样从庞大的数据种找到自己需要的东西,

于是就被吸引住了,如果有那位高人指点一下迷津,不慎感激,把我四分之三的分送出去

The quieter you become, the more you can hear
2012-06-18 01:15
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
回复 3楼 zklhp
恩,比如2,3,5,7就是连续4个素数
17就可以用上面这四个素数表示
然后看17还可不可以用其他的连续素数表示

The quieter you become, the more you can hear
2012-06-18 12:34
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
回复 5楼 有容就大
多谢仁兄关注,其实我倒是挺想和大家一起讨论一下关于质数的各种各种各种的,虽然自己懂得也不多

The quieter you become, the more you can hear
2012-06-18 13:02
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
回复 9楼 beyondyf
非常非常感谢版主的代码,我会花时间仔细看的,

比如输入19,21, 405, 就会产生19个,21个405个连续素数的和,

问题的要求是这些和应该相同,所以是不是要继续做判断,

直到找到和相同的那个最小的素数吧

The quieter you become, the more you can hear
2012-06-18 18:08
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
下面是一个找素数的方法,用的是链表,把找到的素数串起来,

判断一个数是不是素数是就看他能不能被链表里的素数整除,

如果不能的话,那他就是素数了。这和版主杨大哥用的数组来存储的方法应该是一样的了

只是数组需要一开始就把大小定下来,链表确不用

因为任何一个合数都可以被若干个素数表示出来滴

下面是我看到的一个方法

程序代码:
typedef unsigned long long bignum;

struct primerec {
    bignum bn;
    struct primerec *next;
};

void findPrimes(bignum topCandidate) {

    printf("2\n");

    struct primerec *firstPrime = malloc(sizeof(struct primerec));

    assert(firstPrime != NULL);

    struct primerec *latestPrime = firstPrime;

    firstPrime->bn = 3;

    firstPrime->next = NULL;

    bignum candidate = 3;

    while(candidate <= topCandidate) {

        struct primerec *thisPrime = firstPrime;

        int prime = 1;

        while(thisPrime->bn * thisPrime->bn <= candidate) {

            if(candidate % thisPrime->bn == 0) {

                prime = 0;

                break;

            }

            thisPrime = thisPrime->next;

        }

        if(prime ) {

            printPrime(candidate);

            latestPrime->next = malloc(sizeof(struct primerec));

            assert(latestPrime->next != NULL);

            latestPrime = latestPrime->next;

            latestPrime->bn = candidate;

            latestPrime->next = NULL;

        }

        candidate += 2;

    }

    freeList(firstPrime);
}



我看了一下

$ time ./printPrime 10000000 > /dev/null

        3.92 real         3.91 user         0.00 sys

这个方法还算可以吧,

只是 ‘%’ 是个相当笨重的运算符, 所以如果能把它换掉就更好了

The quieter you become, the more you can hear
2012-06-18 18:58
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
回复 12楼 beyondyf
可是。。。可是。。

Find the smallest number that can be expressed as

    the sum of 19 consecutive prime numbers,

    the sum of 21 consecutive prime numbers,

    the sum of 405 consecutive prime numbers,

    the sum of 781 consecutive prime numbers,
这段话的意思应该是

找到最小的这样一个数,它可以被 19个,21个,405个,781个连续的素数表示出来


The quieter you become, the more you can hear
2012-06-18 19:02
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
回复 17楼 beyondyf
好吧,70分归你!30分就留给咱们吧!非常非常感谢,我会花时间把版主的代码仔细推敲的!

The quieter you become, the more you can hear
2012-06-18 22:56
快速回复:在google treasure hunt 上看到的关于素数的问题,不是怎样找素数的问 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015108 second(s), 8 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved