combinations

What is a good way to implement choose notation in Java?

... preferably in Java. Here is what I have: //x choose y public static double choose(int x, int y) { if (y < 0 || y > x) return 0; if (y == 0 || y == x) return 1; double answer = 1; for (int i = x-y+1; i <= x; i++) { answer = answer * i; } for (int j = y; j > 1; j--) { answer = answer / j; } return answer; } I'm wonderin...

Howto create combinations of several vectors without hardcoding loops in C++?

I have several data that looks like this: Vector1_elements = T,C,A Vector2_elements = C,G,A Vector3_elements = C,G,T ..... up to ... VectorK_elements = ... #Note also that the member of each vector is always 3. What I want to do is to create all combination of elements in Vector1 through out VectorK. Hence in the end we hope to get t...

PHP Array Combinations

Hi Guys, Im using the accepted answer in this question. As I have the same requirements, I need to get all combinations of an array of variable length with a variable number of elements. However I also need it to produce all combinations which don't use all the elements of the array, but are in order. If that makes sense? So if this is ...

Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists

I have a dataset of ca. 9K lists of variable length (1 to 100K elements). I need to calculate the length of the intersection of all possible 2-list combinations in this dataset. Note that elements in each list are unique so they can be stored as sets in python. What is the most efficient way to perform this in python? Edit I forgot to ...

Generate all combinations of a char array inside of a CUDA __device__ kernel

Hi, I need help please. I started to program a common brute forcer / password guesser with CUDA (2.3 / 3.0beta). I tried different ways to generate all possible plain text "candidates" of a defined ASCII char set. In this sample code I want to generate all 74^4 possible combinations (and just output the result back to host/stdout). $...

Generate all binary strings of length n with k bits set

What's the best algorithm to find all binary strings of length n that contain k bits set? For example, if n=4 and k=3, there are... 0111 1011 1101 1110 I need a good way to generate these given any n and any k so I'd prefer it to be done with strings. ...

C++ Newbie needs helps for printing combinations of integers

Suppose I am given: A range of integers iRange (i.e. from 1 up to iRange) and A desired number of combinations I want to find the number of all possible combinations and print out all these combinations. For example: Given: iRange = 5 and n = 3 Then the number of combinations is iRange! / ((iRange!-n!)*n!) = 5! / (5-3)! * 3! = 10 ...

remove numbers from a list without changing total sum

I have a list of numbers (example: [-1, 1, -4, 5]) and I have to remove numbers from the list without changing the total sum of the list. I want to remove the numbers with biggest absolute value possible, without changing the total, in the example removing [-1, -4, 5] will leave [1] so the sum doesn't change. I wrote the naive approach,...

reorganizing a series of 19 bytes into every single combination of any length

There are these 19 bytes (I am looking for combinations not the number of combinations) 17 00 00 00 A4 EA DB 13 02 00 00 00 00 00 00 A3 D3 02 CC I need any possible unique combination which matches these "rules": at least 4 bytes long the order of the bytes can't change(so 17 A3 D3 02 CC is ok but A3 D3 02 CC 17 isn't, because in th...

Mysql Update + SELECT query help please.

I want to update data table for those who score exam id 1,2 more than 80. I try this UPDATE data SET column = 'value' WHERE (SELECT * FROM exams WHERE (id = '1' AND score >= 80) AND (id = '2' AND score >= 80)); It gives me 0 result. But it should have few hundreds results ANy help?? I think the problem is this: SELECT * FROM exams ...

Python get all combinations of numbers

I'm trying to display all possible combinations of a list of numbers, for example if I have 334 I want to get: 3 3 4 3 4 3 4 3 3 I need to be able to do this for any set of digits up to about 12 digits long. I'm sure its probably fairly simple using something like itertools.combinations but I can't quite get the syntax right. TIA Sa...

Number of possible outcomes for 2 numbers given that one number is greater than the other

I am trying to write an algorithm to calculate outcomes. But I need help with combinatorics. Suppose I have to choose 2 numbers from 1 to 10. From the fundamental rule of counting, in the absence of any restriction, the number of possible outcomes is 10 * 10 = 100. (10 possible outcomes in choosing the first number x 10 possible outcom...

counting combinations and permutations efficiently

I have some code to count permutations and combinations, and I'm trying to make it work better for large numbers. I've found a better algorithm for permutations that avoids large intermediate results, but I still think I can do better for combinations. So far, I've put in a special case to reflect the symmetry of nCr, but I'd still lik...

Looking for "Domino combination" algorithm

Hello, I'm going to complete my apprenticeship as coder and i got a nice j2me project to work on. But i have to admit that i'm not that good with mathematical algorithms as i'd like to be. My problem is to create all possible "domino pairs" from a given set of values. For example: The possible values go from 0 to 6. Now imagine some ...

All valid combinations of points, in the most (speed) effective way

I know there are quite some questions out there on generating combinations of elements, but I think this one has a certain twist to be worth a new question: For a pet proejct of mine I've to pre-compute a lot of state to improve the runtime behavior of the application later. One of the steps I struggle with is this: Given N tuples of t...

Generate all combinations of arbitrary alphabet up to arbitrary length

Say I have an array of arbitrary size holding single characters. I want to compute all possible combinations of those characters up to an arbitrary length. So lets say my array is [1, 2, 3]. The user-specified length is 2. Then the possible combinations are [11, 22, 33, 12, 13, 23, 21, 31, 32]. I'm having real trouble finding a suitabl...

Algorithm to select a single, random combination of values?

Say I have y distinct values and I want to select x of them at random. What's an efficient algorithm for doing this? I could just call rand() x times, but the performance would be poor if x, y were large. ...

Cartesian product of several vectors

Hi, similar questions have been asked before but I cant find an exact match to my question. I have 4 vectors each of which hold between 200-500 4 digit integers. The exact number of elements in each vector varies but I could fix it to a specific value. I need to find all possible combinations of the elements in these 4 vectors. eg: ...

How should I go about generating every possible map<char, char> combination from map<char, vector<char> > ?

I am looking to take a map<char, vector<char> > and generate each possible map<char, char> from it. I understand this may use a sizeable amount of memory and take a bit of time. Each map<char, char> needs to contain every letter a-z, and be mapped to a unique a-z character. ie. ak bj cp dy ev fh ga hb ir jq kn li mx nc oo pz qs rl sd t...

What are the Ruby equivalent of Python itertools, esp. combinations/permutations/groupby?

Python's itertools module provides a lots of goodies with respect to processing an iterable/iterator by use of generators. For example, permutations(range(3)) --> 012 021 102 120 201 210 combinations('ABCD', 2) --> AB AC AD BC BD CD [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D What are the equivalent in Ruby? By eq...