c语言分解因式

#include <stdio.h>

void fun();
int check(int n);
 
int main(int argc, char *argv[])
{
    fun(1155); 
    return 0;
}

void fun(int n){
    if(check(n)){
        printf("%d ",n);
        return;    
    }else{
        int i;
        for(i=2;i<n;i++){
            if(n%i == 0){
                printf("%d ",i);        //由2开始找到的第一个因数一定是素数 
                if(check(n/i)){
                     printf("%d ",n/i);
                     return;
                }else{
                     fun(n/i);
                      return;                //只需找到n的第一个因数即可        
                 }        
            }        
        }    
    }
} 

//是素数返回1 
int check(int n){
    int i;
    if(n<3) return 1;
    
    for(i=2;i<n;i++){
        if(n%i == 0) return 0;    
    }
    
    return 1;    
} 

输出:

3 5 7 11

posted @ 2016-07-27 16:32  阿豪boy  阅读(448)  评论(0编辑  收藏  举报