combinatorics

Finding all possible value combinations between two arrays

微笑、不失礼 提交于 2019-11-26 23:30:28
问题 I have two arrays of strings, not necessarily of the same length, I want to find all the possible "sets" of combinations between two values from the arrays, without repeats from either array. For example, given the arrays: { "A1", "A2", "A3" } { "B1", "B2" } The result I want is the following sets: { ("A1", "B1"), ("A2", "B2") } { ("A1", "B1"), ("A3", "B2") } { ("A1", "B2"), ("A2", "B1") } { ("A1", "B2"), ("A3", "B1") } { ("A2", "B1"), ("A3", "B2") } { ("A2", "B2"), ("A3", "B1") } My general

Binomial coefficient modulo 142857

流过昼夜 提交于 2019-11-26 23:10:15
问题 How to calculate binomial coefficient modulo 142857 for large n and r . Is there anything special about the 142857? If the question is modulo p where p is prime then we can use Lucas theorem but what should be done for 142857. 回答1: The algorithm is: factorise the base into prime powers; 142857 = 3^3×11×13×37 compute the result modulo each prime power combine the results using the Chinese Remainder Theorem. To compute (n above k) mod p^q : Source: http://www.dms.umontreal.ca/~andrew/PDF

Next Composition of n into k parts - does anyone have a working algorithm?

我怕爱的太早我们不能终老 提交于 2019-11-26 23:09:53
问题 Composition of n into k parts - I want to list all the possible compositions of n into k parts - does anyone have an algorithm (preferably in R)? Or know if it's in library anywhere? For example, if I have n cubes, and k bags, and want to list all the possible arrangements of the cubes in the bags. e.g. there are 3 ways you can arrange 2 cubes into 2 bags: (2, 0) (1, 1) (0, 2) I've found the NEXCOM alogarithm. I've found a version of it here (page 46) in Fortran, but don't code in Fortran so

generate all partitions of a set [closed]

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:42:43
For a set of the form A = {1, 2, 3, ..., n} . It is called partition of the set A , a set of k<=n elements which respect the following theorems: a) the union of all the partitions of A is A b) the intersection of 2 partitions of A is the empty set (they can't share the same elements). For example. A = {1, 2,... n} We have the partitions: {1, 2, 3} {1, 2} {3} {1, 3} {2} {2, 3} {1} {1} {2} {3} These theoretical concepts are presented in my algorithms textbook (by the way, this subchapter is part of the "Backtracking" chapter). I am supposed to find an algorithm to generate all the partitions of

android lock password combinations

只愿长相守 提交于 2019-11-26 22:39:31
问题 I just came across with this interesting question from my colleague. I'm trying now, but meanwhile I thought I could share it here. With the password grid shown in the Android home screen, how many valid passwords are possible? min password length: 4 max: 9 (correct me if I'm wrong) 回答1: Summary The full combinations of 4 to 9 distinctive numbers, minus the combinations which include invalid "jump"s. The Long Version The rule for Android 3x3 password grid: one point for once cannot "jump"

What does this list permutations implementation in Haskell exactly do?

让人想犯罪 __ 提交于 2019-11-26 22:31:42
问题 I am studying the code in the Data.List module and can't exactly wrap my head around this implementation of permutations: permutations :: [a] -> [[a]] permutations xs0 = xs0 : perms xs0 [] where perms [] _ = [] perms (t:ts) is = foldr interleave (perms ts (t:is)) (permutations is) where interleave xs r = let (_,zs) = interleave' id xs r in zs interleave' _ [] r = (ts, r) interleave' f (y:ys) r = let (us,zs) = interleave' (f . (y:)) ys r in (y:us, f (t:y:us) : zs) Can somebody explain in

Finding all the unique permutations of a string without generating duplicates

强颜欢笑 提交于 2019-11-26 22:23:19
Finding all the permutations of a string is by a well known Steinhaus–Johnson–Trotter algorithm. But if the string contains the repeated characters such as AABB, then the possible unique combinations will be 4!/(2! * 2!) = 6 One way of achieving this is that we can store it in an array or so and then remove the duplicates. Is there any simpler way to modify the Johnson algorithm, so that we never generate the duplicated permutations. (In the most efficient way) Use the following recursive algorithm: PermutList Permute(SymArray fullSymArray){ PermutList resultList=empty; for( each symbol A in

Generate sample of 1,000,000 random permutations

一世执手 提交于 2019-11-26 22:07:25
问题 I am working with large number of integer permutations. The number of elements in each permutation is K. The element size is 1 byte. I need to generate N unique random permutations. Constraints: K <= 144, N <= 1,000,000. I came up with the following straightforward algorithm: Generate list of N random permutations. Store all permutations in RAM. Sort the list and delete all duplicates (if any). The number of duplicates will be relatively small. If there were any duplicates, add random

Get all possible word combinations

狂风中的少年 提交于 2019-11-26 21:59:32
问题 I have a list of n words (let's say 26). Now I want to get a list of all possible combinations, but with a maximum of k words per row (let's say 5) So when my word list is: aaa, bbb, ..., zzz I want to get: aaa bbb ... aaabbb aaaccc ... aaabbbcccdddeeefff aaabbbcccdddeeeggg ... I want to make it variable, so that it will work with any n or k value. There should be no word be twice and every combinations needs to be taken (even if there are very much). How could I achieve that? EDIT: Thank you

Combinatoric 'N choose R' in java math?

痞子三分冷 提交于 2019-11-26 19:10:41
问题 Is there a built in method in a java library that can compute 'N choose R' for any N, R? 回答1: The apache-commons "Math" supports this in org.apache.commons.math4.util.CombinatoricsUtils 回答2: The Formula It's actually very easy to compute N choose K without even computing factorials. We know that the formula for (N choose K) is: N! -------- (N-K)!K! Therefore, the formula for (N choose K+1) is: N! N! N! N! (N-K) ---------------- = --------------- = -------------------- = -------- x ----- (N-(K