int n=1;
int m=2;
int i=0;
int result=0;
这几个定义到 public static void main(String[] args)
{
里来
最简便算法:
public
class
main
{
public
static
void
main(string[]
args)
{
int
t
=
0;
for
(int
i
=
2;
i
<=
10000;
i++)
{
int
a
=
1;
for
(int
n
=
2;
n
<=
math.sqrt(i);
n++)
{
a
=
a
*
(
i
%
n);
if
(
i
%
n
>=
2)
{
a
=
a
/
2;
}
}
if
(a
!=
0)
{
t++;
}
}
system.out.println(t);
}
}
你的方法修改为:
public
class
main
{
public
static
void
main(string[]
args)
{
int
i=0;
for
(int
n
=
2;n
<=9999;
n++)
{
for(int
m
=
2;m
<=
n;
m++)
{
if(n
%
m==0&&n!=2&&m!=n)
{
break;
}
else
if(m==n-1||n==2)
{
i++;
}
}
}
system.out.println
("10000以内的质数的个数:"+i);
}
}