Jump to content

addends of a number


hisoka

Recommended Posts

this is what I get so far :

 

for example if I want the addends of 40 then :

 

for( var i = 0 ; i<=20 ; i++)

{
console.log(tmp = +i);
}

 

and I get

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

all the numbers that when added to each other give 40

 

if I am wrong could you correct me ?

Edited by hisoka
Link to comment
Share on other sites

now more difficult . I would like to write a program that gives me the different combinations by the number of addends which when add together gives a number . for example I put 25 as a number then the program generates to me the different combinations of addends like this

 

1+24 = 25

2+23=25 2 combinations of addends

5+20= 25

 

1+1+23=25

4+2+21=25 3 combinations of addends

7+3+15=25

 

7+9+6+3=25

4+10+5+6=25 4 combinations of addends

8+12+1+4=25

 

2+3+7+8+5=25 5 combinations of addends

9+5+4+6+1=25

 

5+6+4+3+2+1+4=25 6 combinations of addends

 

any hint on how to do it ?

Link to comment
Share on other sites

This is more of a math problem than a programming one. Once you've figured out the mathematical solution, the programming one will be easy to implement.

 

Is it a requirement that none of the addends are identical?

Link to comment
Share on other sites

console.log(tmp = +i);

 

used to print all the numbers that the loop engenders

I think the point of the question was why are you assigning it to the variable tmp, and why are you explicitly making it a positive integer when it should already be one? What's wrong with console.log(i)?

 

the addends can be identical or not

You're looking at a series of nested loops, then. The first step is to do the loop for 2 addends and then go from there.
Link to comment
Share on other sites

"Is it a requirement that none of the addends are identical?"

 

the addends can be identical or not

 

for example addends of 10 can be 5+5 = 10 or 6+4=10

 

Okay, but then you have to consider...

1+1+1+1+1+1+1+1+1+1 = 10

...and really for any positive integer the addends would simply be the set of all smaller positive integers.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...