integer-partition

How to improve integer partitioning with n,m,k

我们两清 提交于 2021-01-29 14:52:41
问题 my program counts the integer partitions of n , which have k distinct partition elements, each is smaller or equal to m . How can i improve the performance? I already cache immediate results. public long q(int n, int m, int k){ return q1(n,m,k,0,0,new HashMap()); } private long q1(int n, int m, int k, int level, int last, Map<String, Long> cache){ if(n <0){ return 0; } if(level+1 ==k){ if(n >m){ return 0; } else { return 1; } } int first = (level == 0) ? 1 : last+1; long total = 0; for(int i

Finding A List of All Combinations of 6 Numbers That Add up to 10

眉间皱痕 提交于 2020-01-23 12:13:15
问题 So I've seen similar versions of this question asked before (Getting all combinations which sum up to 100 using R) but I'm struggling to find a way to figure out what I need to run specifically. I'm trying to create a list in R of all the different combinations of 6 numbers that add up to 10. However, I want to include 0s and repeats of the same # in the row. So it would look something like this: 10 0 0 0 0 0 9 1 0 0 0 0 8 2 0 0 0 0 I've tried running the following: C = t(restrictedparts(10,6

Finding A List of All Combinations of 6 Numbers That Add up to 10

拥有回忆 提交于 2020-01-23 12:13:09
问题 So I've seen similar versions of this question asked before (Getting all combinations which sum up to 100 using R) but I'm struggling to find a way to figure out what I need to run specifically. I'm trying to create a list in R of all the different combinations of 6 numbers that add up to 10. However, I want to include 0s and repeats of the same # in the row. So it would look something like this: 10 0 0 0 0 0 9 1 0 0 0 0 8 2 0 0 0 0 I've tried running the following: C = t(restrictedparts(10,6

Python Integer Partitioning with given k partitions

拥有回忆 提交于 2019-12-17 15:31:15
问题 I'm trying to find or develop Integer Partitioning code for Python. FYI, Integer Partitioning is representing a given integer n as a sum of integers smaller than n. For example, an integer 5 can be expressed as 4 + 1 = 3 + 2 = 3 + 1 + 1 = 2 + 2 + 1 = 2 + 1 + 1 + 1 = 1 + 1 + 1 + 1 + 1 I've found a number of solutions for this. http://homepages.ed.ac.uk/jkellehe/partitions.php and http://code.activestate.com/recipes/218332-generator-for-integer-partitions/ However, what I really want is to

Generate restricted weak integer compositions (or partitions) of an integer n into k parts in Python

送分小仙女□ 提交于 2019-12-12 19:19:47
问题 (Re-posting, as I did not get any response to my previous post) I am trying to write a Python code to generate weak integer compositions (partitions) of a number 'n' into 'k' parts but with a MINIMUM and MAXIMUM value constraint on each partition (see example given below). Also, the partitions have to be generated in lexicographic order. I have found some related posts but have not been able to implement it. Any help will be appreciated. Example: Possible integer partitions for n=5 in k=3

variable number of dependent nested loops

余生长醉 提交于 2019-12-07 15:05:04
问题 Given two integers n and d , I would like to construct a list of all nonnegative tuples of length d that sum up to n , including all permutations. This is similar to the integer partitioning problem, but the solution is much simpler. For example for d==3 : [ [n-i-j, j, i] for i in range(n+1) for j in range(n-i+1) ] This can be extended to more dimensions quite easily, e.g., d==5 : [ [n-i-j-k-l, l, k, j, i] for i in range(n+1) for j in range(n-i+1) for k in range(n-i-j+1) for l in range(n-i-j

variable number of dependent nested loops

只愿长相守 提交于 2019-12-05 18:37:57
Given two integers n and d , I would like to construct a list of all nonnegative tuples of length d that sum up to n , including all permutations. This is similar to the integer partitioning problem , but the solution is much simpler. For example for d==3 : [ [n-i-j, j, i] for i in range(n+1) for j in range(n-i+1) ] This can be extended to more dimensions quite easily, e.g., d==5 : [ [n-i-j-k-l, l, k, j, i] for i in range(n+1) for j in range(n-i+1) for k in range(n-i-j+1) for l in range(n-i-j-l+1) ] I would now like to make d , i.e., the number of nested loops, a variable, but I'm not sure how

Find the lexicographic order of an integer partition

北城余情 提交于 2019-12-03 09:47:47
问题 For permutations, given N and k , I have a function that finds the k th permutation of N in lexicographic order. Also, given a permutation perm , I have a function that finds the lexicographic index of the permutation among all permutations of N . To do this, I used the "factorial decomposition" as suggested in this answer. Now I want to do the same thing for integer partitions of N . For example, for N=7 , I want to be able to back and forth between the index (left) and the partition (right)

Find the lexicographic order of an integer partition

倾然丶 夕夏残阳落幕 提交于 2019-12-03 00:18:55
For permutations, given N and k , I have a function that finds the k th permutation of N in lexicographic order. Also, given a permutation perm , I have a function that finds the lexicographic index of the permutation among all permutations of N . To do this, I used the "factorial decomposition" as suggested in this answer . Now I want to do the same thing for integer partitions of N . For example, for N=7 , I want to be able to back and forth between the index (left) and the partition (right): 0 ( 7 ) 1 ( 6 1 ) 2 ( 5 2 ) 3 ( 5 1 1 ) 4 ( 4 3 ) 5 ( 4 2 1 ) 6 ( 4 1 1 1 ) 7 ( 3 3 1 ) 8 ( 3 2 2 )

Partition of an Integer + Number of partitions

瘦欲@ 提交于 2019-11-30 08:51:35
问题 A partition of an integer n is a way of writing n as a sum of positive integers. For example, for n=7, a partition is 1+1+5. I need a program that finds all the partitions of an integer 'n' using 'r' integers. For example, all the partitions of n=7 using r=3 integers are 1+1+5 , 1+2+4 , 1+3+3 , 2+2+3 . This is what I have so far: #include <iostream> #include <vector> using namespace std; void print (vector<int>& v, int level){ for(int i=0;i<=level;i++) cout << v[i] << " "; cout << endl; }