- 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<num/2+1;i++){
- if(num%i==0){
- temp++;
- break;
- }
- }
- if(temp!=0){
- return false;
- }
- return true;
- }
- /**
- * 得到一个数所有的因数
- * @param num
- * @return
- */
- public static List<Integer> zhengChu(int num){
- List<Integer> integers=new ArrayList<Integer>();
- for(int i=2;i<num/2;i++){
- if(num%i==0){
- integers.add(i);
- }
- }
- return integers;
- }
- /**
- *
- * @param args
- */
- public static void main(String[] args) {
- int num=120;
- Set<Integer> integers=new HashSet<Integer>();
- Set<Integer> 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<Integer> test(int num,Set<Integer> data){
- if(data==null){
- data=new HashSet<Integer>();
- }
- if(T1.isZhishu(num)){
- System.out.println(num);
- data.add(num);
- }
- else{
- List<Integer> temp=T1.zhengChu(num);
- for(Integer integer:temp){
- test(integer, data);
- }
- }
- return data;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/070320148897.html
来源: http://www.codesnippet.cn/detail/070320148897.html