标题:[原创]第十二期题目之N0.2 素数的最佳解法(时间效率+空间效率)BY S.K
只看楼主
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
结帖率:100%
 问题点数:0 回复次数:8 
[原创]第十二期题目之N0.2 素数的最佳解法(时间效率+空间效率)BY S.K

/*最近忙于会考,刚刚看到第十二期的题目,马上想到了一个更好的方法,以下便是实践*/
/*计算二数之间有多少个素数 二数范围0到1000000*/
/*程序的精华部分在于使用了一个S[1000]数组*/

/*应该是最佳的算法了,达到了时间效率与空间效率最佳程度*/
/*Write BY S.K*/
#include<stdio.h>
#include<math.h>
unsigned char S[1000]={
0,168,135,127,120,119,114,117,107,110,112,106,103,109,105,102,108,98,104,94,104,
98,104,100,104,94,98,101,94,98,92,95,92,106,100,94,92,99,94,90,96,88,101,102,85,
96,86,90,95,89,98,89,97,89,92,90,93,99,91,90,94,88,87,88,93,80,98,84,99,80,81,98,
95,90,83,92,91,83,95,84,91,88,92,89,84,87,85,88,93,76,94,89,85,97,86,87,95,84,82,
87,87,81,93,87,80,91,82,92,76,91,88,83,84,81,88,82,93,81,90,79,87,88,86,88,88,
83,84,83,86,89,83,85,83,87,82,80,89,96,80,85,84,87,87,82,77,79,85,84,83,83,91,85,
90,88,77,84,85,76,88,77,85,85,84,81,83,77,80,81,83,73,87,87,81,89,79,83,75,95,73,
89,94,71,79,91,79,83,91,79,87,80,88,75,81,89,84,74,85,76,87,86,77,77,87,78,78,
77,83,83,87,85,88,84,86,69,81,86,74,76,80,84,91,78,80,81,80,83,84,76,80,89,88,84,
78,76,71,87,73,76,73,87,79,80,91,76,77,88,80,84,79,88,80,71,88,78,81,76,87,72,78,
86,76,77,73,79,84,80,78,87,94,75,78,84,78,83,71,80,83,83,74,81,73,87,85,77,72,
90,77,71,71,77,85,84,77,78,68,85,75,82,73,73,78,85,83,72,84,88,80,82,73,76,80,79,
69,86,86,76,77,84,84,81,86,79,80,81,71,87,85,73,86,73,81,80,82,72,81,77,77,84,80,
77,68,84,77,77,80,80,76,80,82,77,82,74,81,82,76,87,79,67,80,83,71,68,79,76,84,
77,77,85,79,72,68,70,76,81,73,82,85,80,71,77,83,72,76,74,81,78,80,78,69,75,84,81,
79,86,87,75,72,75,75,82,81,70,71,76,75,70,83,67,81,79,82,73,81,74,69,90,80,67,82,
85,75,75,73,77,83,81,74,71,78,71,89,76,79,84,80,85,82,73,70,75,75,79,72,85,88,82,
68,68,73,70,80,92,76,63,72,74,82,73,77,75,68,77,69,74,77,85,74,69,83,85,72,87,78,
73,78,80,86,75,69,85,71,77,78,82,75,65,63,82,78,83,78,78,76,67,82,80,87,68,81,72,
81,79,74,67,76,76,83,76,71,76,75,72,82,70,77,81,66,85,83,76,78,73,83,79,69,77,79,
84,72,70,78,80,68,79,74,72,71,87,67,78,71,73,77,78,81,68,69,73,76,77,78,79,75,71,
80,77,61,88,68,74,77,86,61,83,67,77,78,72,72,71,80,85,72,85,72,70,77,78,77,76,77,
73,79,73,78,72,81,79,87,73,68,71,67,80,77,78,77,79,73,72,73,76,73,83,76,
73,74,72,78,78,80,73,71,76,79,71,75,85,81,67,73,77,70,74,75,78,69,70,70,71,75,67,
81,77,70,82,78,73,74,59,72,77,71,68,70,86,75,74,79,73,84,61,74,85,69,78,73,71,70,
79,73,83,70,74,77,77,77,73,74,66,74,75,76,76,77,73,75,74,63,82,83,75,78,66,78,72,
74,74,82,74,79,60,79,77,73,76,77,73,79,62,72,75,71,81,71,87,68,82,74,77,77,78,76,
72,73,66,83,69,65,67,74,78,77,73,86,75,69,76,75,76,75,69,76,71,75,74,79,69,78,70,
81,67,74,73,67,64,67,76,71,76,72,68,85,73,71,83,70,66,68,79,77,77,80,68,79,72,82,
78,68,77,74,77,75,70,76,72,67,70,76,81,71,70,82,68,75,75,77,70,73,80,68,78,71,82,
71,73,79,77,72,71,71,85,66,70,69,78,79,68,70,69,78,78,72,69,72,78,69,75,75,62,83,
75,72,84,78,71,81,78,69,69,68,76,79,82,68,67,73,71,64,80,69,70,69,83,68,
78,70,69,77,75,68,70,77,74,66,71,73,78,76,69,71,77,74,83,60,80,80,68,78,80,73,79,
58,76,65,75,80,75,67,78,75,80,69,72,73,69,77,76,71,77,68,68,80,69,72,74,80,64,75,
76,61,74,73,70,63,81,70,80,80,79,82,62,81,71,54,73,70,72,79,75,71,72,72,72,81,
76,80,74,63,70,80,69,69,76,68,81,68,71,71,72,68,74,79,72,76,73,66,72,66,67,75,76,
70,78,65,73,76,58,69,77,69,68,88,71,74,74,70,73,66,75,73,76,78,74,63,85,70,66,60,
80,65,67,75,70,70,70,76,76,63,71,72,71,79,65,68,78,69,69,83,74};

long m=0;
long n1,n2;
long i,k;

void sumx(long n1,long n2)
{
for(i=n1;i<n2;i++)
{
m++;
for(k=2;k<=sqrt(i);k++)
if(i%k==0)
{
m--;
break;
}
}
}

int main(void)
{

scanf("%ld%ld",&n1,&n2);
if(n1/1000!=n2/1000)
{
for(i=n1/1000+2;i<=n2/1000;i++) m+=S[i];
sumx(n1,(n1/1000+1)*1000);
sumx(n2/1000*1000,n2+1);
}
else
sumx(n1,n2+1);
if(n1==1) m--;
printf("%ld",m);
getch();
return 0;
}

搜索更多相关主题的帖子: 时间效率 空间效率 素数 解法 
2007-04-30 20:24
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
得分:0 
算这个unsigned char S[1000];不容易吧.

倚天照海花无数,流水高山心自知。
2007-04-30 20:29
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
得分:0 

回楼上:
还可以,搞了个简单的程序算了好几秒.
就是每一千个数字间素数的个数表


My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2007-04-30 20:35
crackerwang
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:833
专家分:0
注 册:2007-2-14
得分:0 
顶.
我当时做的时候也想列出来但是我没有想全列出来只是列出阶段部分在利用查找.
后来刚脆列出前1000个素数

2007-04-30 21:18
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
得分:0 

你把总的时间加起来,看花了多少时间,另外移植性太差,不提倡这样做


雁无留踪之意,水无取影之心
2007-04-30 22:25
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
得分:0 
回楼上:
可是这样的ACM或OI题目都是在几小时内做完一个程序,这个程序如果可以完善到最佳的速度,那么完全可以在这几小时的时间内做出预先计算,这不违过,这叫优化
用较小的空间做出巨大的作用,在各类尤其是趋向于算法的程序中有很大的应用,如Openbook等等

My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2007-05-01 08:47
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
得分:0 
你搞这么高的效率为非是为了算出很大的素数,但你的范围也太小了,你不如写个100亿以下的素数,那样才能体现出你算法的精妙,1000000也太普通了

[此贴子已经被作者于2007-5-1 9:55:50编辑过]



雁无留踪之意,水无取影之心
2007-05-01 09:55
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
得分:0 
首先我声明:我不希望与楼上展开舌战,观点不同不至于如此,大家可以都坚持自己的观点.
做出100亿以下的素数是完全可能的,但根据题目要求我已经作到了,不需要画设添足到100亿,我没有吹说我的算法如何精妙,只是给大家一个不同的思想,原题中也缀了一句"尽情发挥",且100亿的空间也是确实并不需求较大的,时间与空间的综合考虑是"最大数据的根号",100亿需要S[10000],对于我是可以算出的,数据占用空间也可以接受,但对于原题是无意义的.

希望你 如果对我的算法还有什么异议,请通过短消息讨论,而不要导致技术论坛中的无意义争执.

最后欢迎寻找到更好的算法,欢迎踊跃交流!

My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2007-05-01 19:49
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
呵呵,我有个建议:去看看计算机密码学的大素数生成算法,你会很有启发。

淘宝杜琨
2007-05-01 19:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-136311-1-1.html




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

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.520399 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved