质因数递归分解

#include "stdio.h"
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;

int Prime(int number){
    for(int i = 2; i <= sqrt(number); i++ )
        if(number%i == 0 )
            return 0;
    return 1;
}

void PrimeFactor(int number){
    for(int i = 2; i <= number; i++)
        if(number%i == 0 && Prime(i))
        {
            cout << i;
            if(!Prime(number))
                cout << '*';
            number = number/i;
            PrimeFactor(number);
            return ; //一定要加,否则第一次分解得到的数会因为for循环再次被分解
        }
}
int main(){
    int number;
    cout << "please key in the number:" << endl;
    cin >> number;
    cout << "the result is :number = ";
    PrimeFactor(number);
    cout << endl;
    return 1;

}

 

posted on 2017-12-09 20:57  帝孤  阅读(687)  评论(0编辑  收藏  举报

导航