素因数分解

#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;

int a[100001];//0未知 1素数 2非素数
int main(){
    memset(a, 0, sizeof(a));

    for (int i = 2; i < 100001;i++)
    {
        if (a[i] == 1||a[i]==2)
            continue;
        else{
            int max = sqrt(i) + 1;
            a[i]=1;
            for (int j = 2; j < max; j++)
                if (i%j == 0)
                {
                    a[i] = 2; break;
                }
        }
        if (a[i] == 1)
        {
            for (int k = i; k*i <= 100000; k++)
                a[k*i] = 2;
        }
    }
    int n;
    int b[10],j=0;
    while (cin >> n)
    {
        int i;
        int res = 0;
        int tmp = n;
        for (i = 2; i < n; i++)
        {
            if (a[i] == 1 && n%i == 0)
            {
                b[j++] = i;
                while (tmp%i == 0)
                {
                    res++;
                    tmp /= i;
                }
            }
            if (tmp == 1)break;
        }
        //若不为1则一定是个大于100000的素因数
        if (tmp != 1)
            res++;
        cout << res << endl;

    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KingsCC/article/details/81811838