〖饼子堂比武贴〗素数着色表,有本事就请来SHOW一把,看看你的数学学成咋样?!

soft_2008 2006-07-01 09:10:54
偶数,奇数大家知道吧,那就直接入正题吧:

以ASP实现,输出数列1-1000的表格,逢十换行,凡是素数的格子背景色均改为淡绿色,统计素数总个数,

统计尾数为1的素数总个数,并输出,

统计尾数为3的素数总个数,并输出,

统计尾数为7的素数总个数,并输出,

判断(尾数为1的素数之和) 与 尾数为3、7的素数之“和”及“差”的逻辑关系,

判断(尾数为3的素数之和) 与 尾数为1、7的素数之“和”及“差”的逻辑关系,

判断(尾数为7的素数之和) 与 尾数为1、3的素数之“和”及“差”的逻辑关系。

虽然是训练题,但看谁写的最高效准确,SHOW一下你的程序功底吧,呵呵~~欢迎挑战!

...全文
1393 55 打赏 收藏 转发到动态 举报
写回复
用AI写文章
55 条回复
切换为时间正序
请发表友善的回复…
发表回复
soft_2008 2006-07-12
  • 打赏
  • 举报
回复
已结帐
shanshuiboy 2006-07-11
  • 打赏
  • 举报
回复
DDDDDDD
slawdan 2006-07-11
  • 打赏
  • 举报
回复
ASP里位操作不爽,所以也没人这样去想去用~

C就有很多人直接用用移位处理数据的~
soft_2008 2006-07-07
  • 打赏
  • 举报
回复
几天忙,看了一下楼上各位的编码,报测试数据时请附上机子的配置,这样才算公平!!

抽时间看了一个通过字符移位来判断的例子,根本就打破了惯有的思维方式,不知那位仁兄可以把它完整实现出来??按经验看,这种方式的潜力很巨大...

重在参与,请饼子堂的兄弟也勇跃参加,本人允诺只使用"饼子堂"一次..因为这帖子构想是,比较传统数据处理方式和字符越位硬性规则的效率碰撞..但遗憾的是,尚未看到传统处理方式以外的实现...

net205 2006-07-07
  • 打赏
  • 举报
回复
试水....
lexchou 2006-07-05
  • 打赏
  • 举报
回复
<script language="jscript" runat="server">
var timestamp_begin = (new Date()).getTime();
prime(1000,[1,3,7]);//计算1000个数,统计1,3,7
var timestamp_end = (new Date()).getTime();
Response.Write('总消耗时间'+(timestamp_end-timestamp_begin)+'ms');


function prime(n,ending_digits)
{
var IsNotPrime=new Array(n+1);
var ending=[0,0,0,0,0,0,0,0,0,0];
var base=ending_digits.length;
//识别与统计
for(i=2;i<IsNotPrime.length;i++)
{
for(j=i*2;j<IsNotPrime.length;j+=i)
IsNotPrime[j]=true;
if(!IsNotPrime[i])
ending[i%10]++;
}
IsNotPrime[1]=true;
//输出结果
Response.Write('<table border=1><tr align="center">\n');
for(i=1;i<IsNotPrime.length;i++)
{
LineFeed=(i % 10);
if(LineFeed==1)
Response.Write('<tr align="center">\n');
if(IsNotPrime[i])
Response.Write("<td>" );
else
Response.Write('<td bgcolor="#00FF00"">');
Response.Write(i + "</td>\n");
if(LineFeed==0)
Response.Write('</tr>\n');
}
Response.Write('</table>');
//输出统计结果
for(var i in ending_digits)
{
n=ending_digits[i];
Response.Write(n+' 结尾的素数个数:'+ending[n]+'<br>\n');
}
//防止重复统计结果用的标签
var tags=new Array(base*base);
for(var i=0;i<tags.length;i++)
{
n=i%base;
a=ending_digits[n];
b=ending_digits[((i -n)/base) % base];
if(a!=b && tags[a*base+b]!=true)
{
Response.Write(a+"结尾的素数个数 + "+b+"结尾的素数个数="+(ending[a]+ending[b])+"<br>\n");
Response.Write(a+"结尾的素数个数 - "+b+"结尾的素数个数="+(ending[a]-ending[b])+"<br>\n");
tags[a*base+b]=true;
tags[b*base+a]=true;
}
}
}
</script>
改一下代码,似乎我这个灵活性最强,而速度却没因此降低。我这里平均速度15ms
laisiwei 2006-07-05
  • 打赏
  • 举报
回复
以上3个版本运行时间比较

┏━━━━┯━━━━┯━━━━┯━━━━┓
┃ 计算量 │ 版本一 │ 版本二 │ 版本三 ┃
┠────┼────┼────┼────┨
┃  1000│ 835.938│  7.813│  0.000┃
┠────┼────┼────┼────┨
┃  10000│57164.06│ 296.875│ 93.750┃
┠────┼────┼────┼────┨
┃ 100000│  超时│5343.750│1039.063┃
┠────┼────┼────┼────┨
┃ 1000000│  超时│  超时│6421.875┃
┠────┼────┼────┼────┨
┃10000000│  超时│  超时│68320.31┃
┗━━━━┷━━━━┷━━━━┷━━━━┛
注:⒈90秒超时
  ⒉最后两组数据测试时去掉了表格输出
slawdan 2006-07-05
  • 打赏
  • 举报
回复
<%
server.ScriptTimeout=150
response.flush

function getPrimeTable(max)
dim resultarr
redim resultarr(max)
dim i,j,flag
if max<=100 then
resultarr(0)=false
resultarr(1)=false
resultarr(2)=true
resultarr(3)=true
resultarr(4)=false
for i=5 to max step 2
flag=true
j=3
do while j<=int(sqr(i))
if i mod j=0 then
flag=false
exit do
end if
j=j+1
loop
if flag then
resultarr(i)=true
else
resultarr(i)=false
end if
next
else
filt max,resultarr
end if
getPrimeTable=resultarr
end function

sub filt(max,byref flag)
dim i,sqrtmax,primetable,primetablecount,j,temp
primetable=getPrimeTable(int(sqr(max)))
primetablecount=ubound(primetable)
for i=2 to primetablecount
if primetable(i) then
j=i
do while j<=max
flag(j)=false
j=j+i
loop
flag(i)=true
end if
next
end sub

dim time1,time2,time3,time4
dim flag,max,i,rmax,temparr
dim out:out=false
rmax=request.querystring("max")
if isempty(rmax) or not isnumeric(rmax) then
max=1000
else
max=clng(rmax)
end if
redim flag(max)
for i=0 to max
flag(i)=true
next
response.write "<br>max="&max
response.flush


time1 = timer()
'filt(max,flag)
'temparr=getPrimeTable(max)
time2=timer()
response.write "<br>getPrimeTable Finish"
response.flush
time3 = timer()
filt max,flag
time4=timer()
'显示
if isarray(temparr) and out then
response.write "<table><tr>"
for i=1 to max
if i mod 10 = 0 then response.write "</tr><tr>"
if i>=ubound(temparr) then exit for
response.write "<td>"
if temparr(i)>0 then
response.write "<font color=red>"&i&"</font>"
else
response.write "<font>"&i&"</font>"
end if
if flag(i) then
response.write ",<font color=red>"&i&"</font>"
else
response.write ",<font>"&i&"</font>"
end if

response.write "</td>"
next
response.write "</tr></table>"
else
response.write "temparr非数组!"
if out then
response.write "<table><tr>"
for i=1 to max
if i mod 10 = 0 then response.write "</tr><tr>"
response.write "<td>"
if flag(i) then
response.write "<font color=red>"&i&"</font>"
else
response.write "<font>"&i&"</font>"
end if
response.write "</td>"
next
response.write "</tr></table>"
end if
end if



response.write "<br>运行时间1-2:" &(time2-time1)
response.write "<br>运行时间3-4:" &(time4-time3)
%>

说明:
采用Eratosthenes筛法,并对大于100的素数表循环使用筛法生成。
getPrimeTable在100以下时,可作传统试除法试验,或进行略为修改即可。
输出部分可用out变量控制。大于100000时最好不要输出表格,非常慢。
素数使用红色字体,没有用要求的绿色背景:)


测试平台
Celeron 2.4G , 242M RAM , 测试前系统可用物理内存 100M ,其他进程CPU占用率平均3%左右

测试结果(秒),不包括输出表格时间:

1000: 0
10000:0.015625~0.03125
100000:0.140625 (首次运算 0.125~0.15025,最高0.3125 )
1000000: 1.40625 (多次刷新在1.39~1.45)
10000000: 18 (仅做了一次测试)

计算10000000时,内存占用达到140M左右,因为我的机器内存小,所以可能存在对结果的影响
积木 2006-07-05
  • 打赏
  • 举报
回复
这里是什么饼子堂?
Featured 2006-07-05
  • 打赏
  • 举报
回复
关于素数求法的优化算法
http://community.csdn.net/Expert/topic/4550/4550362.xml?temp=4.839724E-02
sparks345 2006-07-05
  • 打赏
  • 举报
回复
<%
starttime = timer()

dim su() '存储素数的数组
redim Preserve su(1)
su(0) = 0
su(1) = 0

function sushu (x) 'boolean
for ii = 2 to sqr(x)
'response.write("{s"&sqr(x)&"}={"&x&"}-{"&ii&"}:"&(x mod ii)&"|")

if x mod ii = 0 then
sushu = false
exit function
else
'if not (has (x,su)) then
sushu = true
''end if
'end if
end if
next
end function

function ck (arr,r) '判断尾数为r的和
tsum = 0
for each num in arr
'response.write(num&"<br>")
if clng(right(num,1)) = clng(r) then
tsum = tsum + num
end if
next
ck = tsum
end function

%>
<table border='1'>
<%
for yy = 1 to 1000
if (yy-1) mod 10 = 0 then response.write("<tr>")
if sushu(yy) or yy =1 or yy = 3 then
response.write("<td bgcolor='lightgreen'>")
if su(Ubound(su)) <> yy then
length = Ubound(su) + 1
'redim su(length)
redim Preserve su(length)
su(Ubound(su)) = yy
end if
else
response.write("<td >")
end if
response.write(yy&"</td>")
if (yy) mod 10 = 0 then response.write("</tr>")
next
%>
</table>

<%
'test array su
response.write(Ubound(su)-1&"个|&<hr>")
'for ll = 0 to Ubound(su)
'response.write("su("&ll&"):"&su(ll)&" ")
'next
response.write("<hr>")
%>

<%
'尾数为1的和
sum1 = clng(ck(su,1))
sum3 = clng(ck(su,3))
sum7 = clng(ck(su,7))
response.write(sum1&"<hr>")
response.write(sum3&"<hr>")
response.write(sum7&"<hr>")



endtime = timer()
timespan = endtime-starttime
timespan = FormatNumber(timespan*1000,3)
'timespan = FormatNumber((endtime-starttime)*1000,3)
response.write(timespan)
%>


---------------
菜呀,31ms

判断(尾数为1的素数之和) 与 尾数为3、7的素数之“和”及“差”的逻辑关系,

判断(尾数为3的素数之和) 与 尾数为1、7的素数之“和”及“差”的逻辑关系,

判断(尾数为7的素数之和) 与 尾数为1、3的素数之“和”及“差”的逻辑关系。
这段一直不明白,comp是进行逻辑判断的吗,我照着几位大哥的搬过来却提示“类型不匹配: 'comp'”郁闷中


还有这个计算运行时间的和机子的配置有关吧,不知道影响大不大~?
huitouren519 2006-07-05
  • 打赏
  • 举报
回复
mark
wuyg719 2006-07-05
  • 打赏
  • 举报
回复
性能往往与内存占用成反比,应该把内存占用也统计出来。
laisiwei 2006-07-04
  • 打赏
  • 举报
回复
版本三
改进:把逐个判断改为筛选,速度进一步提高


<%
Option Explicit

Dim i,j
dim tmps
Dim s1, s3, s7
Dim n1, n3, n7
dim startime
dim ss(1000)

startime=timer()

n1 = 0
n3 = 0
n7 = 0

s1 = 0
s3 = 0
s7 = 0

tmps="        "

Response.Write "<table border=1><tr align=""center""><td>1</td>"

For i = 2 To 1000

If i Mod 10 = 1 Then Response.Write "<tr align=""center"">"
if ss(i)=0 then
Response.Write "<td bgcolor=""#00FF00"">" & i & "</td>"
j=i*2
do while j<=1000
ss(j)=1
j=j+i
loop
if right(cstr(i), 1) = "1" then
s1 = s1 + i
n1 = n1 + 1
elseif right(cstr(i), 1) = "3" then
s3 = s3 + i
n3 = n3 + 1
elseif right(cstr(i), 1) = "7" then
s7 = s7 + i
n7 = n7 + 1
end if
Else
Response.Write "<td>" & i & "</td>"
End If
If i Mod 10 = 0 Then Response.Write "</tr>"
Next
Response.Write "</table>"

Response.Write("尾数为1的素数总和: A = " & s1 &"<br>")
Response.Write("尾数为3的素数总和: B = " & s3 &"<br>")
Response.Write("尾数为7的素数总和: C = " & s7 &"<br>")
Response.Write("尾数为1的素数总个数: " & n1 &"<br>")
Response.Write("尾数为3的素数总个数: " & n3 &"<br>")
Response.Write("尾数为7的素数总个数: " & n7 &"<br><br>")

Response.Write("A" & comp(s1,s3+s7) & "(B + C)" & tmps)
Response.Write(s1 & comp(s1,s3+s7) & (s3 + s7) & "<br>")

Response.Write("A" & comp(s1,abs(s3-s7)) & "|B - C|" & tmps)
Response.Write(s1 & comp(s1,abs(s3-s7)) & abs(s3 - s7) & "<br>")

Response.Write("B" & comp(s3,s1+s7) & "(A + C)" & tmps)
Response.Write(s3 & comp(s3,s1+s7) & (s1 + s7) & "<br>")

Response.Write("B" & comp(s3,abs(s1-s7)) & "|A - C|" & tmps)
Response.Write(s3 & comp(s3,abs(s1-s7)) & abs(s1 - s7) & "<br>")

Response.Write("C" & comp(s7,s3+s1) & "(A + B)" & tmps)
Response.Write(s7 & comp(s7,s3+s1) & (s3 + s1) & "<br>")

Response.Write("C" & comp(s7,abs(s3-s1)) & "|A - B|" & tmps)
Response.Write(s7 & comp(s7,abs(s3-s1)) & abs(s3 - s1) & "<br>")

Private Function comp(x,y)
if x>y then
comp=" > "
elseif x=y then
comp=" = "
else
comp=" < "
end if
End Function

%>
页面执行时间:<%=FormatNumber((timer()-startime)*1000,3)%>
LicStar 2006-07-04
  • 打赏
  • 举报
回复
mark
laisiwei 2006-07-04
  • 打赏
  • 举报
回复
改一个错误,忘考虑2了

Private Function isSS(ByVal x)
Dim i
if x mod 2 =0 and x<>2 then
isSS = False
Exit Function
end if
For i = 3 To sqr(x)
If x mod i=0 Then
isSS = False
Exit Function
End If
Next
isSS = True
End Function
laisiwei 2006-07-04
  • 打赏
  • 举报
回复
版本二
改进:素数判断函数,消耗时间由原来的平均900ms提升到20ms


<%
Option Explicit

Dim i
dim tmps
Dim s1, s3, s7
Dim n1, n3, n7
dim startime

startime=timer()

n1 = 0
n3 = 0
n7 = 0

s1 = 0
s3 = 0
s7 = 0

tmps="        "

Response.Write "<table border=1><tr align=""center""><td>1</td>"

For i = 2 To 1000
If i Mod 10 = 1 Then Response.Write "<tr align=""center"">"
If isSS(i) = True Then
Response.Write "<td bgcolor=""#00FF00"">" & i & "</td>"
if right(cstr(i), 1) = "1" then
s1 = s1 + i
n1 = n1 + 1
elseif right(cstr(i), 1) = "3" then
s3 = s3 + i
n3 = n3 + 1
elseif right(cstr(i), 1) = "7" then
s7 = s7 + i
n7 = n7 + 1
end if
Else
Response.Write "<td>" & i & "</td>"
End If
If i Mod 10 = 0 Then Response.Write "</tr>"
Next
Response.Write "</table>"

Response.Write("尾数为1的素数总和: A = " & s1 &"<br>")
Response.Write("尾数为3的素数总和: B = " & s3 &"<br>")
Response.Write("尾数为7的素数总和: C = " & s7 &"<br>")
Response.Write("尾数为1的素数总个数: " & n1 &"<br>")
Response.Write("尾数为3的素数总个数: " & n3 &"<br>")
Response.Write("尾数为7的素数总个数: " & n7 &"<br><br>")

Response.Write("A" & comp(s1,s3+s7) & "(B + C)" & tmps)
Response.Write(s1 & comp(s1,s3+s7) & (s3 + s7) & "<br>")

Response.Write("A" & comp(s1,abs(s3-s7)) & "|B - C|" & tmps)
Response.Write(s1 & comp(s1,abs(s3-s7)) & abs(s3 - s7) & "<br>")

Response.Write("B" & comp(s3,s1+s7) & "(A + C)" & tmps)
Response.Write(s3 & comp(s3,s1+s7) & (s1 + s7) & "<br>")

Response.Write("B" & comp(s3,abs(s1-s7)) & "|A - C|" & tmps)
Response.Write(s3 & comp(s3,abs(s1-s7)) & abs(s1 - s7) & "<br>")

Response.Write("C" & comp(s7,s3+s1) & "(A + B)" & tmps)
Response.Write(s7 & comp(s7,s3+s1) & (s3 + s1) & "<br>")

Response.Write("C" & comp(s7,abs(s3-s1)) & "|A - B|" & tmps)
Response.Write(s7 & comp(s7,abs(s3-s1)) & abs(s3 - s1) & "<br>")

'B ( >=< ) A + C
'B ( >=< ) A - C ( C - A )
'C ( >=< ) A + B
'C ( >=< ) A - B ( B - A )


'--求素数的算法
Private Function isSS(ByVal x)
Dim i
if x mod 2 =0 then
isSS = False
Exit Function
end if
For i = 3 To sqr(x)
If x mod i=0 Then
isSS = False
Exit Function
End If
Next
isSS = True
End Function

Private Function comp(x,y)
if x>y then
comp=" > "
elseif x=y then
comp=" = "
else
comp=" < "
end if
End Function

%>
页面执行时间:<%=FormatNumber((timer()-startime)*1000,3)%>
laisiwei 2006-07-04
  • 打赏
  • 举报
回复
版本一:改进自bimyboy(★浩天雪狐★)
改进:1.1不是素数
2.增加是计算时间
3.写完了判断


<%
Option Explicit

Dim i
dim tmps
Dim s1, s3, s7
Dim n1, n3, n7
dim startime

startime=timer()

n1 = 0
n3 = 0
n7 = 0

s1 = 0
s3 = 0
s7 = 0

tmps="        "

Response.Write "<table border=1><tr align=""center""><td>1</td>"

For i = 2 To 1000
If i Mod 10 = 1 Then Response.Write "<tr align=""center"">"
If isSS(i) = True Then
Response.Write "<td bgcolor=""#00FF00"">" & i & "</td>"
if right(cstr(i), 1) = "1" then
s1 = s1 + i
n1 = n1 + 1
elseif right(cstr(i), 1) = "3" then
s3 = s3 + i
n3 = n3 + 1
elseif right(cstr(i), 1) = "7" then
s7 = s7 + i
n7 = n7 + 1
end if
Else
Response.Write "<td>" & i & "</td>"
End If
If i Mod 10 = 0 Then Response.Write "</tr>"
Next
Response.Write "</table>"

Response.Write("尾数为1的素数总和: A = " & s1 &"<br>")
Response.Write("尾数为3的素数总和: B = " & s3 &"<br>")
Response.Write("尾数为7的素数总和: C = " & s7 &"<br>")
Response.Write("尾数为1的素数总个数: " & n1 &"<br>")
Response.Write("尾数为3的素数总个数: " & n3 &"<br>")
Response.Write("尾数为7的素数总个数: " & n7 &"<br><br>")

Response.Write("A" & comp(s1,s3+s7) & "(B + C)" & tmps)
Response.Write(s1 & comp(s1,s3+s7) & (s3 + s7) & "<br>")

Response.Write("A" & comp(s1,abs(s3-s7)) & "|B - C|" & tmps)
Response.Write(s1 & comp(s1,abs(s3-s7)) & abs(s3 - s7) & "<br>")

Response.Write("B" & comp(s3,s1+s7) & "(A + C)" & tmps)
Response.Write(s3 & comp(s3,s1+s7) & (s1 + s7) & "<br>")

Response.Write("B" & comp(s3,abs(s1-s7)) & "|A - C|" & tmps)
Response.Write(s3 & comp(s3,abs(s1-s7)) & abs(s1 - s7) & "<br>")

Response.Write("C" & comp(s7,s3+s1) & "(A + B)" & tmps)
Response.Write(s7 & comp(s7,s3+s1) & (s3 + s1) & "<br>")

Response.Write("C" & comp(s7,abs(s3-s1)) & "|A - B|" & tmps)
Response.Write(s7 & comp(s7,abs(s3-s1)) & abs(s3 - s1) & "<br>")

'B ( >=< ) A + C
'B ( >=< ) A - C ( C - A )
'C ( >=< ) A + B
'C ( >=< ) A - B ( B - A )


'--求素数的算法
Private Function isSS(ByVal x)
Dim i

For i = x - 1 To 2 Step - 1
If x \ i = x / i Then
isSS = False
Exit Function
End If
Next
isSS = True
End Function

Private Function comp(x,y)
if x>y then
comp=" > "
elseif x=y then
comp=" = "
else
comp=" < "
end if
End Function

%>
页面执行时间:<%=FormatNumber((timer()-startime)*1000,3)%>
  • 打赏
  • 举报
回复
不懂
兔子-顾问 2006-07-04
  • 打赏
  • 举报
回复
mark
加载更多回复(35)

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧