combinatorics

How to design an algorithm to calculate countdown style maths number puzzle

两盒软妹~` 提交于 2019-11-27 11:35:46
I have always wanted to do this but every time I start thinking about the problem it blows my mind because of its exponential nature. The problem solver I want to be able to understand and code is for the countdown maths problem: Given set of number X1 to X5 calculate how they can be combined using mathematical operations to make Y. You can apply multiplication, division, addition and subtraction. So how does 1,3,7,6,8,3 make 348 ? Answer: (((8 * 7) + 3) -1) *6 = 348 . How to write an algorithm that can solve this problem? Where do you begin when trying to solve a problem like this? What

How to find pair with kth largest sum?

北慕城南 提交于 2019-11-27 11:23:27
Given two sorted arrays of numbers, we want to find the pair with the kth largest possible sum. (A pair is one element from the first array and one element from the second array). For example, with arrays [2, 3, 5, 8, 13] [4, 8, 12, 16] The pairs with largest sums are 13 + 16 = 29 13 + 12 = 25 8 + 16 = 24 13 + 8 = 21 8 + 12 = 20 So the pair with the 4th largest sum is (13, 8). How to find the pair with the kth largest possible sum? Also, what is the fastest algorithm? The arrays are already sorted and sizes M and N. I am already aware of the O(Klogk) solution , using Max-Heap given here . It

PHP take all combinations

谁说胖子不能爱 提交于 2019-11-27 08:58:01
I saw this algorithm that will take numbers or words and find all possible combinations And I'm using it, but it does NOT return all "real" combinations. PHP: <?php require_once 'Math/Combinatorics.php'; $words = array('cat', 'dog', 'fish'); $combinatorics = new Math_Combinatorics; foreach($combinatorics->permutations($words, 2) as $p) { echo join(' ', $p), "\n"; } ?> And it returns: cat dog dog cat cat fish fish cat dog fish fish dog But these are not all real combinations, all real combinations includes these too: cat cat dog dog fish fish And that is what I need, the method to get all real

Generating all combinations with repetition using MATLAB

南笙酒味 提交于 2019-11-27 08:57:31
How do I create all k-combinations with repetitions of a given set (also called k-multicombinations or multisubsets ) using MATLAB? This is similar to the cartesian product, but two rows that only differ by their sorting should be considered the same (e.g. the vectors [1,1,2]=~=[1,2,1] are considered to be the same), so generating the cartesian product and then applying unique(sort(cartesianProduct,2),'rows') should yield the same results. Example: The call nmultichoosek(1:n,k) should generate the following matrix: nmultichoosek(1:3,3) ans = 1 1 1 1 1 2 1 1 3 1 2 2 1 2 3 1 3 3 2 2 2 2 2 3 2 3

Itertools to generate scrambled combinations

心已入冬 提交于 2019-11-27 08:26:26
问题 What I want to do is obtain all combinations and all unique permutations of each combination. The combinations with replacement function only gets me so far: from itertools import combinations_with_replacement as cwr foo = list(cwr('ACGT', n)) ## n is an integer My intuition on how to move forward is to do something like this: import numpy as np from itertools import permutations as perm bar = [] for x in foo: carp = list(perm(x)) for i in range(len(carp)): for j in range(i+1,len(carp)): if

Permute all unique enumerations of a vector in R

吃可爱长大的小学妹 提交于 2019-11-27 08:04:26
I'm trying to find a function that will permute all the unique permutations of a vector, while not counting juxtapositions within subsets of the same element type. For example: dat <- c(1,0,3,4,1,0,0,3,0,4) has factorial(10) > 3628800 possible permutations, but only 10!/(2!*2!*4!*2!) factorial(10)/(factorial(2)*factorial(2)*factorial(2)*factorial(4)) > 18900 unique permutations when ignoring juxtapositions within subsets of the same element type. I can get this by using unique() and the permn() function from the package combinat unique( permn(dat) ) but this is computationally very expensive,

Calculating the Amount of Combinations

懵懂的女人 提交于 2019-11-27 07:26:41
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 in C++, since for instance with n = 52 n! = 8,0658175170943878571660636856404e+67 the number gets way too big even for unsigned __int64 (or unsigned long long ). Is there some workaround to implement the formula without any third-party "bigint" -libraries? Here's an ancient algorithm which is exact and doesn't overflow unless the result is to big for a long long unsigned long long choose

Permutations of binary number by swapping two bits (not lexicographically)

送分小仙女□ 提交于 2019-11-27 06:58:07
问题 I'm looking for an algorithm which computes all permutations of a bitstring of given length ( n ) and amount of bits set ( k ). For example while n=4 and k=2 the algorithm shall output: 1100 1010 1001 0011 0101 0110 I'm aware of Gosper's Hack which generates the needed permutations in lexicographic order. But i need them to be generated in such a manner, that two consecutive permutations differ in only two (or at least a constant number of) bitpositions (like in the above example). Another

all combinations of k elements out of n

让人想犯罪 __ 提交于 2019-11-27 06:55:16
Can somebody provide me a link or pseudocode of a function for finding all combinations of k elements out of n? possibly in STL. I don't need to compute n choose k, I need to list all vectors of numbers of size k. Thanks In C++ given the following routine: template <typename Iterator> inline bool next_combination(const Iterator first, Iterator k, const Iterator last) { /* Credits: Thomas Draper */ if ((first == last) || (first == k) || (last == k)) return false; Iterator itr1 = first; Iterator itr2 = last; ++itr1; if (last == itr1) return false; itr1 = last; --itr1; itr1 = k; --itr2; while

Algorithm to get all the combinations of size n from an array (Java)? [closed]

◇◆丶佛笑我妖孽 提交于 2019-11-27 06:45:18
Right now I'm trying to write a function that takes an array and an integer n, and gives a list of each size n combination (so a list of int arrays). I am able to write it using n nested loops, but this only works for a specific size of subset. I can't figure out how to generalize it to work for any size of combination. I think I need to use recursion? This is the code for all combinations of 3 elements, and I need an algorithm for any number of elements. import java.util.List; import java.util.ArrayList; public class combinatorics{ public static void main(String[] args) { List<int[]> list =