如何优化这个c程序来找到一个数字的质因数分解

How to optimize this c program to find the prime factorisation of a number

本文关键字:一个 分解 数字 质因数 程序 何优化 优化      更新时间:2023-10-16

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

#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;
    }
}

你的代码相当于

#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之上的偶数不可能是素数。

#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!");
    }
    
    
    
    
}