Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)

04 Jul, 2022
Given two integers and . The task is to find the sum of the series 1/a + 2/a2 + 3/a3 + ... + n/an.Examples:  
Input: a = 3, n = 3 Output: 0.6666667 The series is 1/3 + 1/9 + 1/27 which is equal to 0.6666667Input: a = 5, n = 4 Output: 0.31039998  
 
Approach: Run a loop from 1 to n and get the term of the series by calculating term = (i / ai). Sum all the generated terms which is the final answer.Below is the implementation of the above approach: 
C++
Java
Python 3
C#
PHP
Javascript
// 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 seriesfloat 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 codeint 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 program to find the sum of the given seriesimport 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 program to find the sum of# the given seriesimport math # Function to return the# sum of the seriesdef 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 codea = 3; n = 3;     # Print the sum of the seriesprint(getSum(a, n)); # This code is contributed# by Akanksha Rai
// C# program to find the sum// of the given seriesusing System; class GFG{     // Function to return the sum// of the seriespublic 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 codestatic 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 program to find the// sum of the given series // Function to return the// sum of the seriesfunction 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 seriesecho (getSum($a, $n)); // This code is contributed by akt_mit?>
<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
 
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Method: Finding the sum of series without using pow function 
Python3
Javascript
# Python code to print# sum of seriesn = 3; a = 3; s = 0 # iterating for loop n timesfor i in range(1, n + 1):     # finding sum  s = s + (i/(a**i))   # printing the resultprint(s) # this code is contributed by Gangarajula Laxmi
<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
Time complexity: O(nlogn) since a single using for loop and logn for inbuilt pow() function.
Auxiliary Space: O(1)
jit_t
Sach_Code
Akanksha_Rai
princi singh
simranarora5sos
polymatir3j
laxmigangarajula03
vansikasharma1329
lokeshpotta20
Aptitude-Series
Picked
series-sum
Technical Scripter 2018
Java Programs
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array
Iterate through List in Java
How to Iterate HashMap in Java?
Remove first and last character of a string in Java
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays