PASCAL求出100以内的全部素数,并按每行五个数显示
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/10/07 06:49:55
PASCAL求出100以内的全部素数,并按每行五个数显示
输入n
输出为1~n的素数,每五个一行
var
i,j,count,n:longint;//i,j为循环变量;count为计数器,每满五则换行并清零;n为输入的数;
f:boolean;//判断是否为素数的变量;
begin
readln(n);//读入
for i:=2 to n do//外循环,枚举从2到n的数,由于1比较特殊,所以从2枚举.
begin
f:=true;//判断量先设为true;
for j:=2 to trunc(sqrt(i)) do//枚举从2 到 根号i取整的数,这样可以有效节约时间复杂度;
if i mod j =0 then f:=false;如果在2~n之间有能整除i的数则将判断量设为false;
if f then//如果i为素数
begin
write(i,' ');//输出i;
inc(count);//给计数器加一;
if count=5 then begin writeln;count:=0;end;//如果计数器满五则清零并换行;
end;//end if;
end;//end for i;
end.//end program;
下面是输入为100的输出结果:
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
如果要求每行末尾不能有空格,那么把if f then 后面改为下面:
if f then
begin
inc(count);
if count=5 then begin count:=0;writeln(i);end
else write(i,' ');
end;
最后,还请给个最佳吧~
输出为1~n的素数,每五个一行
var
i,j,count,n:longint;//i,j为循环变量;count为计数器,每满五则换行并清零;n为输入的数;
f:boolean;//判断是否为素数的变量;
begin
readln(n);//读入
for i:=2 to n do//外循环,枚举从2到n的数,由于1比较特殊,所以从2枚举.
begin
f:=true;//判断量先设为true;
for j:=2 to trunc(sqrt(i)) do//枚举从2 到 根号i取整的数,这样可以有效节约时间复杂度;
if i mod j =0 then f:=false;如果在2~n之间有能整除i的数则将判断量设为false;
if f then//如果i为素数
begin
write(i,' ');//输出i;
inc(count);//给计数器加一;
if count=5 then begin writeln;count:=0;end;//如果计数器满五则清零并换行;
end;//end if;
end;//end for i;
end.//end program;
下面是输入为100的输出结果:
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
如果要求每行末尾不能有空格,那么把if f then 后面改为下面:
if f then
begin
inc(count);
if count=5 then begin count:=0;writeln(i);end
else write(i,' ');
end;
最后,还请给个最佳吧~
PASCAL求出100以内的全部素数,并按每行五个数显示
求100以内的素数pascal语言
求100以内的素数(1)用筛选法求出这些素数.(2)在屏幕上显示出求素数的动态过程(在屏幕上先显示出100以内的所有数,
设计一程序,求出5到100之间的所有素数,要求每行输出五个素数.判断一个整数是否是为素数用一个函数来实
C语言:输出500-------1000以内的全部素数,要求每行输出10个素数.
用Pascal语言求出1——1000以内的孪生素数!
pascal求100000以内的纯粹素数.
随机产生300个1000以内的正整数,求出素数个数找出其中素数并按升序排列
求出所有三位数的素数,按每行5个数输出.
1、在屏幕上按每行10个的格式输出100—200间的全部素数,并输出它们的平均值.C语言的
编写一个求水仙花的函数和判断整数n是否为素数的函数,求出3位正整数的全部水仙花数并判断求出的水仙花数是否为素数.所谓水仙
求出所有的既是回文数又是素数的三位数.pascal求大神求解哪里错了?