【问题标题】:How to optimize this c program to find the prime factorisation of a number如何优化此 c 程序以找到数字的素因数分解
【发布时间】:2016-09-03 16:17:26
【问题描述】:

我已经编写了一个数字的素数分解代码,但我不知道如何优化它以获得更大的数字的更好结果有人知道吗?

#include<stdio.h>
int prime(int p, int q, int r);
int main(){
int n,c=0;
scanf("%d",&n);
for(int j=2;j<=n;j++){
    int t=0;
    for(int i=1;i<=j;i++){
        if(j%i==0){
                t++;
    }
 }
 if(t==2){ //check whether J is prime or not
    //  printf(" %d ",j);
        c=prime(n,j,0); //check whether j has any factors with N 
        if(c!=0){
            printf(" %d ",j);
            printf(" %d \n",c);
    }
 }
 }
 return 0;
}
int prime(int p,int q,int r){
    if(p%q==0){
        r++;
        prime(p/q,q,r);
    }
    else{
    return r;
    }
}

【问题讨论】:

  • 我允许建议您的第一件事是通过注释和“美化”它来稍微解释一下您的代码。其次,您能解释一下“获得更大数字的更好结果”是什么意思吗?
  • 更好的结果意味着减少执行时间,关于代码我所做的是我取每个小于 N 的数字,我们必须找到分解,并检查它是否是素数,如果它是素数,然后检查它是否有给定数字的因子,这个循环一直持续到最后一个数字 N
  • 我可以建议您阅读有关整个主题的内容:Polland-Strassen 分解:en.wikipedia.org/wiki/Pollard%27s_rho_algorithm 和 math.stackexchange 中的一个问题:math.stackexchange.com/questions/631559/…
  • 由于您的代码分为两个函数,我建议您从优化主要函数开始。您可以阅读:en.wikipedia.org/wiki/Primality_test
  • 谢谢@francesco-b 实际上我的主要查询是如何优化代码,因为这是我自己的代码,所以优化这段代码可能会让我更清楚,所以我发布了这个问题跨度>

标签: c++ primes prime-factoring


【解决方案1】:

你的代码相当于

#include <stdio.h>

int main(){
    int n;
    scanf("%d", &n);
    for(int j=2; j <= n; j++) {     // for each j in 2..n,

        int t=0;
        for(int i=1; i<=j; i++) {   // count j's divisors
            if(j%i==0) {            //   among 1..j
                t++;
            }
        }

        if( t==2 ){                 // if j is prime:   NB!
            int c=0, nn=n;
            while( nn%j==0 ) {      //   count multiplicity of j 
                c++;                //   as divisor of n, and
                nn /= j;
            }
            if( c!=0 ){             //   report a prime divisor and 
                printf(" %d  %d \n", j, c);   // its multiplicity
            }
        }
     }
     return 0;
}

但实际上只需要这样:

int main(){
    int n, c=0;
    scanf("%d", &n);
    for(int j=2; j <= n; j++) {     // for each j in 2..n,

            int c=0;
            while( n%j==0 ) {       // if j divides n
                c++;                //   count its multiplicity
                n /= j;             //   while dividing it out of n
            }                       //   NB!  changing the `n`  NB!
                                    //   which guarantees j is prime
            if( c!=0 ){             //       when c != 0
                printf(" %d  %d \n", j, c);
            }
    }
    return 0;
}

所以最后的显着优化是

int main(){
    int n, c=0;
    scanf("%d", &n);
    for(int j=2; j*j <= n; j++) {   // here

            int c=0;
            while( n%j==0 ) {
                c++;
                n /= j;             
            }
            if(c!=0){
                printf(" %d  %d \n", j, c);
            }
    }
    if(n>1) {                       // and here
        printf(" %d  %d \n", n, 1);
    }
    return 0;
}

接下来,您可以尝试在主循环中将j 增加2,以仅覆盖奇数,因为在2之上没有偶数可以是素数。

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    //function to check for prime numbers (return '1' if prime ,else return '0').
    int checkprime(int pr)
    {
        if ((pr==2)||(pr==3))
        {
            return (1);
        }    
        else
        {
            for (int  i = 3; i <pr; i=i+2)
            {
                if (pr%i==0)
                {
                    return (0);
                }
                
            }
            return 1;
            
        }
        
    }
    //function to print the prime factors.. 
    int primefactors(int num)
    {
        int temp;
        temp=num;
        while(temp!=0 && temp%2==0)
        {
            if (temp%2==0)
            {
                printf("2 ");
                temp/=2;
            }
            
        }
        for (int k =3; k <=temp&& temp!=0 ; k=k+2)
        {
            if (checkprime(k)==1)
            {
                while ((temp!=0) && (temp%k==0))
                {
                    printf("%d ",k);
                    temp/=k;
                }
        
            }
            
        }
    }
    int main()
    {
        int a;
        a=65;
        if (a>1)
        {
            primefactors(a);
        }
        else
        {
            printf("variable 'a' should be greater 1!");
        }
        
        
        
    
        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2014-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多