Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Permutations & recursive Function
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 01-22-2006, 12:04 PM #1 cdog Member   Registered: Dec 2005 Posts: 65 Rep: Permutations & recursive Function let's say I have the number 10 and I want to represent it by adding 1 and 2. For example: 10=2+2+2+2+1+1 or 10=1+1+2+2+2+2..... How one can put the mathematical formula of permutations (1 and 2) in a recursive function the inputs the number (10) and returns the number of permutations.
 01-22-2006, 03:09 PM #2 paulsm4 LQ Guru   Registered: Mar 2004 Distribution: SusE 8.2 Posts: 5,863 Blog Entries: 1 Rep: It's as simple as: a) deciding what programming language you're going to code it in b) coding it ;-) Seriously - what language do you want to use? How far have you gotten formulating a solution? And where, specifically, are you "stuck"?
01-23-2006, 03:19 AM   #3
cdog
Member

Registered: Dec 2005
Posts: 65

Original Poster
Rep:
Quote:
 Originally Posted by paulsm4 It's as simple as: a) deciding what programming language you're going to code it in b) coding it ;-) Seriously - what language do you want to use? How far have you gotten formulating a solution? And where, specifically, are you "stuck"?
Language: C++
I'm ""stuck"" in writting the function, I mean I have the formula: let's say 10=2+2+2+1+1+1+1 the permutations are: 35. But then again, 10=2+1+1+1.... so you see? I'm stuck here. I though of calling the function with (n-2), but this way I will get the results for 8, 6, ... Any idea?

 01-23-2006, 08:05 AM #4 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: Well the first thing you need with recursive functions is the stopping criteria. I guess when the total is ten would be a good one. So try by starting to create a recursive function that adds two on each call and stops when the total is greater than or equal to your stopping value. Next refine it have two recursive calls one that adds two the other that adds one. Finally fix the code to stop totals of eleven appearing. I hope that gives you something to think about. Remember programming is about breaking a problem down into sub problems that you can manage. graeme.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Disruptor Linux - Software 4 09-23-2014 05:45 AM Melsync Programming 3 01-17-2006 06:13 AM hubabuba Programming 9 10-10-2005 09:59 AM shams Programming 3 07-28-2004 08:44 PM TriggerJ Programming 4 02-02-2004 08:58 PM

LinuxQuestions.org

All times are GMT -5. The time now is 04:11 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -