Dado un entero positivo N , la tarea es encontrar la suma de los primeros N términos de la serie
2, 5, 8, 11, 14..
Ejemplos:
Entrada: N = 5
Salida: 40Entrada : N = 10
Salida : 155
Acercarse:
1er término = 2
2do término = (2 + 3) = 5
3er término = (5 + 3) = 8
4to término = (8 + 3) = 11
.
.
N-ésimo término = (2 + (N – 1) * 3) = 3N – 1
La secuencia se forma usando el siguiente patrón. Para cualquier valor N-
Aquí,
a es el primer término
d es la diferencia común
La solución anterior se puede derivar siguiendo la serie de pasos:
Las series-
2, 5, 8, 11, …., hasta N términos
está en AP con primer término de la serie a = 2 y diferencia común d = 3
La suma de N términos de un AP es
Ilustración:
Entrada: N = 5
Salida: 40
Explicación:
a = 2
d = 3
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <iostream> using namespace std; // Function to return // Nth term of the series int nth(int n, int a1, int d) { return (a1 + (n - 1) * d); } // Function to return sum of // Nth term of the series int sum(int a1, int nterm, int n) { return n * (a1 + nterm) / 2; } // Driver code int main() { // Value of N int N = 5; // First term int a = 2; // Common difference int d = 3; // finding last term int nterm = nth(N, a, d); cout << sum(a, nterm, N) << endl; return 0; }
C
// C program to implement // the above approach #include <stdio.h> // Function to return // Nth term of the series int nth(int n, int a1, int d) { return (a1 + (n - 1) * d); } // Function to return // sum of Nth term of the series int sum(int a1, int nterm, int n) { return n * (a1 + nterm) / 2; } // Driver code int main() { // Value of N int N = 5; // First term int a = 2; // Common difference int d = 3; // Finding last term int nterm = nth(N, a, d); printf("%d", sum(a, nterm, N)); return 0; }
Java
// Java program to implement // the above approach import java.io.*; class GFG { // Driver code public static void main(String[] args) { // Value of N int N = 5; // First term int a = 2; // Common difference int d = 3; // Finding Nth term int nterm = nth(N, a, d); System.out.println(sum(a, nterm, N)); } // Function to return // Nth term of the series public static int nth(int n, int a1, int d) { return (a1 + (n - 1) * d); } // Function to return // sum of Nth term of the series public static int sum(int a1, int nterm, int n) { return n * (a1 + nterm) / 2; } }
Python3
# Python code for the above approach # Function to return # Nth term of the series def nth(n, a1, d): return (a1 + (n - 1) * d); # Function to return sum of # Nth term of the series def sum(a1, nterm, n): return n * (a1 + nterm) / 2; # Driver code # Value of N N = 5; # First term a = 2; # Common difference d = 3; # finding last term nterm = nth(N, a, d); print((int)(sum(a, nterm, N))) # This code is contributed by gfgking
C#
using System; public class GFG { // Function to return // Nth term of the series public static int nth(int n, int a1, int d) { return (a1 + (n - 1) * d); } // Function to return // sum of Nth term of the series public static int sum(int a1, int nterm, int n) { return n * (a1 + nterm) / 2; } static public void Main() { // Code // Value of N int N = 5; // First term int a = 2; // Common difference int d = 3; // Finding Nth term int nterm = nth(N, a, d); Console.Write(sum(a, nterm, N)); } } // This code is contributed by Potta Lokesh
Javascript
<script> // JavaScript code for the above approach // Function to return // Nth term of the series function nth(n, a1, d) { return (a1 + (n - 1) * d); } // Function to return sum of // Nth term of the series function sum(a1, nterm, n) { return n * (a1 + nterm) / 2; } // Driver code // Value of N let N = 5; // First term let a = 2; // Common difference let d = 3; // finding last term let nterm = nth(N, a, d); document.write(sum(a, nterm, N) + '<br>'); // This code is contributed by Potta Lokesh </script>
40
Publicación traducida automáticamente
Artículo escrito por tarakki100 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA