How do I write a a recursive function that counts the number of sequences that sum up to that number (user input)?

7
Contributors
14
Replies
15
Views
7 Years
Discussion Span
Last Post by frogboy77
Featured Replies
• 1

Also, just for giggle, I calculated that this function : [B]1/2n^2-1/2n+1[/B] gives the number of sequence that sums to a number n. Try it out and see if its correct. I give no guarantees.

What do you mean "the number of sequences"? Please give an example number and explain what you would expect the result to be. Did you give it a try? You'll get much better replies if you ask a specific question about a problem you're having rather than just asking for the whole algorithm.

Like say the number is 6 for example...

Then the number of sequences that sum up to 6 is 11 (including 6 itself). This is shown clearly below:

6
5+1
4+1+1
3+1+1+1
2+1+1+1+1
1+1+1+1+1+1
2+2+1+1
3+2+1
4+2
2+2+2
3+3

I think he means for example :

5 = 1 + 1 + 1 + 1 + 1
5 = 1 + 1 + 1 + 2
5 = 1 + 1 + 3
5 = 1 + 4
5 = 5
5 = 4 + 1
5 = 3 + 1 + 1
....

EDIT: too late.

Edited by firstPerson: n/a

No the number 5 has 11 series that adds up to 5, right?

5 = 1 + 1 + 1 + 1 + 1
5 = 1 + 1 + 1 + 2
5 = 1 + 1 + 3
5 = 1 + 4
5 = 5
5 = 4 + 1
5 = 3 + 2
5 = 3 + 1 + 1
5 = 2 + 3
5 = 2 + 2 + 1
5 = 2 + 1 + 1 + 1

looking above you can see that there is some sort of recursive relation. You just need to think about it a little more.

Also, just for giggle, I calculated that this function : 1/2n^2-1/2n+1 gives the number of sequence that sums to a number n. Try it out and see if its correct. I give no guarantees.

I give some help please check the example
def Max(li):
nums = li
biggest=0
for num in nums:
if num > biggest:
biggest = num
return biggest

def main():
li = eval(input("please enter a list of numbers: "))
print("The biggest number is: ", Max(li))
main()

Help? There is no help in this post. Just spamming a sig link.

I have mostly everything figured out except the loop. I can't seem to get it quite right.

``````#include <iostream>

using namespace std;

int partition(int n, int max)
{

if(n==0)
return(0);
int ret = 0;
if(n<=max)
ret=1;
for()
{

}
return(ret);
}

int main()
{

cout << "Enter a number: " << endl;
cin >> n >> endl;
return 0;
}``````
After 36 posts you still can't figure out how to format code properly?

where's the recursion?

p.s is this a euler question?

I don't know how to do the loop. I'm not positive how to make it sum up the sequences without repeating. That is what I'm asking for help with

So I changed some things around and I can run the program successfully but after the user inputs the number nothing happens. I'm really confused about what is happening or not happening.

> I calculated that this function gives the number of sequence that sums to a number n

I seriously doubt that.

> I calculated that this function gives the number of sequence that sums to a number n

I seriously doubt that.

Yea your right, I calculated the mapping of each n wrong. Tried to do it with the correct value and didn't come with a definite answer, although I got an approximation. Anyways. Thanks for the correction.

@kuchick32 post your code as it is now so we can take a look at the problem

This is actually a very difficult problem. As far as i am aware it was solved by the prolific mathematician Leonard Euler. It relates to pentagonal numbers(don't know why). I have the code for your problem if you want it, but i will not post it now in case you want to get this thing by yourself. I will pm it if you ask.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.