combinatorics

Is there an equivalent of Python's itertools for Java?

I'm searching for a library (preferably generic) that generates iterable combinations and permutations of data contained in collections. Cartesian product would also be nice. The best way of describing what I want would be "itertools for Java". ...

Complex Combinatorial Algorithms

So Wendy's advertises their sandwich as having 256 combinations - meaning there are 8 ingredients you can either have to not have (although I wonder why they would count the combination where you include nothing as valid, but I digress). A generalized approach allows you to multiply the various states of each selection together, which a...

Combinations, Dispositions and Permutations in PHP

What is the most efficient way to generate all the combinations, dispositions and permutations of an array in PHP? ...

Generating n-digit numbers sequenced by sum of individual digits (without recursion)

I'm looking to generate all possible values of n-digit number, in the following order, where the sequence is dictated by the sum of the individual digits. For example, with n = 3: 111 sum = 3 112 sum = 4 121 211 122 sum = 5 212 221 113 131 311 114 sum = 6 141 411 ::: 999 sum = 27 The order within the sum group is ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone's keypad. i.e. for 1,0-> No Letter for 2-> A,B,C So for example, 1230 ADG BDG CDG AEG.... Whats the best solution in c/c++ to this problem? ...

What is the probability that the first 4 bytes of MD5 hash computed from file contents will collide?

This is a combinatorics question with some theory in hashing algorithms required. Let's say the input can be any random sequence of bytes 30 kB to 5 MB of size (I guess that makes quite a few combinations of input values :)) What is the probability that the first 4 bytes (or first n bytes) of a MD5 hash computed from the byte sequence ...

Nth Combination

Is there a direct way of getting the Nth combination of an ordered set of all combinations of nCr? Example: I have four elements: [6, 4, 2, 1]. All the possible combinations by taking three at a time would be: [[6, 4, 2], [6, 4, 1], [6, 2, 1], [4, 2, 1]]. Is there an algorithm that would give me e.g. the 3rd answer, [6, 2, 1], in the o...

Set combination question

Got this as an homework assignment and not really sure where to start! Given the set {1,2,3,4}, you can form six combinations of length two from that set, viz: {1,2},{1,3},{1,4},{2,3},{2,4},{3,4} If I was to choose one of the combinations, ({1,2} for example), how can I tell how many of the others are not disjoint to it? In this cas...

Algorithm to find matching pairs in a list

I will phrase the problem in the precise form that I want below: Given: Two floating point lists N and D of the same length k (k is multiple of 2). It is known that for all i=0,...,k-1, there exists j != i such that D[j]*D[i] == N[i]*N[j]. (I'm using zero-based indexing) Return: A (length k/2) list of pairs (i,j) such that ...

If there are M different boxes and N identical balls

and we need to put these balls into boxes. How many states of the states could there be? This is part of a computer simulation puzzle. I've almost forget all my math knowledges. ...

Calculating the Amount of Combinations

Cheers, I know you can get the amount of combinations with the following formula (without repetition and order is not important): // Choose r from n n! / r!(n - r)! However, I don't know how to implement this with C++, since for instance with n = 52 n! = 8,0658175170943878571660636856404e+67 the number gets way too big even for un...

Project Euler: please help me understand #106

I have solved #103 and #105, but I have a hard time understanding #106, specifically where does the number 25 come from? If we are talking about two disjoint subsets with equal number of elements, then 1-elem vs. 1-elem: there are 4 x 3 = 12 comparisons 2 vs. 2: C(4, 2) = 6 comparisons If we include disjoint subsets with non-equal nu...

Solve "Travelling salesman problem" in linear time

How do I tell a "project lead" to f--k off and learn some computer science when he tells me to do something that amounts to solving a generic Travelling salesman problem in linear time. One of his insights was: 1,000 nodes takes only 1 second then 30,000 should take 30 seconds. I'm not up to teaching combinatorics and Big O to idiots. ...

Combinations of Multiple Vector's Elements Without Repetition

Hi, I have n amount of vectors, say 3, and they have n amount of elements (not necessarily the same amount). I need to choose x amount of combinations between them. Like choose 2 from vectors[n]. Example: std::vector<int> v1(3), v2(5), v3(2); There cannot be combinations from one vector itself, like v1[0] and v1[1]. How can I do this...

What's the difference between combinatorial and numerical problems

Could you please give at least two examples of each. Thanks. ...

Looking for novice book on combinatorics

I am looking for books on the math & computing applications of Combinatorics. Let me know your favorite books on this topic. Thanks. ...

C++: compute a number's complement and its number of possible mismatches

I got a bit stuck with my algorithm and I need some help to solve my problem. I think an example would explain better my problem. Assuming: d = 4 (maximum number of allowed bits in a number, 2^4-1=15). m_max = 1 (maximum number of allowed bits mismatches). kappa = (maximum number of elements to find for a given d and m, where m ...

Simple number pattern to store combinations as a unique sum

This is a math problem, but I'm sure this must come up in some programming scenarios, at least I hope so, and I was wondering if there was a name for this type of situation: Suppose I have 7 items in a series. For the sake of this example, let's use days of the week. I would like a user to submit which days of the week they plan to come...

c++ m bits permutations of a number

Hi, I am searching for a function that get as an input a number x (assuming 15), number of bits d (4) and number of permutations m (2). The output of the function will be all the numbers that are m bit's permutations from the given number x at a d length bits. For the given numbers, (x = 15, d = 4 and m = 2) we get 6=\binom{4}{2}differ...

Function to determine number of unordered combinations with non-unqiue choices

I'm trying to determine the function for determining the number of unordered combinations with non-unique choices. Given: n = number of unique symbols to select from r = number of choices Example... for n=3, r=3, the result would be: (edit: added missing values pointed out by Dav) 000 001 002 011 012 022 111 112 122 222 I know th...