> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2023/10-2023-oct-20/21-sum-of-all-divisors-from-1-to-n.md).

# 21. Sum of all divisors from 1 to n

The problem can be found at the following link: [Question Link](https://practice.geeksforgeeks.org/problems/sum-of-all-divisors-from-1-to-n4738/1)

![](https://badgen.net/badge/Level/Easy/green)

## My Approach

By obsevation, you will notice that each number appears a total of (N/i) times.

To find the sum of all divisors from 1 to N,

* I iterate through numbers from 1 to N and add up the contributions from each number by `((N/i)*i)`

## Explain with example

Let's take N = 6 as an example.

* For i = 1, it is a divisor of 1, 2, 3, 4, 5, and 6, so it contributes 1 + 1 + 1 + 1 + 1 + 1 = 6 to the sum.
* For i = 2, it is a divisor of 2, 4, and 6, so it contributes 2 + 2 + 2 = 6 to the sum.
* For i = 3, it is a divisor of 3 and 6, so it contributes 3 + 3 = 6 to the sum.
* For i = 4, it is a divisor of 4, so it contributes 4 to the sum.
* For i = 5, it is a divisor of 5, so it contributes 5 to the sum.
* For i = 6, it is a divisor of 6, so it contributes 6 to the sum.

Adding all these contributions gives the final sum, which is 6 + 6 + 6 + 4 + 5 + 6 = 33, which is the sum of divisors from 1 to 6.

## Time and Auxiliary Space Complexity

* **Time Complexity**: `O(N)` - We iterate through numbers from 1 to N.
* **Auxiliary Space Complexity**: `O(1)` - We use a constant amount of extra space.

## Code (C++)

```cpp
class Solution {
public:
    long long sumOfDivisors(int N) {
        long long out = 0;
        for (int i = 1; i <= N; ++i) {
            out += (N / i) * i;
        }
        return out;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gl01.gitbook.io/gfg-editorials/2023/10-2023-oct-20/21-sum-of-all-divisors-from-1-to-n.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
