Dado un número N , la tarea es encontrar el N-ésimo término de la serie donde cada término difiere en 6 y 2 alternativamente.
Ejemplos:
Entrada: N = 6
Salida: 24
Explicación:
El término N es 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24
Entrada: N = 3
Salida: 14
Explicación:
El término N es 0 + 6 + 2 + 6 = 14
Enfoque ingenuo: la idea es iterar desde 1 con un incremento de 6 y 2 alternativamente, hasta llegar al N-ésimo término.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find Nth term void findNthTerm(int N) { int ans = 0; // Iterate from 1 till Nth term for (int i = 0; i < N; i++) { // Check if i is even and // then add 6 if (i % 2 == 0) { ans = ans + 6; } // Else add 2 else { ans = ans + 2; } } // Print ans cout << ans << endl; } // Driver Code int main() { int N = 3; findNthTerm(N); return 0; }
Java
// Java program for the above approach class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans = 0; // Iterate from 1 till Nth term for (int i = 0; i < N; i++) { // Check if i is even and // then add 6 if (i % 2 == 0) { ans = ans + 6; } // Else add 2 else { ans = ans + 2; } } // Print ans System.out.print(ans +"\n"); } // Driver Code public static void main(String[] args) { int N = 3; findNthTerm(N); } } // This code is contributed by PrinciRaj1992
Python3
# Python3 program for the above approach # Function to find Nth term def findNthTerm(N): ans = 0 # Iterate from 1 till Nth term for i in range(N): # Check if i is even and # then add 6 if (i % 2 == 0) : ans = ans + 6 # Else add 2 else : ans = ans + 2 # Print ans print(ans) # Driver Code if __name__=='__main__': N = 3 findNthTerm(N) # This code is contributed by AbhiThakur
C#
// C# program for the above approach using System; class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans = 0; // Iterate from 1 till Nth term for (int i = 0; i < N; i++) { // Check if i is even and // then add 6 if (i % 2 == 0) { ans = ans + 6; } // Else add 2 else { ans = ans + 2; } } // Print ans Console.Write(ans +"\n"); } // Driver Code public static void Main() { int N = 3; findNthTerm(N); } } // This code is contributed by AbhiThakur
Javascript
<script> // JavaScript program for the above approach // Function to find Nth term function findNthTerm(N) { let ans = 0; // Iterate from 1 till Nth term for (let i = 0; i < N; i++) { // Check if i is even and // then add 6 if (i % 2 == 0) { ans = ans + 6; } // Else add 2 else { ans = ans + 2; } } // Print ans document.write(ans + "<br>"); } // Driver Code let N = 3; findNthTerm(N); // This code is contributed by Mayank Tyagi </script>
14
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(N)
Enfoque Eficiente: Podemos encontrar el N-ésimo término usando la siguiente fórmula:
- Si N es Impar: El N-ésimo término viene dado por (N/2 + 1)*6 + (N/2)*2.
- Si N es Par: El N-ésimo término viene dado por (N/2)*6 + (N/2)*2.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find Nth term void findNthTerm(int N) { int ans; // Check if N is even if (N % 2 == 0) { // Formula for n is even ans = (N / 2) * 6 + (N / 2) * 2; } // Check if N is odd else { // Formula for N is odd ans = (N / 2 + 1) * 6 + (N / 2) * 2; } // Print ans cout << ans << endl; } // Driver Code int main() { int N = 3; findNthTerm(N); return 0; }
Java
// Java program for the above approach class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans; // Check if N is even if (N % 2 == 0) { // Formula for n is even ans = (N / 2) * 6 + (N / 2) * 2; } // Check if N is odd else { // Formula for N is odd ans = (N / 2 + 1) * 6 + (N / 2) * 2; } // Print ans System.out.print(ans +"\n"); } // Driver Code public static void main(String[] args) { int N = 3; findNthTerm(N); } } // This code contributed by PrinciRaj1992
Python3
# Python3 program for the above approach # Function to find Nth term def findNthTerm(N): ans = 0; # Check if N is even if (N % 2 == 0): # Formula for n is even ans = (N // 2) * 6 + (N // 2) * 2; # Check if N is odd else: # Formula for N is odd ans = (N // 2 + 1) * 6 + (N // 2) * 2; # Print ans print(ans); # Driver Code if __name__ == '__main__': N = 3; findNthTerm(N); # This code is contributed by Rajput-Ji
C#
// C# program for the above approach using System; class GFG{ // Function to find Nth term static void findNthTerm(int N) { int ans; // Check if N is even if (N % 2 == 0) { // Formula for n is even ans = (N / 2) * 6 + (N / 2) * 2; } // Check if N is odd else { // Formula for N is odd ans = (N / 2 + 1) * 6 + (N / 2) * 2; } // Print ans Console.Write(ans +"\n"); } // Driver Code public static void Main(String[] args) { int N = 3; findNthTerm(N); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // JavaScript program for the above approach // Function to find Nth term function findNthTerm( N) { let ans; // Check if N is even if (N % 2 == 0) { // Formula for n is even ans = parseInt(N / 2) * 6 + parseInt(N / 2) * 2; } // Check if N is odd else { // Formula for N is odd ans = parseInt(N / 2 + 1) * 6 + parseInt(N / 2) * 2; } // Print ans document.write(ans); } // Driver Function // get the value of N let N = 3; // Calculate and print the Nth term findNthTerm(N); // This code is contributed by todaysgaurav </script>
14
Complejidad de tiempo: O(1)
Espacio Auxiliar : O(1) ya que usa variables constantes
Publicación traducida automáticamente
Artículo escrito por rakshitarora y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA