GF Series

Photo by freestocks on Unsplash

GF Series

Don't misinterpret the title of today's topic. We're not gonna be talking about a series of girlfriends or something, I know you all like talking and spending time with them but if you can only spare some time for this problem too.

GF Series is a concept from mathematics that is made easier with the beauty of coding. In this concept, the first number is zero by default and the second number is one similarly. It is represented by Tn where n is a natural number.

To calculate the next number in this series, we take help from the last two numbers. Formula: -

Tn \= (Tn-2)(Tn-2) - (Tn-1)

#include<bits/stdc++.h> 
using namespace std; 
class Solution
{
public:
    int solve(int n){
        if(n==1){
            return 0;
        }
        if(n==2){
            return 1;
        }

        return solve(n-2)*solve(n-2) - solve(n-1);
    }
    void gfSeries(int N)
    {
        int arr[N];

        for(int i = 1; i <= N; i++){
            arr[i] = solve(i);
        }

        for(int i = 1; i <= N; i++){
            cout << arr[i] << " ";
        }
    }
};
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int N;
        cin>>N;
        Solution ob;
        ob.gfSeries(N);
    }
    return 0;
}