Skip to content

Commit 9e99f73

Browse files
authored
Add combinations(n)
1 parent f49d0f8 commit 9e99f73

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/main/scala/com/github/pathikrit/scalgos/Combinatorics.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ object Combinatorics {
1919
* @return all 2^n ways of choosing elements from s
2020
*/
2121
def combinations[A](s: Seq[A]) = for {i <- 0 to s.length; j <- s combinations i} yield j
22+
23+
/**
24+
* All 2^^n possible combos of n from 0 to 1<<n
25+
* @param n
26+
* @return
27+
*/
28+
def combinations(n: Int): Seq[IndexedSeq[Int]] = (0 until 1<<n).map(i => (0 until n).map(j => ((i>>j)&1) == 1))
2229

2330
/**
2431
* Combinations with repeats e.g. (2, Set(A,B,C)) -> AA, AB, AC, BA, BB, BC, CA, CB, CC

0 commit comments

Comments
 (0)