#Geometric sequence in cpp
54 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
#include <iostream>
int geometricSequence(int firstTerm, int ratio, int n) {
return geometricSequence(firstTerm, ratio, n);
}
int main() {
geometricSequence();
return 0;
}
i have this so far
Can you define the function outside of programming? I.e., do you know the mathematical formula?
thank you in advance!
I haven't solved nor particularly helped you, yet, have I?
no no i meant thank you in advance for helping out
Okay, so do you know how a recursive function generally looks like?
Like, what are the 2 important parts every recursive function has?
that's one of the two important parts: The recursive call
i do not know the 2nd one
But as you can see, right now your function just infinitely calls itself
@trail zealot Has your question been resolved? If so, type !solved :)
Yes. The "end" in a recursive function is the simplest problem to which we already know the solution: The base case.
For example, here is a recursive implementation of the fibonacci sequence:
int fibonacci(unsigned n) {
if (n == 0) return 0; // base case 1
if (n == 1) return 1; // base case 2
int fib_n_minus_1 = fibonacci(n - 1); // recursive call 1
int fib_n_minus_2 = fibonacci(n - 2); // recursive call 2
return fib_n_minus_1 + fib_n_minus_2;
}
// or the same function, but a bit shorter:
int fibonacci(unsigned n) {
if (n <= 1) return n; // base cases
return fibonacci(n - 1) + fibonacci(n - 2); // recursive calls
}
When the input is 0 or 1, then we know the answer already, because these are the most simple cases. Because we don't want to hardcode EVERY answer, we'll use recursion to define fibonacci numbers for higher n. Recursion takes a hard problem like fibonacci(2) and turns it into an easier problem (or in this case 2 easier problems), that we can then immediately solve.
The key trick with recursion is to just assume the recursive call works and only worry about the base cases first.
Btw, the formula for the fibonacci sequence is:
right
basically the sum of the 2 numbers before it
#include <iostream>
int geometricSequence(int firstTerm, int ratio, int n) {
if (n == 1) {
return firstTerm;
}
return geometricSequence(firstTerm, ratio, n);
}
int main() {
int firstTerm, ratio, n;
geometricSequence(firstTerm, ratio, n);
return 0;
}
this is what i have so far
if nth number is 1, retun the first term
#include <iostream>
using namespace std;
int geometricSequence(int firstTerm, int ratio, int n) {
// Base Case
if (n == 1) {
return firstTerm;
}
// Recursive case
return geometricSequence(firstTerm, ratio, n-1);
}
int main() {
geometricSequence(1, 2, 3);
return 0;
}
So with a recursive call the problem always needs to become simpler.
But tbh, looking at the geometric formula rn, I'm a bit stumped as to how we are expected to use recursion here.
The problem is that right now the solution is just:
int geometricSequence(int firstTerm, int ratio, int n) {
return firstTerm * std::exp(ratio, n - 1);
}
```I'm a bit confused, do they want you to essentially implement the exponentiation function?
Ah, quick Wikipedia search clarified things.
You're approaching this with the incorrect formula right now.
So you're approaching this with the function:
a_n = a * r^(n - 1)
```but for recursion you need something in the form:
```matlab
a_n = ... a_n_minus_1 ...
ahh
can you find such a function (you're allowed to look here: https://en.wikipedia.org/wiki/Geometric_progression )?
oops
screenshot is fine
got it
Now, given the fibonacci formula and my example implementation of it, and also given the proper recursive geometric progression formula, try to create the code
#include <iostream>
using namespace std;
int geometricSequence(int firstTerm, int ratio, int n) {
// Base Case
if (n == 1) {
return firstTerm;
}
// Recursive case
return geometricSequence(firstTerm,ratio, n - 1) * ratio;
}
int main() {
return 0;
}
lgtm
*"recursive call", not "recursive case"
using namespace std will import all the symbols from std into the enclosing namespace. This can easily lead to name collisions, as the standard library is filled with common names: get, count, map, array, etc.
A key concern with using namespace std; is not what is imported now but rather what may suddenly be imported in the future.
While using namespace std; is alright for tiny projects, it is important to move away from it as soon as possible. Consider less intrusive alternatives:
// OK: *only* import std::vector
using std::vector;
// OK: namespace alias
namespace chr = std::chrono;
chr::duration x;
What does this mean
looks good to me
How do u turn off the ai shit on clion
Ill look it up
Now I just call the function and give it parameters right
#include <iostream>
int geometricSequence(int firstTerm, int ratio, int n) {
// Base Case
if (n == 1) {
return firstTerm;
}
// Recursive case
return geometricSequence(firstTerm,ratio, n - 1) * ratio;
}
int main() {
std::cout << geometricSequence(2,5,3);
return 0;
}
!solved