Dados dos enteros y . La tarea es encontrar la suma de la serie 1/a + 2/a 2 + 3/a 3 + … + n/a n .
Ejemplos:
Entrada: a = 3, n = 3
Salida: 0,6666667
La serie es 1/3 + 1/9 + 1/27 que es
igual a 0,6666667
Entrada: a = 5, n = 4
Salida: 0,31039998
Enfoque: ejecute un ciclo de 1 a n y obtenga el término de la serie calculando término = (i / a i ) . Sume todos los términos generados, que es la respuesta final.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the sum of // the given series #include <stdio.h> #include <math.h> #include <iostream> using namespace std; // Function to return the // sum of the series float getSum(int a, int n) { // variable to store the answer float sum = 0; for (int i = 1; i <= n; ++i) { // Math.pow(x, y) returns x^y sum += (i / pow(a, i)); } return sum; } // Driver code int main() { int a = 3, n = 3; // Print the sum of the series cout << (getSum(a, n)); return 0; } // This code is contributed // by Sach_Code
Java
// Java program to find the sum of the given series import java.util.Scanner; public class HelloWorld { // Function to return the sum of the series public static float getSum(int a, int n) { // variable to store the answer float sum = 0; for (int i = 1; i <= n; ++i) { // Math.pow(x, y) returns x^y sum += (i / Math.pow(a, i)); } return sum; } // Driver code public static void main(String[] args) { int a = 3, n = 3; // Print the sum of the series System.out.println(getSum(a, n)); } }
Python 3
# Python 3 program to find the sum of # the given series import math # Function to return the # sum of the series def getSum(a, n): # variable to store the answer sum = 0; for i in range (1, n + 1): # Math.pow(x, y) returns x^y sum += (i / math.pow(a, i)); return sum; # Driver code a = 3; n = 3; # Print the sum of the series print(getSum(a, n)); # This code is contributed # by Akanksha Rai
C#
// C# program to find the sum // of the given series using System; class GFG { // Function to return the sum // of the series public static double getSum(int a, int n) { // variable to store the answer double sum = 0; for (int i = 1; i <= n; ++i) { // Math.pow(x, y) returns x^y sum += (i / Math.Pow(a, i)); } return sum; } // Driver code static public void Main () { int a = 3, n = 3; // Print the sum of the series Console.WriteLine(getSum(a, n)); } } // This code is contributed by jit_t
PHP
<?php // PHP program to find the // sum of the given series // Function to return the // sum of the series function getSum($a, $n) { // variable to store the answer $sum = 0; for ($i = 1; $i <= $n; ++$i) { // Math.pow(x, y) returns x^y $sum += ($i / pow($a, $i)); } return $sum; } // Driver code $a = 3; $n = 3; // Print the sum of the series echo (getSum($a, $n)); // This code is contributed by akt_mit ?>
Javascript
<script> // Javascript program to find the sum of the given series // Function to return the sum of the series function getSum( a, n) { // variable to store the answer let sum = 0; for (let i = 1; i <= n; ++i) { // Math.pow(x, y) returns x^y sum += (i / Math.pow(a, i)); } return sum; } // Driver code let a = 3, n = 3; // Print the sum of the series document.write(getSum(a, n).toFixed(7)); // This code contributed by Princi Singh </script>
0.6666667
Complejidad de tiempo: O (nlogn)
Espacio Auxiliar: O(1)
Método: encontrar la suma de series sin usar la función pow
Python3
# Python code to print # sum of series n = 3; a = 3; s = 0 # iterating for loop n times for i in range(1, n + 1): # finding sum s = s + (i/(a**i)) # printing the result print(s) # this code is contributed by Gangarajula Laxmi
Javascript
<script> // JavaScript code for the above approach //sum of series let n = 3, a = 3, s = 0; // iterating for loop n times for (let i = 1; i < n + 1; i++) { // finding sum s = s + (i / (Math.pow(a, i))) } // printing the result document.write(s); // This code is contributed by Potta Lokesh </script>
0.6666666666666667
Complejidad de tiempo : O (nlogn) desde un solo uso para loop y logn para la función pow() incorporada.
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por algorithmic_demon y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA