查找非素數

題目描述

查找出所有大於1小於等於整數mm < 100)的非素數。 例如,若輸入:17,則應輸出:4 6 8 9 10 12 14 15 16

輸入

輸入一個大於1小於100的整數,如 17

輸出

輸出所有查找到的非素數。

樣例輸入

17

樣例輸出

4 6 8 9 10 12 14 15 16

提示

1.編寫一個main函數實現該功能算法。
2.建議用數組來保存找到的非素數。

地區

南京研究所

public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int m=sc.nextInt();
		List<Integer> list=new ArrayList<Integer>();
		for(int j=2;j<=m;j++){
			int count=0;
			for(int i=2;i<=(int)Math.sqrt(j);i++ ){
				if(j%i==0){
					count++;
				}
			}
			if(count>0){
				list.add(j);
			}
		}
		Object[] iArr=list.toArray();
		for(int i=0;i<iArr.length;i++ ){
			System.out.print(iArr[i]+" ");
		}
	}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章