473,498 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

distribution algorithm question

Hi,

I am looking for an algorithm that figures out which numbers from a
given set add up to another number. I am sure this has been done
before, but I have no idea how such a calculation is called, which
kind of limits my further searching.

An example. The set contains the numbers 1-2-3-4-5. Now the algorithm
should calculate all possible combinations of these numbers that add
up to 10. Each number can be used more than one time or not at all.

possible solutions are:

10 x 1
5 x 2
3 x 2 + 4
1 + 4 + 5
2 + 3 + 5
1 + 2 + 3 + 4

etc.
In this case it's pretty easy to do it on paper, but when the numbers
become larger (>1000) it would be nice to have a program to do this.
Any suggestions how to do this and/or where to start looking ?
thanks,

- Koen.
Nov 13 '05 #1
5 4196
Koen wrote:
Hi,

I am looking for an algorithm that figures out which numbers from a
given set add up to another number. I am sure this has been done
before, but I have no idea how such a calculation is called, which
kind of limits my further searching. Are you talking addition or multiplication or both?
The above paragraph talks about addition, but the solutions show
multiplication.

An example. The set contains the numbers 1-2-3-4-5. Now the algorithm
should calculate all possible combinations of these numbers that add
up to 10. Each number can be used more than one time or not at all. Again, you state addition, but the example shows multiplication.
What is exact definition of the problem?


possible solutions are:

10 x 1
5 x 2
3 x 2 + 4
1 + 4 + 5
2 + 3 + 5
1 + 2 + 3 + 4

etc. Let us see, the first solution is invalid according to the given rules.
"10 x 1" is not a solution because the number "10" is not a member of
the set [1,2,3,4,5].

According to your problem definition, the first three solutions:
10 x 1
5 x 2
3 x 2 + 4
are all invalid solutions since they involve multiplication.


In this case it's pretty easy to do it on paper, but when the numbers
become larger (>1000) it would be nice to have a program to do this. As I've been thinking about this problem, it is not easy.
The given solution set does not match the problem definition.
The number of solutions involving mulitiplication is not a finitie
set, given the rule that any number may be used more than once and
the rule of multiplication by 1.

Addition, on the other hand, is a finite set, as long a zero is not
within the set. The rule of addition with zero provides an infinite
quantity of terms for a solution.
Any suggestions how to do this and/or where to start looking ? Yes, get your problem definition correct before proceeding.
Start looking at:
combinatorics
recursion
linked list
stack
Identity Theorum for Multiplication
Identity Theorum for Addition
Programming loop constructs.
Greatest Common Factor (GCF)
Lowest Common Denominator (LCD)
logarithms (sp?)
Series expansion

If this is homework, slap your instructor and say "bad assignment"
then ramble off the Identity Theorums and say this is a never
ending waste of computer cycles (the same as an infinite loop).


thanks,

- Koen.


I would first start out with either multiplication or addition and
get them working. The multiplication with addition adds more
complexity to the function (as well as combinations).

Since "Each number can be used more than one time",
how does one know when to stop?
For example,
5 x 2
5 x 2 x 1
5 x 2 x 1 x 1
5 x 2 x 1 x 1 x 1 ...

--
Thomas Matthews
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

Nov 13 '05 #2
"Koen" <kv******@earthlink.net> wrote in message
news:dd*************************@posting.google.co m...
| I am looking for an algorithm that figures out which numbers from a
| given set add up to another number. I am sure this has been done
| before, but I have no idea how such a calculation is called, which
| kind of limits my further searching.
|
| An example. The set contains the numbers 1-2-3-4-5. Now the algorithm
| should calculate all possible combinations of these numbers that add
| up to 10. Each number can be used more than one time or not at all.
.....
| In this case it's pretty easy to do it on paper, but when the numbers
| become larger (>1000) it would be nice to have a program to do this.
.....
| Any suggestions how to do this and/or where to start looking ?

Not really a question about C... but anyway:

A somewhat similar classic is the "knapsack problem".
A classic approach to solve this type of thing is "Dynamic programming".
(NB: this is not about dynamic code generation, 'programming' is
to be taken in a mathematical sense).

Googling for these terms should take you in the right direction.
I hope this helps,
Ivan
--
http://www.post1.com/~ivec
Nov 13 '05 #3


Thomas Matthews wrote:

Koen wrote:
Hi,

I am looking for an algorithm that figures out which numbers from a
given set add up to another number. I am sure this has been done
before, but I have no idea how such a calculation is called, which
kind of limits my further searching.

Are you talking addition or multiplication or both?
The above paragraph talks about addition, but the solutions show
multiplication.

An example. The set contains the numbers 1-2-3-4-5. Now the algorithm
should calculate all possible combinations of these numbers that add
up to 10. Each number can be used more than one time or not at all.

Again, you state addition, but the example shows multiplication.
What is exact definition of the problem?

possible solutions are:

10 x 1
5 x 2
3 x 2 + 4
1 + 4 + 5
2 + 3 + 5
1 + 2 + 3 + 4

etc.

Let us see, the first solution is invalid according to the given rules.
"10 x 1" is not a solution because the number "10" is not a member of
the set [1,2,3,4,5].

According to your problem definition, the first three solutions:
10 x 1
5 x 2
3 x 2 + 4
are all invalid solutions since they involve multiplication.


In this case it's pretty easy to do it on paper, but when the numbers
become larger (>1000) it would be nice to have a program to do this.

As I've been thinking about this problem, it is not easy.
The given solution set does not match the problem definition.
The number of solutions involving mulitiplication is not a finitie
set, given the rule that any number may be used more than once and
the rule of multiplication by 1.

Addition, on the other hand, is a finite set, as long a zero is not
within the set. The rule of addition with zero provides an infinite
quantity of terms for a solution.
Any suggestions how to do this and/or where to start looking ?

Yes, get your problem definition correct before proceeding.
Start looking at:
combinatorics
recursion
linked list
stack
Identity Theorum for Multiplication
Identity Theorum for Addition
Programming loop constructs.
Greatest Common Factor (GCF)
Lowest Common Denominator (LCD)
logarithms (sp?)
Series expansion

If this is homework, slap your instructor and say "bad assignment"
then ramble off the Identity Theorums and say this is a never
ending waste of computer cycles (the same as an infinite loop).


thanks,

- Koen.


I would first start out with either multiplication or addition and
get them working. The multiplication with addition adds more
complexity to the function (as well as combinations).

Since "Each number can be used more than one time",
how does one know when to stop?
For example,
5 x 2
5 x 2 x 1
5 x 2 x 1 x 1
5 x 2 x 1 x 1 x 1 ...

--
Thomas Matthews
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

Aw, come on!
It seems obvious that the original posting
10x1
is just a shorthand for saying "1+1+1+1+1+1+1+1+1+1"
and was not meant to say that multiplication was part of the deal.
--
Fred L. Kleinschmidt
Associate Technical Fellow
Boeing Common User Interface Services
Nov 13 '05 #4
In article <3f********@news.swissonline.ch>,
"Ivan Vecerina" <iv**@myrealbox.com> wrote:
A somewhat similar classic is the "knapsack problem".
A classic approach to solve this type of thing is "Dynamic programming".
(NB: this is not about dynamic code generation, 'programming' is
to be taken in a mathematical sense).

Googling for these terms should take you in the right direction.
I hope this helps,
Ivan

Thanks Ivan for a grown-up response ;) I will look into the knapsack
problem.

- Koen.
Nov 13 '05 #5
In article <Nj**********************@newssvr28.news.prodigy.c om>,
Thomas Matthews <Th**********************@sbcglobal.net> wrote:
Are you talking addition or multiplication or both?
The above paragraph talks about addition, but the solutions show
multiplication. If this is homework, slap your instructor and say "bad assignment"
then ramble off the Identity Theorums and say this is a never
ending waste of computer cycles (the same as an infinite loop).


Sorry Thomas, I don't understand your replies. I am glad another reader
gave a more mature reply and pointed me in the correct direction (my
problem is similar to the 'knapsack problem').
have a nice day,

- Koen.
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7036
by: Michael Peuser | last post by:
Hi, I should like to make a distribution (using Tkinter), with standard DLLs removed. pythonXX.dll is no problem. tcl und tk, which make the mass of mega bytes, cannot be removed because...
3
5195
by: Tan Thuan Seah | last post by:
Hi all, I am looking for a way to generate a random number given the variance of a gaussian distribution(or normal distribution). The mean is 0 but the variance will be a user input. Does C++...
7
2123
by: aegis | last post by:
is there any trickery that can be done with rand() so that it generates a uniformly distributed sequence? Suppose that I want any thing in the range m..n (inclusive) to be generated such that...
2
2088
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
24
6244
by: Raven | last post by:
Hi to all, I need to calculate the hpergeometric distribution: choose(r, x) * choose(b, n-x) p(x; r,b,n) = ----------------------------- choose(r+b, n) choose(r,x) is the binomial...
9
2216
by: Ulterior | last post by:
Hi, everyone, I have a simple problem, which bothers me for some while. I will try to explain it - There is some string, whith different letters in it. Is it possible to analyse this string...
10
6408
by: Verbal Kint | last post by:
DEAR ALL, I just have a very short question. In the FAQ list question 13.20 the following text is mentioned: "These methods all generate numbers with mean 0 and standard deviation 1. (To adjust...
4
2702
by: cr4kb0y | last post by:
hello all: I have to program a function which can produce a Poisson Distribution number,I have no idea about it, anyone who can give some suggestions? thanks!
11
11527
by: Alex | last post by:
Hi everybody, I wonder if it is possible in python to produce random numbers according to a user defined distribution? Unfortunately the random module does not contain the distribution I need...
0
7126
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,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7210
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...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
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...
0
5465
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,...
0
4595
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
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
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 ...

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.