Created
November 4, 2013 16:37
-
-
Save t1anchen/7305345 to your computer and use it in GitHub Desktop.
factorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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