儿童海绵垫多少钱:编写程序求出1000以内的所有素数

来源:百度文库 编辑:高校问答 时间:2024/04/29 14:00:58
谢谢

不知道你要的是什么语言.我以VB语言为例.

for i=3 to 1000
for j=2 to i-1
if i mod j =0 then
a=1
exit for
end if
next
if a<>1 then
print i
end if
next

我是从3开始打印的. 要么还得判断.

Dim a As Integer, b As Integer, c As Integer
a = 1
Do
a = a + 1
For b = 2 To a - 1
If a Mod b = 0 Then Exit For
Next b
If b = a Then
Print a;
c = c + 1
If c Mod 10 = 0 Then Print
End If
Loop Until a = 1000

#include <stdio.h>
#include "math.h"
#define N 1001
main()
{
int i,j,line,a[N];
for(i=2;i<N;i++) a[i]=i;
for(i=2;i<sqrt(N);i++)
for(j=i+1;j<N;j++)
{
if(a[i]!=0&&a[j]!=0)
if(a[j]%a[i]==0)
a[j]=0;}
printf("\n");
for(i=2,line=0;i<N;i++)
{
if(a[i]!=0)
{printf("%5d",a[i]);
line++;}
if(line==10)
{printf("\n");
line=0;}
}
}