Sum of elements in range L | Function to calculate the sum if n is even ; Both l and r are to the left of mid ; First and last element ; Total number of terms in the sequence is r - l + 1 ; Use of formula derived ; Both l and r are to the right of mid ; First and last element ; Use of formula derived ; Left is to the left of mid and right is to the right of mid ; Take two sums i . e left and right differently and add ; First and last element ; total terms ; no of terms ; The first even number is 2 ; The last element is ; formula applied ; Function to calculate the sum if n is odd ; Take ceil value if n is odd ; Both l and r are to the left of mid ; First and last element ; number of terms ; formula ; both l and r are to the right of mid ; first and last term , ; no of terms ; formula used ; If l is on left and r on right ; calculate separate sums ; first half ; calculate terms ; second half ; add both halves ; Function to find the sum between L and R ; If n is even ; If n is odd ; Driver Code
def sumeven ( n , l , r ) : NEW_LINE INDENT sum = 0 NEW_LINE mid = n // 2 NEW_LINE if ( r <= mid ) : NEW_LINE INDENT first = ( 2 * l - 1 ) NEW_LINE last = ( 2 * r - 1 ) NEW_LINE no_of_terms = r - l + 1 NEW_LINE sum = ( ( no_of_terms ) * ( ( first + last ) ) ) // 2 NEW_LINE DEDENT elif ( l >= mid ) : NEW_LINE INDENT first = ( 2 * ( l - n // 2 ) ) NEW_LINE last = ( 2 * ( r - n // 2 ) ) NEW_LINE no_of_terms = r - l + 1 NEW_LINE sum = ( ( no_of_terms ) * ( ( first + last ) ) ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT sumleft , sumright = 0 , 0 NEW_LINE first_term1 = ( 2 * l - 1 ) NEW_LINE last_term1 = ( 2 * ( n // 2 ) - 1 ) NEW_LINE no_of_terms1 = n // 2 - l + 1 NEW_LINE sumleft = ( ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) // 2 ) NEW_LINE first_term2 = 2 NEW_LINE last_term2 = ( 2 * ( r - n // 2 ) ) NEW_LINE no_of_terms2 = r - mid NEW_LINE sumright = ( ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) // 2 ) NEW_LINE sum = ( sumleft + sumright ) ; NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumodd ( n , l , r ) : NEW_LINE INDENT mid = n // 2 + 1 ; NEW_LINE sum = 0 NEW_LINE if ( r <= mid ) : NEW_LINE INDENT first = ( 2 * l - 1 ) NEW_LINE last = ( 2 * r - 1 ) NEW_LINE no_of_terms = r - l + 1 NEW_LINE sum = ( ( ( no_of_terms ) * ( ( first + last ) ) ) // 2 ) NEW_LINE DEDENT elif ( l > mid ) : NEW_LINE INDENT first = ( 2 * ( l - mid ) ) NEW_LINE last = ( 2 * ( r - mid ) ) NEW_LINE no_of_terms = r - l + 1 NEW_LINE sum = ( ( ( no_of_terms ) * ( ( first + last ) ) ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT sumleft , sumright = 0 , 0 NEW_LINE first_term1 = ( 2 * l - 1 ) NEW_LINE last_term1 = ( 2 * mid - 1 ) NEW_LINE no_of_terms1 = mid - l + 1 NEW_LINE sumleft = ( ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) // 2 ) NEW_LINE first_term2 = 2 NEW_LINE last_term2 = ( 2 * ( r - mid ) ) NEW_LINE no_of_terms2 = r - mid NEW_LINE sumright = ( ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) // 2 ) NEW_LINE sum = ( sumleft + sumright ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def rangesum ( n , l , r ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT return sumeven ( n , l , r ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return sumodd ( n , l , r ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE l = 1 NEW_LINE r = 11 ; NEW_LINE print ( rangesum ( n , l , r ) ) NEW_LINE DEDENT