Mathematical Function Help

I'm a beginner coder (it's been a few months since I've written any however).

I'm studying numbers and I'm just messing around with sequences of numbers and their properties, and I've decided I should be able to write basic programs that will generate long lists of them for me to make it quicker for me to jump in and study the numbers.

The basic formula is start with 1, add the next two numbers. Take the answer, add the next 2 numbers, etc etc...I know that I need to make a function to do the mathematics, and then I should have to call that function recursively to add the numbers, and put some condition like if the answer becomes greater than a hundred thousand then pause the system or whatever I want to do...can someone tell me how or write how the code should be to get this working? It's just confusing the hell out of me. I'm good enough at coding to where once I see how it's supposed to be written I'll be able to understand what it all means and how to adapt it to further mathematical functions I want to work with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>

using namespace std;

int main()
{
        int n = 1;
        int x = 2;
        int y = 3;
        int answer = n+x+y;

        int z = answer+(answer+1)+(answer+2);

        cout << answer << endl;
        cout << z << endl;

    return 0;
}
If I'm reading this right, you start with: 1, 2, 3.
You sum those numbers: 6.
Then you have: 6, 7, 8.
You sum those numbers: 21.
Then you have: 21, 22, 23.

and so on?

dont clearly understand what you want
You may want to end up having different conditions for stopping. Here's how I would get started with just one stopping condition. You can add on more later:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

int GetAnswer(int answer_arg)
{

int x = answer_arg + 1;
int y = answer_arg + 2;
return answer_arg + x + y;
}

int main ()
{
int answer = 0;
int StoppingCondition;
cout << "Enter stopping condition: (1 = Answer exceeds max, 2 = (some other condition),... )\n";
cin >> StoppingCondition;

if( StoppingCondition == 1 )
{
int StoppingNumber;
cout << "Enter number to stop at: \n";
cin >> StoppingNumber;

while( answer < StoppingNumber )
{
answer = GetAnswer(answer);
}
cout << "answer = " << answer << "\n";
}
return 0;
}

That should work. Let me know if it doesn't.


Last edited on
didn't clearly understand what you want. But if Yay295 said is what you want, then this code should do the job.
Also, u said you were studying sequences so i don't think its the sum of a series that you need but the whole series laid out for you to study right?

You also made mention of recursion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

void sum(int,int );
int main()
{
    int key = 1;
    int upper = 0;

    cout<<"Enter upper limit: ";
    cin>>upper;

    sum(key,upper);
    cin.ignore();
}
void sum(int key,int upper)
{
    int a,b,c;
    if(key < upper)
    {
        a = key;
        b = key + 1;
        c = key + 2;

        cout<<a<<setw(3)<<b<<setw(3)<<c<<endl<<endl;

        sum(a+b+c,upper);
    }
}
Let's stop thinking code for a minute and think math. If I'm right, the equation is
sum = n + n+1 + n+2
We can factor out an n and make it
sum = n * ( 1 + 1 + 1 ) + 1 + 2
or
sum = 3*n + 3
or
sum = 3 * ( n + 1 )

So then we can just do this:
1
2
3
4
5
6
7
8
9
#include <iostream>

const int START = 1, MAX = 1000000;

int main ( )
{
    for ( int i = START; i < MAX; ++i *= 3 )
        std::cout << i << '\n';
}



edit: I decided to golf it. 74 70 characters:
1
2
#include<cstdio>
int main(){for(int i=1;i>0;++i*=3)printf("%i\n",i);}
Last edited on
Thank you all for the replies, especially Yay295... I can't believe a for loop didn't come to mind with something like this, and then just ending it when I get above a certain value...I'm so feeble minded sometimes. Thank you a bunch this is absolutely perfect. If I ever publish a paper dealing with some interesting maths patterns I'll reference you in the citations ;P
Topic archived. No new replies allowed.