java 找出100以内的素数
2009-11-12 20:59:54 来源:WEB开发网核心提示:view plaincopy to clipboardPRint?public class FindPrime { /** * @param args */public static void main(String[] args) { // TODO Auto-generated method stub int nu
view plaincopy to clipboardPRint?
public class FindPrime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 100;
String s = "100以内的素数:";
for (int i = 1; i <= num; i++) {
int count = 0;
for (int j = 1; j <= (int) Math.sqrt(i); j++) {
if (i % j == 0)
count++;
}
if (count > 1)
s += "";
else
s += i + " ";
}
System.out.println(s);
}
//result
//100以内的素数:1 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
}
public class FindPrime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 100;
String s = "100以内的素数:";
for (int i = 1; i <= num; i++) {
int count = 0;
for (int j = 1; j <= (int) Math.sqrt(i); j++) {
if (i % j == 0)
count++;
}
if (count > 1)
s += "";
else
s += i + " ";
}
System.out.println(s);
}
//result
//100以内的素数:1 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
}
赞助商链接