项目作者: bharatrajan

项目描述 :
Simple factorial generator (JavaScript)
高级语言: JavaScript
项目地址: git://github.com/bharatrajan/factorial.git
创建时间: 2017-12-08T07:17:49Z
项目社区:https://github.com/bharatrajan/factorial

开源协议:Other

下载


Factorial generator

Simple factorial generator written in JavaScript. This utility function uses bigNumber

Factorial Definition

The Factorial of a number n, can be defined by:

  1. n! = (n-1)! * n
  2. 0! = 1

Algorithm and reasoning

  • bigNumber : An external library is used as JavaScript safe integer limits to (2^53) - 1
  • Used iterative approach instead of recursive due to performance
  • Memoization is used to store the results. Thus, cached results are used for future computations

Room for improvement

  • Memoization is costing space.
    • It took 40MB(approx.) for an array to store factorials of first 2000 integers
    • We can watch the trend for n and optimize the cache array
      • We can warm up this cache array during initialization
      • We can put an upper limit for this this cache array

Credits