LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-22-2006, 01:04 PM   #1
cdog
Member
 
Registered: Dec 2005
Posts: 65

Rep: Reputation: 15
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.
 
Old 01-22-2006, 04:09 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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"?
 
Old 01-23-2006, 04:19 AM   #3
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
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?
 
Old 01-23-2006, 09:05 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc: recursive function quirk Disruptor Linux - Software 4 09-23-2014 06:45 AM
recursive execution of function in subdirectories Melsync Programming 3 01-17-2006 07:13 AM
using return in recursive function hubabuba Programming 9 10-10-2005 10:59 AM
error recursive function in c shams Programming 3 07-28-2004 09:44 PM
Recursive Function TriggerJ Programming 4 02-02-2004 09:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration