用Java语言编写程序:输出100以内的素数

                                                              用Java语言编写程序:输出100以内的素数

素数定义:在大于1的整数中,只能被1和这个数本身整除的数。

判别方法:n是大于1的整数,若n不能被2~根号n中的任意一个整数整除,则n为素数。

本人代码如下:(算法改进了,上一次用了数组存储。方法不一,仅供参考)

public class Prime{
	public static void main(String args[]){
		int a[]=new int[100];
		int j;
		
		for(int i=2;i<100;i++){
			for(j=2;j<=Math.sqrt(i);j++)
				if(i%j==0){
					j=0;
					break;
				}
			if(j!=0)
				System.out.print(i+" ");
		}
	}
}


猜你喜欢

转载自blog.csdn.net/qq_38861828/article/details/77911327