求质因数,因数,刷质数

求一个数的质因数和因数都是可以采用sqrt的算法。

求质因数:

vector<int> g;
void cal(int x)
{
    int xx=x;
    for(int i=2;i*i<=x;i++)
    {
        if(xx==1)
            break;
        while(xx%i==0)
        {
            xx/=i;
            g.push_back(i);
        }
    }
    if(xx!=1)
        g.push_back(xx);
}

求因数:

vector<int> g;
void cal(int x)
{
    for(int i=1;i*i<=x;i++)
    {
        if(x%i==0)
        {
            g.push_back(i);
            if(x/i>i)
                g.push_back(x/i);
        }
    }
}

刷质数:

int v[111111];
void sol()
{
    for(int i=2;i<=10000;i++)
      if(!v[i])
      for(int j=i*i;j<=10000;j+=i)
      {
        v[j]=1;
     }
}

其余方法待补

原文地址:https://www.cnblogs.com/acliang/p/4995722.html