- package airthmatic;
- public class demo8 {
- /**
- * 素数是指因数只有1和本身的数字
- * @param arg
- */
- public static void main(String arg[])
- {
- for(int i=1;i<=100;i++)
- {
- if(find(i))
- System.out.print(i+" ");
- }
- }
- /**
- * 1-n个自然数中的素数
- * @param n
- * @return
- */
- public static boolean find(int n)
- {
- for(int i=2;i<=Math.sqrt(n);i++)
- {
- if(n%i==0)
- return false;
- }
- return true;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1704201512313.html
来源: http://www.codesnippet.cn/detail/1704201512313.html