Sum of series (n/1) + (n/2) + (n/3) + (n/4) +……. + (n/n)


This this article, we will discuss the different approaches to calculate the sum of the given series.

Problem Statement

We are given a number and our task is to calculate the sum of the series Σ (n / i) for I =0 to i=n. We are given any given value of n where n can be any number less than 10^12 considering on integer division.

For example, if the given input is 10,

The sum of given series can be written as

(10/1) + (10/2) + (10/3) + (10/4) + (10/5) + (10/6) + (10/7) + (10/8) + (10/9) + (10/10) 
= 10 + 5 + 3.33.. + 2.5 + 2 + 1.666… + 1.428 + 1.25 + 1.111… + 1

First of all, let us understand what is integer division −

Integer division is a mathematical operation that involves dividing two integers and obtaining a result that is also an integer. In other words, it gives us the quotient of the division without any fractional or decimal part.

While performing integer division, we truncate or remove any decimal or fractional part of the result. Only the whole number portion of the division is considered as the result.

For example, let's consider dividing 10 by 3 using integer division.

The quotient will be 3, as it represents the whole number part of the division. The fractional part (0.3333...) is removed, and only the integer value is retained.

Let us now consider the brute force approach for the summation of given series.

Approach 1: Brute force approach

In this approach, we initialize a variable sum to 0.

We will use a for loop to iterate from 1 to n. For each iteration, we calculate number / iterator and add it to the sum.

After the loop completes, we return the value of the sum, which represents the sum of the series.

We will use long long data structure to store the value of sum as the size of the sum variable needs to be very small in order to carry the summation of numbers up to 10^12.

Example

The implementation of this approach with C++ is given below.

#include <bits/stdc++.h>
#include<iostream>

// defined ll as long long to be used later for larger values of "number"
#define ll long long 
using namespace std;
ll int summation( ll int number ){
   ll int answer = 0;
int iterator =0;
   for ( iterator = 1; iterator <= number ; iterator++){
      answer += (number / iterator ) ;
   }
   return answer ;
}
int main(){
   ll int number = 35;
   cout << "The sum of the series (n/1) + (n/2) + (n/3) + (n/4) +……. + (n/n) for n = " << number << " is " << summation(number) << endl;
   return 0;
}

Output

The sum of the series (n/1) + (n/2) + (n/3) + (n/4) +……. + (n/n) for n = 35 is 131.

Complexities

The time complexity of the approach given above is O(n) with n = number for which, we have to calculate the series sum. As the value of n can be very high up to the limit 10^12, this approach is good for only small numbers and for significantly large numbers, this code is not preferable.

In simpler terms, the code works fine for small numbers, but as the number gets larger, it will take more time to calculate the sum using this approach. Therefore, for very large numbers, it would be better to consider alternative approaches that have better performance characteristics.

  • Space Complexity − As The is no external space used in the given code, the space complexity is O(1).

Approach 2

Another approach to find the summation of the series Σ (n / i) for I = 0 to i = n. could be using the property that the summation of the series given above is also equal to −

2 * (summation of series (1 + 2 + 3 +… up to sqrt(n)) - (sqrt(n) * sqrt(n))

Hence, we just need to iterate over a loop from 1 to sqrt(n) hence reducing the time complexity of the calculation.

Example

The implementation of this approach with C++ is given below.

#include <bits/stdc++.h>
#include<iostream>

// defined ll as long long to be used later for larger values of "number"
#define ll long long 
using namespace std;
ll int summation( ll int number){
   ll int rt = sqrt(number);
   ll int sum = 0;
   for (int iterator = 1; iterator <= rt ; iterator++)
      sum += number / iterator;	
   sum = 2 * sum - (rt * rt);
   return sum;
}
int main(){
   ll int number = 35;
   cout << "The sum of the series (n/1) + (n/2) + (n/3) + (n/4) +……. + (n/n) for n = " << number << " is " << summation(number) << endl;
   return 0;
}

Output

The sum of the series (n/1) + (n/2) + (n/3) + (n/4) +……. + (n/n) for n = 35 is 131
  • Time complexity − The time complexity of this approach is O(sqrt(n)) with sqrt(n) = square root of the number for which, we have to calculate the sum of the series as we are iterating over the loop for 1 to sqrt(n).

  • Space Complexity − As The is no external space used in the given code, the space complexity is O(1).

Updated on: 05-Oct-2023

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements