簡體   English   中英

如何優化這個 c 程序來找到一個數的質因數分解

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

我已經編寫了一個數字的質因數分解代碼,但我不知道如何優化它以獲得更大數字的更好結果,任何人都可以知道嗎?

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

    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM