Dada una serie matemática como 3, 9, 21, 41, 71… Para un entero n dado, tienes que encontrar el n-ésimo número de esta serie.
Ejemplos:
Input : n = 4 Output : 41 Input : n = 2 Output : 9
Nuestra primera tarea para resolver este problema es descifrar la serie. Si observa más de cerca la serie, para un n-ésimo término general, el valor será (Σn 2 )+(Σn)+1 , donde
- (Σn 2 ) : es la suma de los cuadrados de los primeros n-números naturales .
- (Σn) : es la suma de los primeros n-números naturales .
- 1 es un valor constante simple.
Entonces, para calcular cualquier término n-ésimo de una serie dada, digamos f(n) tenemos:
f(n) = (Σn 2 )+(Σn)+1
= ( ((n*(n+1)*(2n+ 1))/6) + (n*(n+1)/2) + 1
= (n 3 + 3n 2 + 2n + 3 ) /3
C++
// Program to calculate // nth term of a series #include <bits/stdc++.h> using namespace std; // func for calualtion int seriesFunc(int n) { // for summation of square // of first n-natural nos. int sumSquare = (n * (n + 1) * (2 * n + 1)) / 6; // summation of first n natural nos. int sumNatural = (n * (n + 1) / 2); // return result return (sumSquare + sumNatural + 1); } // Driver Code int main() { int n = 8; cout << seriesFunc(n) << endl; n = 13; cout << seriesFunc(13); return 0; }
Java
// Java Program to calculate // nth term of a series import java.io.*; class GFG { // func for calualtion static int seriesFunc(int n) { // for summation of square // of first n-natural nos. int sumSquare = (n * (n + 1) * (2 * n + 1)) / 6; // summation of first n natural nos. int sumNatural = (n * (n + 1) / 2); // return result return (sumSquare + sumNatural + 1); } // Driver Code public static void main(String args[]) { int n = 8; System.out.println(seriesFunc(n)); n = 13; System.out.println(seriesFunc(13)); } } // This code is contributed by Nikita Tiwari.
Python3
# Program to calculate # nth term of a series # func for calualtion def seriesFunc(n): # for summation of square # of first n-natural nos. sumSquare = (n * (n + 1) * (2 * n + 1)) / 6 # summation of first n # natural nos. sumNatural = (n * (n + 1) / 2) # return result return (sumSquare + sumNatural + 1) # Driver Code n = 8 print (int(seriesFunc(n))) n = 13 print (int(seriesFunc(n))) # This is code is contributed by Shreyanshi Arun.
C#
// C# program to calculate // nth term of a series using System; class GFG { // Function for calualtion static int seriesFunc(int n) { // For summation of square // of first n-natural nos. int sumSquare = (n * (n + 1) * (2 * n + 1)) / 6; // summation of first n natural nos. int sumNatural = (n * (n + 1) / 2); // return result return (sumSquare + sumNatural + 1); } // Driver Code public static void Main() { int n = 8; Console.WriteLine(seriesFunc(n)); n = 13; Console.WriteLine(seriesFunc(13)); } } // This code is contributed by vt_m.
PHP
<?php // Program to calculate // nth term of a series // func for calualtion function seriesFunc($n) { // for summation of square // of first n-natural nos. $sumSquare = ($n * ($n + 1) * (2 * $n + 1)) / 6; // summation of first n natural nos. $sumNatural = ($n * ($n + 1) / 2); // return result return ($sumSquare + $sumNatural + 1); } // Driver Code $n = 8; echo(seriesFunc($n) . "\n"); $n = 13; echo(seriesFunc($n) . "\n"); // This code is contributed by Ajit. ?>
Javascript
<script> // JavaScript Program to calculate // nth term of a series // func for calualtion function seriesFunc(n) { // for summation of square // of first n-natural nos. let sumSquare = (n * (n + 1) * (2 * n + 1)) / 6; // summation of first n natural nos. let sumNatural = (n * (n + 1) / 2); // return result return (sumSquare + sumNatural + 1); } // Driver code let n = 8; document.write(seriesFunc(n) + "<br/>"); n = 13; document.write(seriesFunc(13)); </script>
Producción :
241 911
Complejidad temporal : O(1) ya que se realizan operaciones constantes
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA