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 = 125Similarly,
N = 6
N = N * N
N = 6 * 6
N = 36 and so on..
Implementation of the above approach is given below:
C++
// 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; } |
Java
// 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 |
Python 3
# 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 = 6print (logicOfSequence(N)) # This code is contributed by # Vishal Gupta |
[/sourcecode]
C#
// 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 |
PHP
<?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. ?> |
Output:
36
Time Complexity : O(1)
Recommended Posts:
- Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
- Program to find sum of elements in a given array
- Program to find largest element in an array
- Program to find GCD or HCF of two numbers
- Program to find LCM of two numbers
- Program to find transpose of a matrix
- Program to find area of a triangle
- Program to find area of a circle
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Find all angles of a given triangle
- Find count of digits in a number that divide the number
- Find a time for which angle between hour and minute hands is given theta
- Find other two sides of a right angle triangle
- Find start and ending index of an element in an unsorted array
- Find the missing number in a sorted array of limited range
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.



