Sum of all odd factors of numbers in the range [ l , r ] | C # implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
using System ; class GFG { public static int MAX = 100001 ; public static int [ ] prefix = new int [ MAX ] ; public static void sieve_modified ( ) { for ( int i = 1 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) { prefix [ j ] += i ; } } for ( int i = 1 ; i < MAX ; i ++ ) { prefix [ i ] += prefix [ i - 1 ] ; } } public static int sumOddFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } public static void Main ( string [ ] args ) { sieve_modified ( ) ; int l = 6 , r = 10 ; Console . WriteLine ( sumOddFactors ( l , r ) ) ; } }