1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
#include <sstream>
#include <gmp.h>
using namespace std;
mpz_t* p_x;
mpz_t* p_one;
mpz_t x;
mpz_t y;
mpz_t* p_y;
mpz_t one;
mpz_t fact;
mpz_t* p_fact;
stringstream ss;
// factorial small numbers
int factorial(int x){
if( x <= 1){
return 1;
}
return factorial(x-1) * x;
}
void initInts(){
mpz_init(x);
mpz_init(y);
mpz_init(one);
mpz_init(fact);
mpz_set_str(one,"1",10);
p_one = &one;
mpz_set_str(x,"0",10);
p_x = &x;
mpz_set_str(y,"0",10);
p_y = &y;
mpz_set_str(fact,"0",10);
p_fact = &fact;
}
// factorial big numbers
mpz_t* factorialBig(mpz_t* n,int strNum){
gmp_printf ("% Zd ", *n);
if(mpz_cmp(*n,one) == 0){
cout << "retuning :" << endl;
cout << "p one = ";
gmp_printf ("% Zd ", *p_one);
cout << endl;
return p_one;
}
char* strTemp;
ss << strNum;
ss >> strTemp;
mpz_t temp;
mpz_init(temp);
mpz_set_str(temp,strTemp,10);
mpz_t tempTwo;
mpz_init(tempTwo);
mpz_set_str(tempTwo,strTemp,10);
mpz_sub(tempTwo,temp,one);
mpz_swap(*n,tempTwo);
ss.str("");
ss.clear();
factorialBig(n,strNum);
mpz_mul(fact,temp,tempTwo);
cout << "*n ";
gmp_printf ("% Zd ", fact);
cout << endl;
return p_fact;
}
void callFactBig(char* str,int num){
mpz_set_str(x,str,10);
factorialBig(p_x,num);
//gmp_printf ("% Zd ", *factorialBig(x));
}
int main()
{
initInts();
callFactBig("5",5);
}
|