hisoka Posted February 24, 2016 Share Posted February 24, 2016 How to write a Javascript program that find the addends of a number ? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 24, 2016 Share Posted February 24, 2016 Loop starting at 0 or 1 and ending at half the number. Link to comment Share on other sites More sharing options...
hisoka Posted February 24, 2016 Author Share Posted February 24, 2016 (edited) 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 01234567891011121314151617181920 all the numbers that when added to each other give 40 if I am wrong could you correct me ? Edited February 24, 2016 by hisoka Link to comment Share on other sites More sharing options...
davej Posted February 24, 2016 Share Posted February 24, 2016 Good grief. Link to comment Share on other sites More sharing options...
hisoka Posted February 24, 2016 Author Share Posted February 24, 2016 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 More sharing options...
Ingolme Posted February 24, 2016 Share Posted February 24, 2016 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 More sharing options...
justsomeguy Posted February 25, 2016 Share Posted February 25, 2016 The solution for 2 is what I suggested with the loop, but you only printed 1 of them. It should be obvious how to find the second one. The others would use a series of nested loops. Link to comment Share on other sites More sharing options...
davej Posted February 25, 2016 Share Posted February 25, 2016 What does this mean? console.log(tmp = +i); Link to comment Share on other sites More sharing options...
hisoka Posted February 25, 2016 Author Share Posted February 25, 2016 console.log(tmp = +i); used to print all the numbers that the loop engenders Link to comment Share on other sites More sharing options...
hisoka Posted February 25, 2016 Author Share Posted February 25, 2016 "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 Link to comment Share on other sites More sharing options...
justsomeguy Posted February 25, 2016 Share Posted February 25, 2016 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 notYou'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 More sharing options...
Ingolme Posted February 25, 2016 Share Posted February 25, 2016 Perhaps it's possible to do this for an unlimited number of addends using a recursive function, but I would have to do some homework to figure it out. Link to comment Share on other sites More sharing options...
davej Posted February 26, 2016 Share Posted February 26, 2016 "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 More sharing options...
justsomeguy Posted February 26, 2016 Share Posted February 26, 2016 It would make more sense to cap it at a certain number, like only up to 2, 3, 4 etc addends. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now