Positive integer factorization

Input a positive integer n, decompose the prime factors of the positive integer n, and output.
Analysis:
To decompose the prime factors of n, you should first find the smallest prime number k, which is a step-by-step decomposition process. because we know that any n=2k13k25k37k4... can be decomposed into the following forms.
code show as below:

  1. recursive implementation
  2. Loop implementation
import java.util.ArrayList;
import java.util.Scanner;

/**
 * 质因数分解
 * @author ShaoCheng
 *
 */
public class PrimeFactor {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        ArrayList<Integer> res = new ArrayList<>();
        ArrayList<Integer> res1 = new ArrayList<>();
        primeFactor(res, n);
        primeFactorLoop(res1, n);
        System.out.println(res);
        System.out.println(res1);
        scanner.close();
    }

    /**
     * 递归实现质因子分解
     * @param res 存放分解出来的质因子
     * @param n 输入正整数
     */
    public static void primeFactor(ArrayList<Integer> res, int n){
        if(n <= 1)
            return;
        for(int i = 2; i <= n; i++){
            if(n % i == 0){
                n /= i;
                res.add(i);
                primeFactor(res, n);
                break; //成功分解后跳出
            }
        }
    }

    /**
     * 循环实现
     * @param res 存放分解出来的质因子
     * @param n 输入正整数
     */
    public static void primeFactorLoop(ArrayList<Integer> res, int n){
        if(n <= 1)
            return;
        for(int i = 2; i <= n; i++){ //从质数因子2开始找
            if(n % i == 0){ //找到质数因子
                n /= i;
                res.add(i);
            }
            //看当前的n能否继续被已经找到的质因子整除殆尽
            for(int j = 0; j < res.size(); j++){
                int factor = res.get(j);
                if(n % factor != 0)
                    break;
                n /= factor;
                res.add(factor);
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325714008&siteId=291194637