473,549 Members | 2,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Print all combinations of a number N, as a sum of positive integers?

7 New Member
For example:

3=
2 1
1 2
1 1 1

4=
3 1
1 3
1 1 2
1 2 1
2 1 1
1 1 1 1

and so on...


How to write a program in C for such a problem...I can't even seem to figure out the algo?
May 18 '09 #1
15 17928
scruggsy
147 New Member
@vikrantt
Some thoughts:
-for every number c less than n, c + (n - c) = n
-given the above, you can do this recursively. For instance, if 2 + 3 = 5 then the numbers summing to 2 plus the numbers summing to 3 also = 5.
-you only have to calculate sums for numbers from (n/2) through n; below that the sums are the same, just reversed. i.e.
4 + 2 = 6
3 + 3 = 6
2 + 4 = 6 <- you can stop at 3 + 3

Hope this helps.
May 18 '09 #2
vikrantt
7 New Member
okay, i think i did not make it clear. But I do not need to calculate the numbers, rather print them. And here 1 2, is different than 2 1. Or may be we can skip permutation.
May 18 '09 #3
scruggsy
147 New Member
@vikrantt
I'm not clear on what you need, then.
Do you already have an algorithm to compute the numbers, or are the numbers precomputed? That would be the first step before you can worry about printing them.
If you've already got the first step down, are you stuck on how to output the numbers to the screen?
May 18 '09 #4
vikrantt
7 New Member
no....u do not need to count the numbers...i mean its not that u must not...but only thing required is to print them.... counting is not really a concern here....
May 18 '09 #5
donbock
2,426 Recognized Expert Top Contributor
I hear you telling us that you already have a solution for the much harder problem of determining the "additive factors" of a number N and that you only need help printing out that list of numbers after you have generated the list. Is that what you mean?
May 18 '09 #6
JosAH
11,448 Recognized Expert MVP
You mention combinations in your thread title but you mention permutations of an integer number in your post. Maybe you are interested in this link.

kind regards,

Jos
May 18 '09 #7
Bassem
344 Contributor
@JosAH
Wow I was going to suggest a simple algorithm to do that, but you surprised me a lot.
Also I think you (all) didn't agree on the definition of counting, he wants to calculate the numbers then printing them out the screen.
May 23 '09 #8
JosAH
11,448 Recognized Expert MVP
I coughed up a bit of Java code (sorry, no C/C++ compiler on this laptop, I'm sitting outside in my garden and am way too lazy to move in to find one of my other laptops). The following code snippet does what the OP wants:

Expand|Select|Wrap|Line Numbers
  1. public class Partition {
  2.  
  3.     private static void printPartition(int[] p, int n) {
  4.  
  5.         for (int i= 0; i < n; i++)
  6.             System.out.print(p[i]+" ");
  7.         System.out.println();
  8.     }
  9.  
  10.     private static void partition(int[] p, int n, int m, int i) {
  11.  
  12.         if (n == 0)
  13.             printPartition(p, i);
  14.         else
  15.             for (int k= m; k > 0; k--) {
  16.                 p[i]= k;
  17.                 partition(p, n-k, n-k, i+1);
  18.             }
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         partition(new int[6], 6, 6, 0);
  24.     }
  25. }
If you change the recursive call to this:

Expand|Select|Wrap|Line Numbers
  1.                 partition(p, n-k, Math.min(n-k, k), i+1);
  2.  
... you get the combinations of all the partitions.

kind regards,

Jos
May 23 '09 #9
scalpa98
3 New Member
hello i am a teacher in a primary school who try to program in vb as hobbyist. (i com here by googling )
I am interested in a code in vbnet or C# able to find all the partition in 2 or 4 terms of any numbers n. Is you java code able to do that ? If yes, Do you know how to translate it in C# or VB please?
thank you
pascal

I would like to build a software for my young pupils : here is the goal


Tables of numbers:

In a table of numbers, pupils have to find the pairs (vertical or horizontal numbers side by side) whose sum is 10 (for example). or a variant that is finding squares with the numbers (quadruplets?) gives 100 (for example in yellow in the grid).

Objectives: To bring the children to make many calculations to track down peers or square in question.

I want to automate the creation of job as upper. However, mathematically speaking, I wonder if there is an "equation" that would find all the pairs or quadruplets of a number?

The user variables to integrate are:

the size of the grid: LgGrille and HtGrille as integer
the target number: NbCible as integer (or decimal?)
The type of work (pairs / quadruplets) JobType
The amount of peers / quadruplets hidden in the grid: QteJob
The allowed or prohibited overlay solutions possible (as boolean?)

The table of possible pairs or quadruplets calculated and stored as array? ArrayPatterns

Pseudo code:

1. Find all the possible patterns and store them in an array? Calculate the number of possible Patterns: Nbpatterns
2. Show this number in the user interface to choose an intelligent QteJob (If QteJob> Nbpatterns, then it will use several times the same ArrayPatterns.i tem)
3. Place the QteJob ArrayPatterns.i tems randomly on the grid within the non-overlapping, if it is prohibited
4. Finish filling the grid with numbers being careful not to accidentally create new ArrayPatterns.i tem
5. Using the same way to print the job as Maze and magic square
May 24 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1526
by: Bhanu | last post by:
I have a problem in a piece of C Program. I have to print a number of size 19 digit (No decimals). What data type should I use to store and print this no. I have already tried using double, long double but in vain. I also think that it might have to do with our environment setting but do not know what to change. I searched in the new but was...
6
8933
by: shaveta | last post by:
pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5....
7
5549
by: mathon | last post by:
hi, i have the following recursive function: unsigned int sum_odds(unsigned int n) { if(n==1) return 1; else
4
2661
by: nickcleary1 | last post by:
I need to pass 4 integers between two forms but am having trouble passing more than one.I am hoping to use the contructor approach as i need the variables straight away Any help would be great thanks Nick
7
2856
by: kepano | last post by:
pls help me to write a program (in c) such that we input an integer x,where x>0. For x, the program has to convert it into the sum of positive integers (all posible!). for e.g. x =8 1. 8 2. 7 + 1 3. 6 + 2 4. 6 + 1 + 1 5. 5 + 3 6. 5 + 2 + 1 7. 5 + 1 + 1 + 1 8. 4 + 4 9. 4 + 3 + 1
5
18753
by: dmitrey | last post by:
hi all, could you inform how to print binary number? I.e. something like print '%b' % my_number it would be nice would it print exactly 8 binary digits (0-1, with possible start from 0) Thank you in advance, D
0
1269
by: castironpi | last post by:
On May 7, 3:31 pm, Mensanator <mensana...@aol.comwrote: ) for a in range( 10 ) ] ) 00000000 00000001 00000010 00000011 00000100 00000101 00000110
1
25291
by: Sin Jeong-hun | last post by:
It looked like that I can print a number in hexadecimal format with Console.Write, but I couldn't find how to print in binary format. I mean 000...0001 for 1, 000...0010 for 2 and so on. I wrote a dirty code, and it seemed to be working, but it looks like quite inefficient. Is there any .NET built in method or a better way? Below is the...
7
4347
by: tallman | last post by:
Hi all, I am using the mlabwrap module in python for my thesis and I have encountered an error : some times when I call my matlab function from my python code it stops and shows this error " mlabrap.error: Subscript indices must be either real positive integers or logicals" and the line where this error occured:"l=mlab.tetris_learn(summ)" The...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.