java求整数的因子_java 求任何整数的因子

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;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值