Dado un entero positivo N . La tarea es encontrar el término N de la serie 3, 7, 19, 55, 163, …..
Ejemplos :
Entrada : N = 5
Salida : 163Entrada : N = 1
Salida : 3
Enfoque : La secuencia se forma usando el siguiente patrón. Para cualquier valor N
TN = 2 * 3 N – 1 + 1
Ilustración:
Entrada: N = 5
Salida: 163
Explicación:
T N = 2 * 3 N – 1 + 1
= 2 * 3 5 – 1 + 1
= 2 * 81 + 1
= 162 + 1
= 163
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return Nth term // of the series int calcNum(int N) { return 2 * pow(3, N - 1) + 1; } // Driver Code int main() { int N = 5; cout << calcNum(N); return 0; }
Java
// Java code to implement the above approach import java.lang.*; public class gfg { /* Function to return the Nth term of the series */ static int calcNum(int N) { return (int)(2*(Math.pow(3,N-1))) + 1 ; } // Driver Code public static void main(String[] args) { int N = 5; System.out.println(calcNum(N)); } } // This code is contributed by Abhishek Thakur
Python3
# Python3 program to implement # the above approach # Function to return Nth term # of the series def calcNum(N): return 2 * (3 ** (N - 1)) + 1 # Driver Code N = 5 print(calcNum(N)) # This code is contributed by gfgking
C#
// C# code to implement the above approach using System; public class gfg { /* Function to return the Nth term of the series */ static int calcNum(int N) { return (int)(2 * (Math.Pow(3, N - 1))) + 1; } // Driver Code public static void Main(string[] args) { int N = 5; Console.WriteLine(calcNum(N)); } } // This code is contributed by Abhishek Thakur
Javascript
<script> // JavaScript code for the above approach // Function to return Nth term // of the series function calcNum(N) { return 2 * Math.pow(3, N - 1) + 1; } // Driver Code let N = 5; document.write(calcNum(N)); // This code is contributed by Potta Lokesh </script>
Producción
163
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por akashjha2671 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA