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>
usingnamespace 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.
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:
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?
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>
constint 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);}
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