47 long double factorial(
long double);
48 long double power_of_x(
long double,
long double);
49 long double sum_of_logs(
long double);
50 long double stirling_upperbound(
long double);
51 int binary_search_int(
int n,
int xleft,
int yleft,
int xright,
int yright);
52 int binary_search_int_arrayless(
int n,
int xleft,
int yleft,
int xright,
int yright);
53 long double binary_search_dble(
long double n,
long double xleft,
long double yleft,
long double xright,
long double yright);
55 static long double factor=-1.0;
66 long double n = 0.0, i=0.0;
67 long double prevlogsumoflogs=0.0;
68 long double prevsum = 0.0, sum = 0.0, prevsumdiff = 0.0, sumdiff = 0.0, term1 = 0.0;
70 for(n = 4110501.0; n <= 4199901.0; n++)
72 struct timeval tv_start;
73 gettimeofday(&tv_start,NULL);
75 cout<<
"=========================================================="<<endl;
77 cout<<
"Factorization begins at: "<<tv_start.tv_sec<<
" secs and "<<tv_start.tv_usec<<
" microsecs "<<endl;
88 double sumoflogs=sum_of_logs(n);
89 cout<<
"Factorization of n="<<n<<
"; log(N) ="<<log2l(n)<<
";(log(N))^3="<<log2l(n)*log2l(n)*log2l(n)<<
"; Theoretically derived upperbound == sum of logs which is total binary search time on all discretized tiles (with roundoff for negative values) :"<<sumoflogs<<endl;
90 cout<<
"Factorization of n="<<n<<
"; Stirling Upperbound (without roundoff for negative values) == "<<stirling_upperbound(n)<<endl;
93 struct timeval tv_end;
94 gettimeofday(&tv_end,NULL);
95 cout<<
"Factorization ends at: "<<tv_end.tv_sec<<
" secs and "<<tv_end.tv_usec<<
" microsecs "<<endl;
96 cout<<
"Factorization of n="<<n<<
" takes time duration:"<<tv_end.tv_sec-tv_start.tv_sec<<
" secs and "<<tv_end.tv_usec-tv_start.tv_usec<<
" microsecs "<<endl;
97 cout<<
"Factorization of n="<<n<<
", log(timeduration)/log(log(N)) [approximately could be the exponent of number of bits i.e polylog in input size divided by some constant ]:"<<log2l((
double)(tv_end.tv_usec-tv_start.tv_usec))/log2l(log2l(n))<<endl;
98 cout<<
"Factorization of n="<<n<<
", log(sumoflogs)/log(log(N)) [approximately could be the exponent of number of bits i.e polylog in input size divided by some constant ]:"<<log2l(sumoflogs)/log2l(log2l(n))<<endl;
99 cout<<
"Factorization of n="<<n<<
"; sumoflogs diff convergence test: present term - prev term:"<<log2l(sumoflogs)/log2l(log2l(n))-prevlogsumoflogs<<endl;
100 cout<<
"Factorization of n="<<n<<
"; sumoflogs/(log(n))^3 convergence test:"<<sumoflogs/(log2l(n)*log2l(n)*log2l(n))<<endl;
101 cout<<
"Factorization of n="<<n<<
"; sumoflogs diff ratio with prev term convergence test: (present term - prev term)/prev term:"<<(log2l(sumoflogs)/log2l(log2l(n))-prevlogsumoflogs)/prevlogsumoflogs<<endl;
102 cout<<
"Factorization of n="<<n<<
"; sumoflogs D'Alembert's convergence test: present term/prev term:"<<(log2l(sumoflogs)/log2l(log2l(n)))/prevlogsumoflogs<<endl;
103 prevlogsumoflogs=log2l(sumoflogs)/log2l(log2l(n));
106 cout<<
"=========================================================="<<endl;
113 long double factorial(
long double n)
115 long double fact_of_n=1.0;
116 for(
long double i=1.0; i<=n; i++)
118 fact_of_n=fact_of_n*i;
123 long double sum_of_logs(
long double n)
125 long double sumoflogs=0.0;
126 long double temp=0.0;
127 long double xtile_start=n/2.0;
128 long double xtile_end=n;
129 long double xtile_sum=n/2.0;
133 if(log2l(n/(y*(y+1.0))) < 0)
136 temp = log2l(n/(y*(y+1.0)));
144 factor=binary_search_int_arrayless((
int)n,(
int)(xtile_start)-PADDING,(
int)y,(
int)(xtile_end)+PADDING,(
int)y);
147 xtile_end=xtile_start;
148 xtile_start=xtile_end-(n/((y+1.0)*(y+2.0)));
150 xtile_sum += (n/(y*(y+1.0)));
158 int binary_search_int_arrayless(
int N,
int xleft,
int yleft,
int xright,
int yright)
166 if ((xleft+(
int)(xright-xleft/2))*yleft == N)
168 cout<<
"binary_search_int_arrayless(): N="<<N<<
" (a "<<log2l(N)<<
" bit number); Factor is "<<xleft+(int)(xright-xleft)/2<<
"; Factor*(yright or yleft)="<<(xleft+((int)((xright-xleft)/2)))<<
"*"<<yright<<
"="<<(xleft+((
int)((xright-xleft)/2)))*yright<<endl;
169 return xleft+(int)((xright-xleft)/2);
173 if ((xleft+(
int)(floor((xright-xleft)/2)))*yleft > N)
176 binary_search_int(N, xleft, yleft, xleft+(
int)((xright-xleft)/2), yright);
181 binary_search_int(N, xleft+(
int)((xright-xleft)/2)+1, yleft, xright, yright);
186 int binary_search_int(
int N,
int xleft,
int yleft,
int xright,
int yright)
188 int array[(xright-xleft)+1];
192 for(i=0;i <= (xright-xleft); i++)
194 array[i]=(xleft+i)*yleft;
199 if(i==0 ||
sizeof(array) == 4)
203 if (array[(
int)((xright-xleft)/2)] == N)
205 cout<<
"binary_search_int(): N="<<N<<
" (a "<<log2l(N)<<
" bit number); Factor is "<<xleft+(int)((xright-xleft)/2)<<
"; Factor*(yright or yleft)="<<(xleft+((
int)((xright-xleft)/2)))<<
"*"<<yright<<
"="<<(xleft+((
int)((xright-xleft)/2)))*yright<<endl;
206 return xleft+(int)((xright-xleft)/2);
210 if (array[(
int)((xright-xleft)/2)] > N)
211 binary_search_int(N, xleft, yleft, xleft+(
int)((xright-xleft)/2), yright);
213 binary_search_int(N, xleft+(
int)((xright-xleft)/2)+1, yleft, xright, yright);
218 long double binary_search_dble(
long double N,
long double xleft,
long double yleft,
long double xright,
long double yright)
220 int array[(int)(xright-xleft)+1];
222 cout<<
"binary_search_dble(): tile searched is [";
223 for(
int i=0;i <= ((int)xright-(
int)xleft); i++)
225 cout<<
"binary_search_dble(): i="<<i<<
"; (xleft+i)="<<(xleft+i)<<
"; yleft="<<yleft<<endl;
226 array[i]=(int)(((
int)xleft+i)*yleft);
230 cout<<
"size of tile array:"<<
sizeof(array)<<endl;
235 if (array[(((
int)xright-(int)xleft)/2)] == N)
237 cout<<
"binary_search_dble(): N="<<N<<
"; Factor is "<<xleft+((int)xright-(
int)xleft)/2<<endl;
238 return xleft+((
int)xright-(
int)xleft)/2;
242 if (array[(((
int)xright-(int)xleft)/2)] > N)
243 binary_search_dble(N, xleft, yleft, xleft+(((
int)xright-(
int)xleft)/2), yright);
245 binary_search_dble(N, xleft+(((
int)xright-(
int)xleft)/2), yleft, xright, yright);
249 long double stirling_upperbound(
long double n)
251 long double term=0.0;
252 long double e=2.71828;
253 long double pi=3.14159265359;
254 long double term1=0.0;
255 long double term2=0.0;
256 term1=power_of_x(e, 2*n);
257 term2=2*pi*e*n*power_of_x(n-1,n);
258 return log2l(term1/term2);
261 long double power_of_x(
long double x,
long double n)
263 long double power = 1.0 ;
264 for (
long double i=n;i > 0.0;i--)