Skip to content

Instantly share code, notes, and snippets.

@nobuh
Created August 18, 2011 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobuh/1153720 to your computer and use it in GitHub Desktop.
Save nobuh/1153720 to your computer and use it in GitHub Desktop.
BigInteger factorial in CoffeeScript
# Usage: coffee factorial.coffee _number_
bi = (require "./bigdecimal").BigInteger # github.com/jhs/bigdecimal.js
ONE = bi '1'
n = bi (process.argv[2] or= '100')
# factorial (BigInteger) returns BigInteger
factorial = (x) ->
if x.compareTo(ONE) is 0
ONE
else
x.multiply factorial x.subtract ONE
console.log ((factorial n).toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment