473,484 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Create 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 17922
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.item)
3. Place the QteJob ArrayPatterns.items 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.item
5. Using the same way to print the job as Maze and magic square
May 24 '09 #10
JosAH
11,448 Recognized Expert MVP
@scalpa98
Sure, my code can do that: in the printPartition() method bail out if the length n of the partition doesn't equal the wanted length, so insert at line #4:

Expand|Select|Wrap|Line Numbers
  1. if (n != WANTED_LENGTH) return; // bail out
  2.  
The rest of your problem description suggests another solution to your (badly defined) problem though. Start your own thread because this is a completely different problem.

kind regards,

Jos
May 25 '09 #11
vedantsharma
2 New Member
@josah
can this be done iteratively?
without recursion?
May 26 '09 #12
JosAH
11,448 Recognized Expert MVP
@vedantsharma
Sure, but you need more code; why would you want that?

kind regards,

Jos
May 26 '09 #13
scalpa98
3 New Member
Hello again and thanks for your reply Jos. I would like to store all the possible partition of a number in four terms in an array.
In fact , i just wanted to know if a vb version of this code exists in vbnet (it's the language i know a little) Or is it possible to translate it in vb with a tool?
thanks

public class Partition {

private static void printPartition(int[] p, int n) {

for (int i= 0; i < n; i++)
System.out.print(p[i]+" ");
System.out.println();
}

private static void partition(int[] p, int n, int m, int i) {

if (n == 0)
printPartition(p, i);
else
for (int k= m; k > 0; k--) {
p[i]= k;
partition(p, n-k, n-k, i+1);
}
}

public static void main(String[] args) {

partition(new int[6], 6, 6, 0);
}
}
May 26 '09 #14
JosAH
11,448 Recognized Expert MVP
@scalpa98
Storing all (size 4) partitions in an array is easy too: everytime the printPartition() method is called, check n (if it isn't equal to 4 bail out) and store the current partition somewhere.

I don't know any Visual Basic; you can take my code over to the Visual Basic forum and ask if some kind sould wants to translate it for you. I don't know of any Visual Basic translators that translate another language (like Java) to Visual Basic.

kind regards,

Jos
May 27 '09 #15
scalpa98
3 New Member
Thanks a lot, i will follow your advise.
pascal
May 27 '09 #16

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

Similar topics

1
1520
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...
6
8924
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=...
7
5539
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
2656
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 ...
7
2842
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....
5
18749
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) ...
0
1262
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
25263
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...
7
4335
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 "...
0
7100
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,...
0
7135
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...
0
7152
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...
1
4833
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4523
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...
0
3041
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1346
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 ...
1
583
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
228
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.