Skip to content

Instantly share code, notes, and snippets.

@t1anchen
Created November 4, 2013 16:37
Show Gist options
  • Save t1anchen/7305345 to your computer and use it in GitHub Desktop.
Save t1anchen/7305345 to your computer and use it in GitHub Desktop.
factorial
;; Take k-th product while n!(n-1)!...(n-k+1)!...2!1!
;;
(map (fn [x] (reduce * x)) (take 10 (iterate rest (range 10 0 -1))))
;; (3628800 362880 40320 5040 720 120 24 6 2 1)
(reduce *' (map (fn [x] (reduce *' x)) (take 10 (iterate rest (range 10 0 -1)))))
;; 6658606584104736522240000000N
// Take k-th product while n!(n-1)!...(n-k+1)!...2!1!
//
1.to(10).reverse.toList.tails.toList.filter(_.size>0).map(_.reduce(_*_))
// res18: List[Int] = List(3628800, 362880, 40320, 5040, 720, 120, 24, 6, 2, 1)
1.to(10).reverse.toList.tails.toList.filter(_.size>0).map(_.reduce(_*_)).foldLeft(BigInt(1))(_*_)
// res19: scala.math.BigInt = 6658606584104736522240000000
10.to(1).by(-1).toList.tails.toList.filter(_.size>0).map(_.reduce(_*_)).foldLeft(BigInt(1))(_*_)
// res23: scala.math.BigInt = 6658606584104736522240000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment