Find n-th term of series 1, 4, 27, 16, 125, 36, 343 …….

Given a number n, find the n-th term in the series 1, 4, 27, 16, 125, 36, 343……..

Examples:

Input : 5
Output : 125

Input : 6
Output : 36

Naive approach : In this Sequence, if the Nth term is even then the Nth term is square of N else Nth term is a cube of N.

N = 5
In this case, N is odd
N = N * N * N
N = 5 * 5 * 5
N = 125

Similarly,
N = 6
N = N * N
N = 6 * 6
N = 36 and so on..

Implementation of the above approach is given below:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to generate first 'n' terms
// of Logic sequence
#include <bits/stdc++.h>
using namespace std;
  
// Function to generate a fixed \number
int logicOfSequence(int N)
{
    if (N % 2 == 0) 
        N = N * N;    
    else 
        N = N * N * N;   
    return N;
}
  
// Driver Method
int main()
{
    int N = 6;
    cout << logicOfSequence(N) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA code to generate first 
// 'n' terms of Logic sequence
  
class GFG {
  
// Function to generate
// a fixed \number
public static int logicOfSequence(int N)
{
    if (N % 2 == 0
        N = N * N; 
    else
        N = N * N * N; 
    return N;
}
  
// Driver Method
public static void main(String args[])
{
    int N = 6;
    System.out.println(logicOfSequence(N));
}
}
  
// This code is contributed by Jaideep Pyne

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to generate first 
# 'n' terms of Logic sequence
  
# Function to generate a fixed
# number
def logicOfSequence(N):
    if(N % 2 == 0):
        N = N * N
    else:
        N = N * N * N
    return N
  
N = 6
print (logicOfSequence(N))
  
# This code is contributed by
# Vishal Gupta

chevron_right


[/sourcecode]

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to generate first 
// 'n' terms of Logic sequence
using System; 
  
class GFG {
  
// Function to generate
// a fixed number
public static int logicOfSequence(int N)
{
    if (N % 2 == 0) 
        N = N * N; 
    else
        N = N * N * N; 
    return N;
}
  
// Driver Method
public static void Main()
{
    int N = 6;
    Console.Write(logicOfSequence(N));
}
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to generate first 'n' terms
// of Logic sequence
  
// Function to generate a fixed number
function logicOfSequence($N)
{
    if ($N % 2 == 0) 
        $N = $N * $N
    else
        $N = $N * $N * $N
    return $N;
}
  
    // Driver Code
    $N = 6;
    echo logicOfSequence($N);
  
// This code is contributed by nitin mittal.
?>

chevron_right



Output:

36

Time Complexity : O(1)



My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.