Krishna iResearch Intelligent Cloud Platform - acadpdrafts
DiscreteHyperbolicFactorizationUpperbound.cpp
1 /***************************************************************************************
2 ASFER - a ruleminer which gets rules specific to a query and executes them
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 ---------------------------------------------------------------------------------------------------
18 Copyright (C):
19 Srinivasan Kannan (alias) Ka.Shrinivaasan (alias) Shrinivas Kannan
20 Independent Open Source Developer, Researcher and Consultant
21 Ph: 9789346927, 9003082186, 9791165980
22 Open Source Products Profile(Krishna iResearch): http://sourceforge.net/users/ka_shrinivaasan
23 Personal website(research): https://sites.google.com/site/kuja27/
24 emails: ka.shrinivaasan@gmail.com, shrinivas.kannan@gmail.com, kashrinivaasan@live.com
25 ---------------------------------------------------------------------------------------------------
26 *****************************************************************************************/
27 
28 /*
29  IMPORTANT CAUTIONARY DISCLAIMER:
30  -------------------------------
31  This code is purely an academic research exercise. The author is NOT RESPONSIBLE for any unauthorized, unlawful execution and usage
32  of this code with criminal intent.
33 */
34 
35 using namespace std;
36 #include <iostream>
37 #include <fstream>
38 
39 extern "C"
40 {
41 #include <stdio.h>
42 #include <math.h>
43 #include <stdlib.h>
44 #include <sys/time.h>
45 }
46 
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);
54 
55 static long double factor=-1.0;
56 
57 const int PADDING=3;
58 
59 int main()
60 {
61 /*
62 Textual Function Plotter Code for Computation of Upperbound for Discrete Hyperbolic Factorization with Stirling Formula - using Rectangular Binary or Interpolation Search (10 September 2013) - http://sourceforge.net/projects/acadpdrafts/files/DiscreteHyperbolicFactorization_UpperboundDerivedWithStirlingFormula_2013-09-10.pdf/download
63 
64 Complete LatexPDF draft version uploaded at: http://sourceforge.net/projects/acadpdrafts/files/DiscreteHyperbolicPolylogarithmicSieveForIntegerFactorization_updated_rectangular_interpolation_search_and_StirlingFormula_Upperbound.pdf/download
65 */
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;
69  //for(n = 2.0; n <= 5000000.0; n++)
70  for(n = 4110501.0; n <= 4199901.0; n++)
71  {
72  struct timeval tv_start;
73  gettimeofday(&tv_start,NULL);
74  //term1 = power_of_x(n,sqrt(n)-1)/(factorial(sqrt(n)-1.0)*factorial(sqrt(n))) ;
75  cout<<"=========================================================="<<endl;
76  //cout<<"term1 = "<<term1<<"; log(n^(sqrt(n)-1)/sqrt(n)!(sqrt(n)-1)!)/loglogn = "<<log2l(term1)/log2l(log2l(n))<<endl;
77  cout<<"Factorization begins at: "<<tv_start.tv_sec<<" secs and "<<tv_start.tv_usec<<" microsecs "<<endl;
78  system("date");
79  fflush(stdout);
80  //cout<<"Factorization of n="<<n<<"; Stirling upperbound for sum of logs ---------- ="<<stirling_upperbound(n)<<endl;
81 
82  /*
83  Sum of log/n*(n+1) and its Stirling Upperbound log(n^(n-1)/n!*(n-1)!) and its intriguingly widening gap with log(n)
84  - Ka.Shrinivaasan 8November2013
85  Added a roundoff for negative values in sum_of_logs()
86  - Ka.Shrinivaasan 12November2013
87  */
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;
91  //cout<<"Factorization of n="<<n<<"; sum of logs as single log ----- log2l(n^(n-1)/n!*(n-1)!) ="<<log2l(term1)<<endl;
92  //cout<<"Factorization of n, log2l(n)="<<log2l(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));
104  system("date");
105  fflush(stdout);
106  cout<<"=========================================================="<<endl;
107  }
108  //binary_search_int(232,2,11,15,11);
109  //cout<<"Binary search for factors of 35 as :["<<factor<<",5]<<"<<endl;
110 }
111 
112 
113 long double factorial(long double n)
114 {
115  long double fact_of_n=1.0;
116  for(long double i=1.0; i<=n; i++)
117  {
118  fact_of_n=fact_of_n*i;
119  }
120  return fact_of_n;
121 }
122 
123 long double sum_of_logs(long double n)
124 {
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;
130  long double y=1.0;
131  do
132  {
133  if(log2l(n/(y*(y+1.0))) < 0)
134  temp = 0.0; // tile has length 1
135  else
136  temp = log2l(n/(y*(y+1.0)));
137  //cout<<"########"<<endl;
138  //cout<<"tesselation from 1 to N="<<n<<" ------ discretized tile for log(deltai) at i="<<i<<" : "<<temp<<endl;
139  //cout<<"binary_search_int("<<(int)n<<","<<(int)tile_sum<<","<<(int)i<<","<<(int)(tile_sum+(n/(i*(i+1.0))))<<","<<(int)i<<")"<<endl;
140  //cout<<"tesselation from 1 to N="<<n<<" ------ discretized tile for log(deltai) at i="<<y<<" ranges from "<<xtile_start<<" to "<<xtile_end<<endl;
141  //cout<<"binary_search_int("<<(int)n<<","<<(int)xtile_start<<","<<(int)y<<","<<(int)(xtile_end)<<","<<(int)y<<")"<<endl;
142  //factor=binary_search_int((int)n,(int)(xtile_start)-PADDING,(int)y,(int)(xtile_end)+PADDING,(int)y);
143  //cout<<"binary_search_int_arrayless("<<(int)n<<","<<(int)xtile_start<<","<<(int)y<<","<<(int)(xtile_end)<<","<<(int)y<<")"<<endl;
144  factor=binary_search_int_arrayless((int)n,(int)(xtile_start)-PADDING,(int)y,(int)(xtile_end)+PADDING,(int)y);
145  //cout<<"binary_search_dble("<<n<<","<<xtile_start<<","<<y<<","<<(xtile_end)<<","<<y<<")"<<endl;
146  //factor=binary_search_dble(n,xtile_start,y,xtile_end,y);
147  xtile_end=xtile_start;
148  xtile_start=xtile_end-(n/((y+1.0)*(y+2.0)));
149  //cout<<"########"<<endl;
150  xtile_sum += (n/(y*(y+1.0)));
151  sumoflogs += temp;
152  }
153  while(y++ < (n));
154 
155  return sumoflogs;
156 }
157 
158 int binary_search_int_arrayless(int N, int xleft, int yleft, int xright, int yright)
159 {
160  /*
161  For DiscreteHyperbolicFactorization tile search always yleft==yright
162  - Ka.Shrinivaasan 8November2013
163  */
164 
165  //cout<<"(int)((xleft+xright)/2):"<<(int)((xleft+xright)/2)<<"; array[(int)((xright-xleft)/2)]:"<<array[(int)((xright-xleft)/2)]<<endl;
166  if ((xleft+(int)(xright-xleft/2))*yleft == N)
167  {
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);
170  }
171  else
172  {
173  if ((xleft+(int)(floor((xright-xleft)/2)))*yleft > N)
174  {
175  //binary_search_int_arrayless(N, xleft, yleft, xleft+(int)(floor((xright-xleft)/2)), yright);
176  binary_search_int(N, xleft, yleft, xleft+(int)((xright-xleft)/2), yright);
177  }
178  else
179  {
180  //binary_search_int_arrayless(N, xleft+(int)(floor((xright-xleft)/2))+1, yleft, xright, yright);
181  binary_search_int(N, xleft+(int)((xright-xleft)/2)+1, yleft, xright, yright);
182  }
183  }
184 }
185 
186 int binary_search_int(int N, int xleft, int yleft, int xright, int yright)
187 {
188  int array[(xright-xleft)+1];
189  //cout<<"binary_search_int(): xleft = "<<xleft<<"; xright = "<<xright<<endl;
190  //cout<<"binary_search_int(): tile searched is [";
191  int i;
192  for(i=0;i <= (xright-xleft); i++)
193  {
194  array[i]=(xleft+i)*yleft;
195  //cout<<array[i]<<",";
196  }
197  //cout<<"]"<<endl;
198  //cout<<"sizeof tile:"<<sizeof(array)<<endl;
199  if(i==0 || sizeof(array) == 4)
200  return xright;
201  i=0;
202  //cout<<"(int)((xleft+xright)/2):"<<(int)((xleft+xright)/2)<<"; array[(int)((xright-xleft)/2)]:"<<array[(int)((xright-xleft)/2)]<<endl;
203  if (array[(int)((xright-xleft)/2)] == N)
204  {
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);
207  }
208  else
209  {
210  if (array[(int)((xright-xleft)/2)] > N)
211  binary_search_int(N, xleft, yleft, xleft+(int)((xright-xleft)/2), yright);
212  else
213  binary_search_int(N, xleft+(int)((xright-xleft)/2)+1, yleft, xright, yright);
214  }
215 }
216 
217 
218 long double binary_search_dble(long double N, long double xleft, long double yleft, long double xright, long double yright)
219 {
220  int array[(int)(xright-xleft)+1];
221  //cout<<"binary_search_int(): xleft = "<<xleft<<"; xright = "<<xright<<endl;
222  cout<<"binary_search_dble(): tile searched is [";
223  for(int i=0;i <= ((int)xright-(int)xleft); i++)
224  {
225  cout<<"binary_search_dble(): i="<<i<<"; (xleft+i)="<<(xleft+i)<<"; yleft="<<yleft<<endl;
226  array[i]=(int)(((int)xleft+i)*yleft);
227  cout<<array[i]<<",";
228  }
229  cout<<"]"<<endl;
230  cout<<"size of tile array:"<<sizeof(array)<<endl;
231  if(sizeof(array)==4)
232  return xleft;
233 
234  //cout<<"(int)((xleft+xright)/2):"<<(int)((xleft+xright)/2)<<"; array[(int)((xright-xleft)/2)]:"<<array[(int)((xright-xleft)/2)]<<endl;
235  if (array[(((int)xright-(int)xleft)/2)] == N)
236  {
237  cout<<"binary_search_dble(): N="<<N<<"; Factor is "<<xleft+((int)xright-(int)xleft)/2<<endl;
238  return xleft+((int)xright-(int)xleft)/2;
239  }
240  else
241  {
242  if (array[(((int)xright-(int)xleft)/2)] > N)
243  binary_search_dble(N, xleft, yleft, xleft+(((int)xright-(int)xleft)/2), yright);
244  else
245  binary_search_dble(N, xleft+(((int)xright-(int)xleft)/2), yleft, xright, yright);
246  }
247 }
248 
249 long double stirling_upperbound(long double n)
250 {
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);
259 }
260 
261 long double power_of_x(long double x, long double n)
262 {
263  long double power = 1.0 ;
264  for (long double i=n;i > 0.0;i--)
265  power = power * x;
266  return power;
267 }