Geek-onacci Number - GeeksforGeeks

30 Apr, 2021
Given four integers A, B, C and N, where A, B, C represents the first three numbers of the geekonacci series, the task is to find the Nth term of the geekonacci series.
The Nth term of the geekonacci series is the sum of its previous three terms in the series i.e., sum of (N – 1)th, (N – 2)th, and (N – 3)th geekonacci numbers.
Examples:
Input: A = 1, B = 3, C = 2, N = 4Output: 6Explanation: The geekonacci series is 1, 3, 2, 6, 11, 19, ......Therefore, the 4th geekonacci number is 6.
Input: A = 1, B = 3, C = 2, N = 6Output: 19
Approach: The given problem can be solved by generating the geekonacci series upto N terms and print the Nth term of the series obtained. Follow the steps below to solve this problem:
Initialize an array arr[] of size N and initialize arr[0] = A, arr[1] = B, and arr[2] = C.
Iterate over the range [3, N – 1] and update the value of each every ith element, i.e. arr[i] as (arr[i – 1] + arr[i – 2] + arr[i – 3]) to get the ith term of the geekonacci series.
After completing the above steps, print the value of arr[N – 1] as the resultant Nth number of the geekonacci series.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
#include <bits/stdc++.h>using namespace std; // Function to calculate the// N-th Geek-onacci Numberint find(int A, int B,                int C, int N){       // Stores the geekonacci series    int arr[N];     // Store the first three    // terms of the series    arr[0] = A;    arr[1] = B;    arr[2] = C;     // Iterate over the range [3, N]    for (int i = 3; i < N; i++) {         // Update the value of arr[i]        // as the sum of previous 3        // terms in the series        arr[i] = arr[i - 1]                 + arr[i - 2]                 + arr[i - 3];    }     // Return the last element    // of arr[] as the N-th term    return arr[N - 1];} // Driver Codeint main(){  int A = 1, B = 3, C = 2, N = 4;  cout<<(find(A, B, C, N));   return 0;} // This code is contributed by mohit kumar 29.
// Java program for the above approach import java.io.*;import java.lang.*;import java.util.*; class GFG {     // Function to calculate the    // N-th Geek-onacci Number    static int find(int A, int B,                    int C, int N)    {        // Stores the geekonacci series        int[] arr = new int[N];         // Store the first three        // terms of the series        arr[0] = A;        arr[1] = B;        arr[2] = C;         // Iterate over the range [3, N]        for (int i = 3; i < N; i++) {             // Update the value of arr[i]            // as the sum of previous 3            // terms in the series            arr[i] = arr[i - 1]                     + arr[i - 2]                     + arr[i - 3];        }         // Return the last element        // of arr[] as the N-th term        return arr[N - 1];    }     // Driver Code    public static void main(String[] args)    {        int A = 1, B = 3, C = 2, N = 4;        System.out.print(find(A, B, C, N));    }}
# Python3 program for the above approach # Function to calculate the# N-th Geek-onacci Numberdef find(A, B, C, N) :       # Stores the geekonacci series    arr = [0] * N     # Store the first three    # terms of the series    arr[0] = A    arr[1] = B    arr[2] = C     # Iterate over the range [3, N]    for i in range(3, N):         # Update the value of arr[i]        # as the sum of previous 3        # terms in the series        arr[i] = (arr[i - 1]                 + arr[i - 2]                 + arr[i - 3])         # Return the last element    # of arr[] as the N-th term    return arr[N - 1] # Driver CodeA = 1B = 3C = 2N = 4 print(find(A, B, C, N)) # This code is contributed by sanjoy_62.
// C# program for the above approachusing System; class GFG{   // Function to calculate the  // N-th Geek-onacci Number  static int find(int A, int B,                  int C, int N)  {    // Stores the geekonacci series    int[] arr = new int[N];     // Store the first three    // terms of the series    arr[0] = A;    arr[1] = B;    arr[2] = C;     // Iterate over the range [3, N]    for (int i = 3; i < N; i++) {       // Update the value of arr[i]      // as the sum of previous 3      // terms in the series      arr[i] = arr[i - 1]        + arr[i - 2]        + arr[i - 3];    }     // Return the last element    // of arr[] as the N-th term    return arr[N - 1];  }   // Driver Code  public static void Main(string[] args)  {    int A = 1, B = 3, C = 2, N = 4;    Console.Write(find(A, B, C, N));  }} // This code is contributed by code_hunt.
<script> // Javascript program for the above approach     // Function to calculate the    // N-th Geek-onacci Number    function find(A, B, C, N)    {        // Stores the geekonacci series        let arr = new Array(N).fill(0);         // Store the first three        // terms of the series        arr[0] = A;        arr[1] = B;        arr[2] = C;         // Iterate over the range [3, N]        for (let i = 3; i < N; i++) {             // Update the value of arr[i]            // as the sum of previous 3            // terms in the series            arr[i] = arr[i - 1]                     + arr[i - 2]                     + arr[i - 3];        }         // Return the last element        // of arr[] as the N-th term        return arr[N - 1];    } // Driver code         let A = 1, B = 3, C = 2, N = 4;    document.write(find(A, B, C, N));     </script>
6
 
Time Complexity: O(N)Auxiliary Space: O(N)
mohit kumar 29
code_hunt
sanjoy_62
chinmoy1997pal
series
Dynamic Programming
Mathematical
Dynamic Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Longest Palindromic Substring | Set 1
Matrix Chain Multiplication | DP-8
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Program to find GCD or HCF of two numbers