找出所有质因子

#include <iostream>
#include <math.h>
using namespace std;
bool is_prime(unsigned long long n) { //quick
    unsigned long long stop = n / 6 + 1, Tstop = sqrt(n) + 5;
    if (n == 2 || n == 3 || n == 5 || n == 7 || n == 11) {
        return 1;
    }
    if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0 || n == 1) {
        return 0;
    }
    for (unsigned long long i = 1; i <= stop; i++) {
        if (i * 6 >= Tstop) { break; }
        if ((n % (i * 6 + 1) == 0) || (n % (i * 6 + 5) == 0)) {
            return 0;
        }
    }
    return 1;
}

void printZhi(int n) {

    if (is_prime(n)) {
        cout << n << " ";
        return;
    }
    int i = 2;
    for (;i <= sqrt(n);++i) {
        if (n % i == 0) {
            printZhi(i);
            printZhi(n / i);
            break;
        }
    }

}

int main()

{
    int a;
    while (cin >> a) {
        printZhi(a);
        cout << endl;
    }
    return 0;
}

posted @ 2022-03-03 14:06  icefield817  阅读(92)  评论(0编辑  收藏  举报