【问题标题】:count distinct prime factors计算不同的主要因素
【发布时间】:2013-07-14 08:53:31
【问题描述】:

我必须计算 2 到 100000 的不同素数的数量,有没有比我正在做的更快的方法? 即 2 有 1 个不同的素因数 2 10 有 2 个不同的素数 (2,5) 12 有 2 个不同的素数 (2,3) 我的代码:-

#include<stdio.h>
#include<math.h>
typedef unsigned long long ull;
char prime[100000]={0};
int P[10000],fact[100000],k;

void sieve()
{
    int i,j;
    P[k++]=2;
    for(i=3;i*i<1000;i+=2)    
    {
            if(!prime[i])
            {
                    P[k++]=i;
                    for(j=i*i;j<100000;j+=i+i)    
                            prime[j] = 1;
            }
    }
    for(i=1001;i<100000;i+=2)    
            if(!prime[i])
                    P[k++]=i;
}

int calc_fact() {
  int root,i,count,j;
  fact[1]=fact[2]=fact[3]=1;
  for(i=4;i<=100000;i++) {
     count=0;
     root=i/2+1;
     for(j=0;P[j]<=root;j++) {
        if(i%P[j]==0)count++;
     }
     if(count==0) fact[i]=1;
     else fact[i]=count;
  }
 return 0;
}
int main(){
 int i;
 sieve();
 calc_fact(); 
 for(i=1;i<10000;i++) printf("%d  ,",fact[i]);
 return 0;
}

【问题讨论】:

    标签: math prime-factoring number-theory


    【解决方案1】:

    您可以轻松地调整 Erasthotenes 的筛子来计算一个数字所具有的素因子的数量。

    这是一个 C 实现,以及一些测试:

    #include <stdio.h>
    
    #define N 100000
    
    static int factorCount[N+1];
    
    int main(void)
    {
        int i, j;
    
        for (i = 0; i <= N; i++) {
            factorCount[i] = 0;
        }
    
        for (i = 2; i <= N; i++) {
            if (factorCount[i] == 0) { // Number is prime
                for (j = i; j <= N; j += i) {
                    factorCount[j]++;
                }
            }
        }
    
        printf("2 has %i distinct prime factors\n", factorCount[2]);
        printf("10 has %i distinct prime factors\n", factorCount[10]);
        printf("11111 has %i distinct prime factors\n", factorCount[11111]);
        printf("12345 has %i distinct prime factors\n", factorCount[12345]);
        printf("30030 has %i distinct prime factors\n", factorCount[30030]);
        printf("45678 has %i distinct prime factors\n", factorCount[45678]);
    
        return 0;
    }
    

    【讨论】:

    • 顺便说一句,@v-delecroix 的答案中的代码无法正常工作(例如,它报告 12345 有 4 个不同的质因数,而它有 3 个)。 (PS:我在这里发布这个是因为 SO 信誉系统)
    【解决方案2】:

    做一个埃拉托色尼筛子绝对可以做得更好。

    在 Python 中

    N = 100000
    M = int(N**.5)                         # M is the floor of sqrt(N)
    nb_of_fact = [0]*N
    for i in xrange(2,M):
        if nb_of_fact[i] == 0:             # test wether i is prime
            for j in xrange(i,N,i):        # loop through the multiples of i
                nb_of_fact[j] += 1
    for i in xrange(M,N):
        if nb_of_fact[i] == 0:
            nb_of_fact[i] = 1
    

    在循环结束时,nb_of_fact[i] 是 i 的素数因子的数量(特别是当且仅当 i 是素数时它是 1)。

    旧的错误版本

    N = 100000
    nb_of_fact = [1]*N
    for i in xrange(2,N):
        if nb_of_fact[i] == 1:             # test wether i is prime
            for j in xrange(2*i,N,i):      # loop through the multiples of i
                nb_of_fact[j] += 1
    

    【讨论】:

    • 很好,但是操作要求 distinct 素因数的数量。例如,4 只有一个不同的质因数。
    • 由于所有内容都初始化为 1,这不是一个好的答案...在@user2580621 之后,我查看了我的代码...
    • +1,看起来很不错。可以通过将第一个循环更改为在 int(N0.5) 处停止,然后添加一个从 int(N0.5) + 1 到 N 的最终循环来替换任何剩余的零,从而提高效率和那些。
    • @GregS 非常感谢你让它成为一个工作代码和一个漂亮的代码!
    • N//2 大于所需。 N**0.5(与 N 的平方根相同)就足够了。
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多