Skip to content

Commit bb4c680

Browse files
Math: GFG. Sum of all divisors from 1 to n
1 parent 0c826ad commit bb4c680

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/math/easy/SumOfAllDivisors.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package math.easy;
2+
3+
/***
4+
* Problem in GeeksForGeeks: https://practice.geeksforgeeks.org/problems/sum-of-all-divisors-from-1-to-n4738/1
5+
*
6+
* Given a positive integer N., The task is to find the value of F(i) where function F(i) for the number i be defined as the sum of all divisors of ‘i‘.
7+
*
8+
* Example 1:
9+
* Input: n = 4
10+
* Output: 15
11+
*
12+
* Example 2:
13+
* Input: n = 5
14+
* Output: 21
15+
*/
16+
17+
public class SumOfAllDivisors {
18+
public static void main(String[] args) {
19+
int n = 4;
20+
21+
System.out.println("Divisors sum is: " + findSumOfAllDivisors(n));
22+
}
23+
24+
private static int findSumOfAllDivisors(int n) {
25+
int sum = 0;
26+
27+
for (int i = 1; i <= n; i++) {
28+
sum += ((n / i) * i);
29+
}
30+
31+
return sum;
32+
}
33+
}

0 commit comments

Comments
 (0)