Dada una serie y un número n, la tarea es encontrar la suma de sus primeros n términos. A continuación se muestra la serie:
2, 5, 13, 35, 97, …
Ejemplos:
Input: N = 2 Output: 7 The sum of first 2 terms of Series is 2 + 5 = 7 Input: N = 4 Output: 55 The sum of first 4 terms of Series is 2 + 5 + 13 + 35 = 55
Enfoque: De esta serie dada encontramos que es la suma de la serie Two GP con proporciones comunes 2, 3 .
Sn = 2 + 5 + 13 + 35 + 97 … + hasta el enésimo término
Sn = (2^0 + 3^ 0) + (2^1 + 3^1) + (2^2 + 3^2) + (2 ^3 + 3^3)+ (2^4 + 3^4) …… + hasta el enésimo término
Sn = (2^0 + 2^1 + 2^2 + 2^3 + 2^4 … + hasta el enésimo término ) + ( 3^0 + 3^1 + 3^2 + 3^3 …… + hasta el enésimo término )
Ya que, Sabemos que la suma de n términos del PG viene dada por la siguiente fórmula:
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for finding the sum // of first N terms of the series. #include <bits/stdc++.h> using namespace std; // CalculateSum function returns the final sum int calculateSum(int n) { // r1 and r2 are common ratios // of 1st and 2nd series int r1 = 2, r2 = 3; // a1 and a2 are common first terms // of 1st and 2nd series int a1 = 1, a2 = 1; return a1 * (pow(r1, n) - 1) / (r1 - 1) + a2 * (pow(r2, n) - 1) / (r2 - 1); } // Driver code int main() { int n = 4; // function calling for 4 terms cout << "Sum = " << calculateSum(n) << endl; return 0; }
Java
//Java program for finding the sum //of first N terms of the series. public class GFG { //CalculateSum function returns the final sum static int calculateSum(int n) { // r1 and r2 are common ratios // of 1st and 2nd series int r1 = 2, r2 = 3; // a1 and a2 are common first terms // of 1st and 2nd series int a1 = 1, a2 = 1; return (int)(a1 * (Math.pow(r1, n) - 1) / (r1 - 1) + a2 * (Math.pow(r2, n) - 1) / (r2 - 1)); } //Driver code public static void main(String[] args) { int n = 4; // function calling for 4 terms System.out.println("Sum = " +calculateSum(n)); } }
Python 3
# Python 3 program for finding the sum # of first N terms of the series. # from math import everything from math import * # CalculateSum function returns the final sum def calculateSum(n) : # r1 and r2 are common ratios # of 1st and 2nd series r1, r2 = 2, 3 # a1 and a2 are common first terms # of 1st and 2nd series a1, a2 = 1, 1 return (a1 * (pow(r1, n) - 1) // (r1 - 1) + a2 * (pow(r2, n) - 1) // (r2 - 1)) # Driver Code if __name__ == "__main__" : n = 4 # function calling for 4 terms print("SUM = ",calculateSum(n)) # This code is contributed by ANKITRAI1
C#
// C# program for finding the sum // of first N terms of the series. using System; class GFG { // CalculateSum function // returns the final sum static int calculateSum(int n) { // r1 and r2 are common ratios // of 1st and 2nd series int r1 = 2, r2 = 3; // a1 and a2 are common first // terms of 1st and 2nd series int a1 = 1, a2 = 1; return (int)(a1 * (Math.Pow(r1, n) - 1) / (r1 - 1) + a2 * (Math.Pow(r2, n) - 1) / (r2 - 1)); } // Driver code static public void Main () { int n = 4; // function calling for 4 terms Console.Write("Sum = " + calculateSum(n)); } } // This code is contributed by Raj
PHP
<?php // PHP program for finding the sum // of first N terms of the series. // CalculateSum function returns // the final sum function calculateSum($n) { // r1 and r2 are common ratios // of 1st and 2nd series $r1 = 2; $r2 = 3; // a1 and a2 are common first // terms of 1st and 2nd series $a1 = 1; $a2 = 1; return $a1 * (pow($r1, $n) - 1) / ($r1 - 1) + $a2 * (pow($r2, $n) - 1) / ($r2 - 1); } // Driver code $n = 4; // function calling for 4 terms echo "Sum = ", calculateSum($n); // This code is contributed by ash264 ?>
Javascript
<script> //javascript program for finding the sum //of first N terms of the series. //CalculateSum function returns the final sum function calculateSum(n) { // r1 and r2 are common ratios // of 1st and 2nd series var r1 = 2, r2 = 3; // a1 and a2 are common first terms // of 1st and 2nd series var a1 = 1, a2 = 1; return parseInt((a1 * (Math.pow(r1, n) - 1) / (r1 - 1) + a2 * (Math.pow(r2, n) - 1) / (r2 - 1))); } //Driver code var n = 4; // function calling for 4 terms document.write("Sum = " +calculateSum(n)); // This code contributed by shikhasingrajput </script>
Sum = 55
Publicación traducida automáticamente
Artículo escrito por SURENDRA_GANGWAR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA