Skip to content

Commit

Permalink
Improve performance of factorial for BigNumbers (#1691)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Leoniak authored and josdejong committed Dec 31, 2019
1 parent 64972a0 commit 1f10538
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
24 changes: 15 additions & 9 deletions src/function/probability/gamma.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,29 @@ export const createGamma = /* #__PURE__ */ factory(name, dependencies, ({ typed,
* @param {BigNumber} n
* @returns {BigNumber} Returns the factorial of n
*/

function bigFactorial (n) {
if (n.isZero()) {
return new BigNumber(1) // 0! is per definition 1
if (n < 8) {
return new BigNumber([1, 1, 2, 6, 24, 120, 720, 5040][n])
}

const precision = config.precision + (Math.log(n.toNumber()) | 0)
const Big = BigNumber.clone({ precision: precision })

let res = new Big(n)
let value = n.toNumber() - 1 // number
while (value > 1) {
res = res.times(value)
value--
if (n % 2 === 1) {
return n.times(bigFactorial(new BigNumber(n - 1)))
}

let p = n
let prod = new Big(n)
let sum = n.toNumber()

while (p > 2) {
p -= 2
sum += p
prod = prod.times(sum)
}

return new BigNumber(res.toPrecision(BigNumber.precision))
return new BigNumber(prod.toPrecision(BigNumber.precision))
}

return gamma
Expand Down
38 changes: 34 additions & 4 deletions test/benchmark/factorial.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Benchmark = require('benchmark')
const BigNumber = require('decimal.js')
const padRight = require('pad-right')

function pad (text) {
return padRight(text, 40, ' ')
}
Expand All @@ -23,6 +24,10 @@ function bigFactorial (n) {
}

function betterFactorial (n) {
if (n < 8) {
return new BigNumber([1, 1, 2, 6, 24, 120, 720, 5040][n])
}

if (n % 2 === 1) {
return n.times(betterFactorial(new BigNumber(n - 1)))
}
Expand All @@ -40,19 +45,26 @@ function betterFactorial (n) {
return prod
}

console.log('factorial(5) = ' + bigFactorial(new BigNumber(5)))
console.log('new factorial(5) = ' + betterFactorial(new BigNumber(5)))

const suite = new Benchmark.Suite()
suite
.add(pad('bigFactorial for small numbers'), function () {
const res = bigFactorial(new BigNumber(8))
results.push(res)
})
.add(pad('new bigFactorial'), function () {
.add(pad('new bigFactorial for small numbers'), function () {
const res = betterFactorial(new BigNumber(8))
results.push(res)
})

.add(pad('bigFactorial for small numbers 2'), function () {
const res = bigFactorial(new BigNumber(20))
results.push(res)
})
.add(pad('new bigFactorial for small numbers 2'), function () {
const res = betterFactorial(new BigNumber(20))
results.push(res)
})

.add(pad('bigFactorial for big numbers'), function () {
const res = bigFactorial(new BigNumber(600))
results.push(res)
Expand All @@ -61,6 +73,24 @@ suite
const res = betterFactorial(new BigNumber(600))
results.push(res)
})

.add(pad('bigFactorial for HUGE numbers'), function () {
const res = bigFactorial(new BigNumber(1500))
results.push(res)
})
.add(pad('new bigFactorial for HUGE numbers'), function () {
const res = betterFactorial(new BigNumber(1500))
results.push(res)
})

.add(pad('bigFactorial for "HUGER" numbers'), function () {
const res = bigFactorial(new BigNumber(10000))
results.push(res)
})
.add(pad('new bigFactorial for "HUGER" numbers'), function () {
const res = betterFactorial(new BigNumber(10000))
results.push(res)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
Expand Down

0 comments on commit 1f10538

Please sign in to comment.