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

Recommended Answers

All 14 Replies

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.

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()

commented: Help? There is no help in this post. Just spamming a sig link. -3

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;
}
commented: After 36 posts you still can't figure out how to format code properly? -3

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.