java求任何整数的因子
1.[代码][Java]代码
public class T1 {
/**
* 分析这个数是不是质数
* @param num
*/
public static boolean isZhishu(int num){
switch (num) {
case 1:
case 2:
case 3:
return true;
}
int temp=0;
for(int i=2;i
if(num%i==0){
temp++;
break;
}
}
if(temp!=0){
return false;
}
return true;
}
/**
* 得到一个数所有的因数
* @param num
* @return
*/
public static List zhengChu(int num){
List integers=new ArrayList();
for(int i=2;i
if(num%i==0){
integers.add(i);
}
}
return integers;
}
/**
*
* @param args
*/
public static void main(String[] args) {
int num=120;
Set integers=new HashSet();
Set integers2= T1.test(num, integers);
System.out.println(integers2);
//打印结果
for (Integer integer : integers2) {
System.out.println(integer);
}
}
/**
* 正式求解
* @param num
* @param data
* @return
*/
public static Set test(int num,Set data){
if(data==null){
data=new HashSet();
}
if(T1.isZhishu(num)){
System.out.println(num);
data.add(num);
}
else{
List temp=T1.zhengChu(num);
for(Integer integer:temp){
test(integer, data);
}
}
return data;
}
}