Sum of all divisors from 1 to n |  ; int t1 = i * ( num / i - i + 1 ) ; adding i every time it appears with numbers greater than or equal to itself int t2 = ( ( ( num / i ) * ( num / i + 1 ) ) / 2 ) - ( ( i * ( i + 1 ) ) / 2 ) ; adding numbers that appear with i and are greater than i ; Driver code
import java . io . * ; class GFG { public static int sum_all_divisors ( int num ) { int sum = 0 ; for ( int i = 1 ; i <= Math . sqrt ( num ) ; i ++ ) { sum += t1 + t2 ; } return sum ; } public static void main ( String [ ] args ) { int n = 1 ; int sum = sum_all_divisors ( n ) ; System . out . println ( sum ) ; } }