POTD Solutions | 21 Oct’ 23 | Sum of all divisors from 1 to n Last Updated : 22 Nov, 2023 Improve Improve Like Article Like Save Share Report View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of mathematics but will also help you build up problem-solving skills. POTD 21 Oct Sum of all divisors from 1 to n Solve! Watch! POTD 21 October: Sum of all divisors from 1 to n Given a positive integer N., The task is to find the value of Σi from 1 to N F(i) where function F(i) for the number i is defined as the sum of all divisors of i. Example: Input: 4Output: 15Explanation:F(1) = 1F(2) = 1 + 2 = 3F(3) = 1 + 3 = 4F(4) = 1 + 2 + 4 = 7ans = F(1) + F(2) + F(3) + F(4) = 1 + 3 + 4 + 7 = 15 Input: 5Output: 21 Sum of all divisors from 1 to n using basic mathematical concept: The basic idea is to calculate the sum of divisors for a given number N by iterating through numbers from 1 to N and accumulating the contributions of each divisor to the total sum. For each number ‘i’ in the range, it calculates how many times ‘i’ divides ‘N’ without a remainder and adds this contribution to the sum. The final sum represents the sum of divisors for the input N. Step-by-step approach: A long long variable called “sum” is initialized to zero. This variable will be used to accumulate the sum of divisors. A for loop iterates from 1 to N. This loop is used to find the divisors of the input number N. Inside the loop, the program calculates the contribution of each divisor to the sum. For each value of ‘i’ in the loop, it calculates (N / i) * i. Here’s how this works: N / i gives the number of times ‘i’ divides ‘N‘ without a remainder. It essentially represents how many times ‘i’ appears as a divisor in ‘N‘. Multiplying (N / i) by ‘i’ essentially gives the total value contributed by the divisor ‘i’ to the sum of divisors. The result of (N / i) * i is added to the “sum” variable, effectively accumulating the sum of divisors. Once the loop finishes iterating from 1 to N, the method returns the final value of the “sum,” which represents the sum of divisors of the input number N. Below is the implementation of the above approach: C++ class Solution { public: // Function to calculate the sum of divisors of a given // number N. long long sumOfDivisors(int N) { long long sum = 0; // iterating from 1 to N. for (int i = 1; i <= N; ++i) // calculating and accumulating the sum of // divisors. sum += (N / i) * i; // returning the sum of divisors. return sum; } }; Java public class Solution { // Function to calculate the sum of divisors of a given // number N. public long sumOfDivisors(int N) { long sum = 0; // Iterating from 1 to N. for (int i = 1; i <= N; i++) { // Calculating and accumulating the sum of // divisors. sum += (N / i) * i; } // Returning the sum of divisors. return sum; } } Python3 class Solution: def sumOfDivisors(self, N): sum = 0 # Iterating from 1 to N. for i in range(1, N + 1): # Calculating and accumulating the sum of divisors. sum += (N // i) * i # Returning the sum of divisors. return sum Time complexity: O(N) Auxiliary space: O(1) POTD 21 Oct Sum of all divisors from 1 to n Read Full Solution! Like Article Suggest improvement Next Sum of all divisors from 1 to N | Set 3 Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads POTD Solutions | 30 Oct’ 23 | Sum of XOR of all pairs POTD Solutions | 31 Oct’ 23 | Move all zeroes to end of array POTD Solutions | 23 Oct’ 23 | Maximum Sum Increasing Subsequence POTD Solutions | 15 Oct' 23 | Normal BST to Balanced BST POTD Solutions | 16 Oct’ 23 | Making A Large Island POTD Solutions | 17 Oct’ 23 | Transitive Closure of a Graph POTD Solutions | 18 Oct’ 23 | Eventual Safe States POTD Solutions | 19 Oct’ 23 | Level of Nodes POTD Solutions | 20 Oct’ 23 | Form a number divisible by 3 using array digits POTD Solutions | 22 Oct’ 23 | Number of paths Like V vaibhav_gfg Follow Article Tags : GFG-POTD-Solutions Competitive Programming DSA