算法学习笔记1.3.3 质因数分解

任务

给一个正整数N,将N分解质因数。

说明

N的质因数要么是N本身(N是素数),要么一定小于等于sqrt(N)。与i那次可以用小于等于sqrt(N)的数对N进行试除,一直到不能除为止。这时候剩下的数如果不是1,那就是N最大的质因数。

接口

void factor(int n, int a[maxn], int b[maxn], int &tot);

复杂度:O(sqrt(N))
输入:n 待分解的整数
输出:

  • &tot 不同质因数的个数
  • a a[i]表示第i个质因数的值
  • b b[i]表示第i个质因数的指数

代码

#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 1010;

void factor(int n, int a[maxn], int b[maxn], int &tot) {
    int temp, i, now;
    temp = (int) ( (double) sqrt(n) + 1 );
    tot = 0;
    now = n;
    for (i = 2; i <= temp; i ++) if (now % i == 0) {
        a[++tot] = i;
        b[tot] = 0;
        while (now % i == 0) {
            ++ b[tot];
            now /= i;
        }
    }
    if (now != i) {
        a[++tot] = now;
        b[tot] = 1;
    }
}

int main() {
    int n = 120480, a[maxn], b[maxn], tot;
    factor(n, a, b, tot);
    cout << n << "=";
    for (int i = 1; i <= tot; i ++) {
        if (i > 1) cout << "+";
        cout << a[i] << "^" << b[i];
    }
}

使用范例

POJ1142

原文地址:https://www.cnblogs.com/zifeiy/p/9526365.html