Geek-onacci Number | Practice | GeeksforGeeks

Geek created a random series and given a name geek-onacci series. Given four integers A, B, C, N. A, B, C represents the first three numbers of geek-onacci series. Find the Nth number of the series. The nth number of geek-onacci series is a sum of the last three numbers (summation of N-1th, N-2th, and N-3th geek-onacci numbers)
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains four space-separated integers A, B, C, and N.

Output: For each test case, print Nth geek-onacci number

Constraints:
1. 1 <= T <= 3
2. 1 <= A, B, C <= 100
3. 4 <= N <= 10

Example:
Input:
3
1 3 2 4
1 3 2 5
1 3 2 6
Output:
6
11
19
0
gkhushi281 month ago
class GFG {public static void main (String[] args) { //code int[][] a = {     {1,3,2,4},     {1,3,2,5},     {1,3,2,6} }; for(int[] arr : a) {     System.out.println(geekonacci(arr)); }}public static int geekonacci(int[] arr){    int a = arr[0],b=arr[1],c=arr[2],n=arr[3];    if(n==4)    return a+b+c;    int temp = a+b+c;    arr[0] = arr[1];    arr[1] = arr[0];    arr[2] = temp;    arr[3] = arr[3]-1;    return geekonacci(arr);}}
can someone tell me what is wrong in this solution?
 
+1
codeman992 months ago
#include <iostream>
using namespace std;

int main() {
	//code
	int t;cin>>t;
	while(t--){
	    int a,b,c,n;cin>>a>>b>>c>>n;
	    int arr[n+1];
	    arr[0]=a,arr[1]=b,arr[2]=c;
	    for(int i=3;i<n;i++){
	        arr[i]=arr[i-1]+arr[i-2]+arr[i-3];
	    }
	    cout<<arr[n-1]<<"\n";
	}
	return 0;
}
+1
nchandel5622 months ago
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++){     int a = sc.nextInt();     int b=sc.nextInt();     int c=sc.nextInt();     int n=sc.nextInt();     int ans = geekonacci(n,a,b,c);     System.out.println(ans); }}static int geekonacci(int n,int a,int b,int c){    if(n==3){        return c;    }    if(n==2){        return b;    }    if(n==1){        return a;    }    return geekonacci(n-1,a,b,c)+geekonacci(n-2,a,b,c)+geekonacci(n-3,a,b,c);}}
0
ariharanv2 months ago
t = int(input().strip())for q in range(t):   a, b, c, n = map(int, input().strip().split())   n -= 3   for _ in range(n):       a, b, c = b, c, a + b + c   print(c)
0
darshitagupta2 months ago
int solve(int a,int b,int c,int n,int count){   if(count==n)return c;      solve(b,c,a+c+b,n,count+1);}
int main() {//codeint t,a,b,c,n;cin>>t;for(int i=0;i<t;i++){cin>>a>>b>>c>>n;cout<<solve(a,b,c,n,3)<<endl;}}
+1
rahulpathak21712 months ago
#include <iostream>using namespace std;int  func(int a,int b,int c,int n,int ct){
   if(ct==n)   {       return c;   }      func(b,c,a+b+c,n,ct+1);}
int main() {int t,a,b,c,n;cin>>t;for(int i=0;i<t;i++){    cin>>a;    cin>>b;    cin>>c;    cin>>n;    cout<<func(a,b,c,n,3)<<endl;        }}
-2
abhinav32543 months ago
Java code
import java.util.*;import java.lang.*;import java.io.*;class GFG{public static void main (String[] args) { //code Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++){     int a = sc.nextInt();     int b = sc.nextInt();     int c = sc.nextInt();     int n = sc.nextInt();     int count = 3;     int k=0;     for(int i1=0;i1<n;i1++){         k = a+b+c;         a = b;         b = c;         c = k;         count = count+1;         if(count==n){             break;         }     }     System.out.println(k); } }}
0
mohankumarit20013 months ago
#include<bits/stdc++.h>
using namespace std;
int main()
 {
    int t;
    cin>>t;
    while(t--){
	    int a,b,c,d,n;
	    cin>>a>>b>>c>>n;
	    n=n-3;
	    while(n--){
	        d = a+b+c;
	        a=b;
	        b=c;
	        c=d;
	    }
        cout<<d<<'\n';
    }
	return 0;
}
0
apurvamalasi20003 months ago
#include <iostream>using namespace std;
int geekonacci(int a, int b, int c, int d ){   if(d==1)  {      return a;  }   if(d==2)  {      return b;  }   if(d==3)  {      return c;  }  return (geekonacci(a, b, c, d-1) + geekonacci(a, b, c, d-2) + geekonacci(a, b, c, d-3));}
int main() {   int t;cin>>t;while(t--){    int a,b,c,d;    cin>>a>>b>>c>>d;    cout<<geekonacci(a, b, c, d)<<endl;}return 0;}
 
 
0
clau2blei3 months ago
PYTHONall test cases passed0.0s runtimeO(n)
from sys import stdin

def GeekOnacci(args):
    A,B,C,N = list(map(int, args))
    N_th = A
    N_th2 = B
    N_th3 = C
    #code
    if N>3:
        for case in range(N-3):
            if case%3 == 0:
                N_th = N_th + N_th2 + N_th3
    
            elif case%3 == 1:
                N_th2 = N_th + N_th2 + N_th3
    
            else:
                N_th3 = N_th + N_th2 + N_th3
    if N%3 == 1:
        print(N_th)
    if N%3 == 2:
        print(N_th2)
    if N%3 == 0:
        print(N_th3)
n = int(stdin.readline())

for test_case in range(n):
    GeekOnacci(stdin.readline().strip().split(' '))
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.