作业帮 > 综合 > 作业

数素数 pascal数素数Time Limit:1000MS Memory Limit:65536KTotal Subm

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/26 03:47:11
数素数 pascal
数素数
Time Limit:1000MS Memory Limit:65536K
Total Submit:654 Accepted:172
Description
输出[m,n]之间的所有素数的个数k
Input
只有一行共有二个正整数:m n
( 1 < m < n < 1000000 )
Output
只有一行且只有一个整数:k
Sample Input
5 10
Sample Output
2
Source
基础题
var n,m,ans,i,j:longint; f:boolean;
begin
readln(m,n);
ans:=0;
for i:=m to n do
begin
f:=true;
for j:=2 to trunc(sqrt(i)) do
begin
if i mod j=0 then
begin
f:=false; break;
end;
end;
if f then inc(ans);
end;
writeln(ans);
end.
再问: 超时了
再答: 思路是用筛选法求素数,然后统计一边吧,可是我不会筛法求素数... 貌似还有o(n)求素数表的方法,LZ你可以上网找找,这个我真的不会了...