File: mapmfact.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (244 lines) | stat: -rw-r--r-- 5,359 bytes parent folder | download | duplicates (5)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

/*
 *  M_APM  -  mapmfact.c
 *
 *  Copyright (C) 1999 - 2007   Michael C. Ring
 *
 *  Permission to use, copy, and distribute this software and its
 *  documentation for any purpose with or without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and
 *  that both that copyright notice and this permission notice appear
 *  in supporting documentation.
 *
 *  Permission to modify the software is granted. Permission to distribute
 *  the modified code is granted. Modifications are to be distributed by
 *  using the file 'license.txt' as a template to modify the file header.
 *  'license.txt' is available in the official MAPM distribution.
 *
 *  This software is provided "as is" without express or implied warranty.
 */

/*
 *
 *      This file contains the FACTORIAL function.
 *
 */

/*
 *      Brief explanation of the factorial algorithm.
 *      ----------------------------------------------
 *
 *      The old algorithm simply multiplied N * (N-1) * (N-2) etc, until
 *	the number counted down to '2'. So one term of the multiplication
 *	kept getting bigger while multiplying by the next number in the
 *	sequence.
 *
 *      The new algorithm takes advantage of the fast multiplication
 *	algorithm. The "ideal" setup for fast multiplication is when
 *	both numbers have approx the same number of significant digits
 *	and the number of digits is very near (but not over) an exact
 *	power of 2.
 *
 *	So, we will multiply N * (N-1) * (N-2), etc until the number of
 *	significant digits is approx 256.
 *
 *	Store this temp product into an array.
 *
 *	Then we will multiply the next sequence until the number of
 *	significant digits is approx 256.
 *
 *	Store this temp product into the next element of the array.
 *
 *	Continue until we've counted down to 2.
 *
 *	We now have an array of numbers with approx the same number
 *	of digits (except for the last element, depending on where it
 *	ended.) Now multiply each of the array elements together to
 *	get the final product.
 *
 *      The array multiplies are done as follows (assume we used 11
 *	array elements for this example, indicated by [0] - [10] ) :
 *
 *	initial    iter-1     iter-2       iter-3     iter-4
 *
 *	  [0]
 *	     *  ->  [0]
 *	  [1]
 *                      * ->    [0]
 *
 *	  [2]
 *	     *  ->  [1]
 *	  [3]
 *                                   * ->   [0]
 *
 *	  [4]
 *	     *  ->  [2]
 *	  [5]
 *
 *                      * ->    [1]
 *
 *	  [6]
 *	     *  ->  [3]                           *  ->  [0]
 *	  [7]
 *
 *
 *	  [8]
 *	     *  ->  [4]
 *	  [9]
 *                      * ->    [2]    ->   [1]
 *
 *
 *	  [10]  ->  [5]
 *
 */

#include "pgAdmin3.h"
#include "pgscript/utilities/mapm-lib/m_apm_lc.h"

/* define size of local array for temp storage */

#define NDIM 32

/****************************************************************************/
void	m_apm_factorial(M_APM moutput, M_APM minput)
{
	int     ii, nmul, ndigits, nd, jj, kk, mm, ct;
	M_APM   array[NDIM];
	M_APM   iprod1, iprod2, tmp1, tmp2;

	/* return 1 for any input <= 1 */

	if (m_apm_compare(minput, MM_One) <= 0)
	{
		m_apm_copy(moutput, MM_One);
		return;
	}

	ct       = 0;
	mm       = NDIM - 2;
	ndigits  = 256;
	nd       = ndigits - 20;
	tmp1     = m_apm_init();
	tmp2     = m_apm_init();
	iprod1   = m_apm_init();
	iprod2   = m_apm_init();
	array[0] = m_apm_init();

	m_apm_copy(tmp2, minput);

	/* loop until multiply count-down has reached '2' */

	while (TRUE)
	{
		m_apm_copy(iprod1, MM_One);

		/*
		 *   loop until the number of significant digits in this
		 *   partial result is slightly less than 256
		 */

		while (TRUE)
		{
			m_apm_multiply(iprod2, iprod1, tmp2);

			m_apm_subtract(tmp1, tmp2, MM_One);

			m_apm_multiply(iprod1, iprod2, tmp1);

			/*
			 *  I know, I know.  There just isn't a *clean* way
			 *  to break out of 2 nested loops.
			 */

			if (m_apm_compare(tmp1, MM_Two) <= 0)
				goto PHASE2;

			m_apm_subtract(tmp2, tmp1, MM_One);

			if (iprod1->m_apm_datalength > nd)
				break;
		}

		if (ct == (NDIM - 1))
		{
			/*
			 *    if the array has filled up, start multiplying
			 *    some of the partial products now.
			 */

			m_apm_copy(tmp1, array[mm]);
			m_apm_multiply(array[mm], iprod1, tmp1);

			if (mm == 0)
			{
				mm = NDIM - 2;
				ndigits = ndigits << 1;
				nd = ndigits - 20;
			}
			else
				mm--;
		}
		else
		{
			/*
			 *    store this partial product in the array
			 *    and allocate the next array element
			 */

			m_apm_copy(array[ct], iprod1);
			array[++ct] = m_apm_init();
		}
	}

PHASE2:

	m_apm_copy(array[ct], iprod1);

	kk = ct;

	while (kk != 0)
	{
		ii = 0;
		jj = 0;
		nmul = (kk + 1) >> 1;

		while (TRUE)
		{
			/* must use tmp var when ii,jj point to same element */

			if (ii == 0)
			{
				m_apm_copy(tmp1, array[ii]);
				m_apm_multiply(array[jj], tmp1, array[ii + 1]);
			}
			else
				m_apm_multiply(array[jj], array[ii], array[ii + 1]);

			if (++jj == nmul)
				break;

			ii += 2;
		}

		if ((kk & 1) == 0)
		{
			jj = kk >> 1;
			m_apm_copy(array[jj], array[kk]);
		}

		kk = kk >> 1;
	}

	m_apm_copy(moutput, array[0]);

	for (ii = 0; ii <= ct; ii++)
	{
		m_apm_free(array[ii]);
	}

	m_apm_free(tmp1);
	m_apm_free(tmp2);
	m_apm_free(iprod1);
	m_apm_free(iprod2);
}
/****************************************************************************/