Given two integers a and n. 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.6666667
Input: 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++
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
float getSum( int a, int n)
{
float sum = 0;
for ( int i = 1; i <= n; ++i)
{
sum += (i / pow (a, i));
}
return sum;
}
int main()
{
int a = 3, n = 3;
cout << (getSum(a, n));
return 0;
}
|
Java
import java.util.Scanner;
public class HelloWorld {
public static float getSum( int a, int n)
{
float sum = 0 ;
for ( int i = 1 ; i <= n; ++i) {
sum += (i / Math.pow(a, i));
}
return sum;
}
public static void main(String[] args)
{
int a = 3 , n = 3 ;
System.out.println(getSum(a, n));
}
}
|
Python 3
import math
def getSum(a, n):
sum = 0 ;
for i in range ( 1 , n + 1 ):
sum + = (i / math. pow (a, i));
return sum ;
a = 3 ; n = 3 ;
print (getSum(a, n));
|
C#
using System;
class GFG
{
public static double getSum( int a, int n)
{
double sum = 0;
for ( int i = 1; i <= n; ++i)
{
sum += (i / Math.Pow(a, i));
}
return sum;
}
static public void Main ()
{
int a = 3, n = 3;
Console.WriteLine(getSum(a, n));
}
}
|
PHP
<?php
function getSum( $a , $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; ++ $i )
{
$sum += ( $i / pow( $a , $i ));
}
return $sum ;
}
$a = 3;
$n = 3;
echo (getSum( $a , $n ));
?>
|
Javascript
<script>
function getSum( a, n) {
let sum = 0;
for (let i = 1; i <= n; ++i) {
sum += (i / Math.pow(a, i));
}
return sum;
}
let a = 3, n = 3;
document.write(getSum(a, n).toFixed(7));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Method: Finding the sum of series without using pow function
C++
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int n = 3, a = 3;
double s = 0;
for ( int i = 1; i < n + 1; i++)
{
s = s + (i / ( pow (a, i)));
}
cout << s;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int n = 3 ,a = 3 ,s = 0 ;
for ( int i = 1 ; i <= n; i++) {
s = s + (i/(a**i))
}
System.out.println(s);
}
}
|
Python3
n = 3 ; a = 3 ; s = 0
for i in range ( 1 , n + 1 ):
s = s + (i / (a * * i))
print (s)
|
C#
using System;
public class GFG
{
static public void Main ()
{
int n = 3, a = 3, s = 0;
for ( int i = 1; i < n + 1; i++)
{
s = s + (i / (Math.Pow(a, i)));
}
Console.WriteLine(s);
}
|
Javascript
<script>
let n = 3, a = 3, s = 0;
for (let i = 1; i < n + 1; i++) {
s = s + (i / (Math.pow(a, i)))
}
document.write(s);
</script>
|
Time complexity: O(nlogn) since a single using for loop and logn for inbuilt pow() function.
Auxiliary Space: O(1)
Method 3: Using the formula for the sum of a geometric series
1. The series 1/a + 2/a^2 + 3/a^3 + … + n/a^n is a geometric series with first term 1/a and common ratio 1/a. The sum of a geometric series is given by the formula:
S = a(1 – r^n)/(1 – r)
where S is the sum of the series, a is the first term, r is the common ratio, and n is the number of terms. Substituting the values for this series, we get:
S = (1/a)(1 – (1/a)^n)/(1 – 1/a)
C++
#include <iostream>
#include <cmath>
using namespace std;
double sum_of_series( double a, int n) {
return (1/a)*(1 - pow ((1/a),n))/(1 - 1/a);
}
int main() {
cout << sum_of_series(4, 5) << endl;
return 0;
}
|
Java
public class Main {
public static double sumOfSeries( double a, int n) {
return ( 1 /a)*( 1 - Math.pow(( 1 /a), n))/( 1 - 1 /a);
}
public static void main(String[] args) {
System.out.println(sumOfSeries( 4 , 5 ));
}
}
|
Python3
def sum_of_series(a, n):
return ( 1 / a) * ( 1 - ( 1 / a) * * n) / ( 1 - 1 / a)
print (sum_of_series( 4 , 5 ))
|
C#
using System;
public class Program
{
public static double SumOfSeries( int a, int n)
{
return (1.0/a)*(1 - Math.Pow(1.0/a, n))/(1 - 1.0/a);
}
public static void Main()
{
Console.WriteLine(SumOfSeries(4, 5));
}
}
|
Javascript
function sum_of_series(a, n) {
return (1/a)*(1 - Math.pow((1/a),n))/(1 - 1/a);
}
console.log(sum_of_series(4, 5));
|
Time complexity: O(n)
Auxiliary Space: O(n)