| 1 |
module etc.bigint.factorial; |
|---|
| 2 |
import etc.bigint.bigint_int; |
|---|
| 3 |
|
|---|
| 4 |
/* |
|---|
| 5 |
|
|---|
| 6 |
Copyright (c) 2004, Arcane Jill |
|---|
| 7 |
|
|---|
| 8 |
All rights reserved. Intellectual Property Me Arse! |
|---|
| 9 |
|
|---|
| 10 |
Redistribution and use in source and binary forms, with or without modification, are permitted |
|---|
| 11 |
provided that the following conditions are met: |
|---|
| 12 |
|
|---|
| 13 |
* Redistributions of source code must retain the above copyright notice, the phrase |
|---|
| 14 |
"Intellectual Property Me Arse!", this list of conditions, and the following disclaimer. |
|---|
| 15 |
* Redistributions in binary form must reproduce the above copyright notice, the phrase |
|---|
| 16 |
"Intellectual Property Me Arse!", this list of conditions and the following disclaimer |
|---|
| 17 |
in the documentation and/or other materials provided with the distribution. |
|---|
| 18 |
* The name Arcane Jill may not be used to endorse or promote products derived from this |
|---|
| 19 |
software without specific prior written permission. |
|---|
| 20 |
|
|---|
| 21 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
|---|
| 22 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
|---|
| 23 |
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER, |
|---|
| 24 |
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 25 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 26 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED, AND ON ANY |
|---|
| 27 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
|---|
| 29 |
OF SUCH DAMAGE. |
|---|
| 30 |
|
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 34 |
Returns z = x! |
|---|
| 35 |
*/ |
|---|
| 36 |
|
|---|
| 37 |
Int factorial(Int n) |
|---|
| 38 |
{ |
|---|
| 39 |
if (n.isInt()) return factorial(n.toInt()); |
|---|
| 40 |
if (n < 0) throw new IntException("Factorial(n) not defined for n < 0"); |
|---|
| 41 |
throw new IntException("Overflow in Factorial(n)"); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
Int factorial(int n) |
|---|
| 45 |
{ |
|---|
| 46 |
if (n < 0) throw new IntException("Factorial(n) not defined for n < 0"); |
|---|
| 47 |
if (n < 2) return Int.ONE; |
|---|
| 48 |
|
|---|
| 49 |
// Stage one. Here we use only uints, for speed. |
|---|
| 50 |
Int[] pool; |
|---|
| 51 |
uint a = 1; |
|---|
| 52 |
uint i; |
|---|
| 53 |
for (i=2; i<=n; ++i) |
|---|
| 54 |
{ |
|---|
| 55 |
if ((bsr(i) + bsr(a)) <= 30) |
|---|
| 56 |
{ |
|---|
| 57 |
a *= i; |
|---|
| 58 |
} |
|---|
| 59 |
else |
|---|
| 60 |
{ |
|---|
| 61 |
pool.length = pool.length + 1; |
|---|
| 62 |
pool[pool.length-1] = new Int(a); |
|---|
| 63 |
a = i; |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
pool.length = pool.length + 1; |
|---|
| 67 |
pool[pool.length-1] = new Int(a); |
|---|
| 68 |
|
|---|
| 69 |
// Stage two. Now multiply all the Ints together, trying to keep all the sizes balanced as we go. |
|---|
| 70 |
while (pool.length > 1) |
|---|
| 71 |
{ |
|---|
| 72 |
for (i=0; i<(pool.length>>1); ++i) |
|---|
| 73 |
{ |
|---|
| 74 |
pool[i] = pool[i+i] * pool[i+i+1]; |
|---|
| 75 |
} |
|---|
| 76 |
if (pool.length == i+i) |
|---|
| 77 |
{ |
|---|
| 78 |
pool.length = i; |
|---|
| 79 |
} |
|---|
| 80 |
else |
|---|
| 81 |
{ |
|---|
| 82 |
pool[i] = pool[i+i]; |
|---|
| 83 |
pool.length = i+1; |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
// All done. We have our result |
|---|
| 88 |
return pool[0]; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
version(Paranoid) unittest |
|---|
| 92 |
{ |
|---|
| 93 |
assert(factorial(50) == new Int("30414093201713378043612608166064768844377641568960512000000000000")); |
|---|
| 94 |
} |
|---|