编写一个java程序,用穷举法找出2~50之间的素数,并打印出来.

2025-03-23 04:43:24
推荐回答(2个)
回答1:

public static void main(String[] args) {
int i, k;
boolean yes;
for (k = 2; k <= 50; k++) {
yes = true;
i = 2;
while (i <= k - 1 && yes) {
if (k % i == 0)
yes = false;
i++;
}
if (yes)
System.out.print(k + " ");
}
}

回答2:

public class A {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
A a = new A();

for(int c=0 ;c<=50;c++)
{
if(c/2==1||c%2!=0&&c!=1&&c!=9&&c!=15&&c!=21&&c!=25&&c!=27&&c!=33&&c!=35&&c!=39&&c!=45&&c!=49)
{
System.out.println(c);
}
}
}

}