Find XOR of numbers from the range [ L , R ] | Python3 implementation of the approach ; Function to return the XOR of elements from the range [ 1 , n ] ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to return the XOR of elements from the range [ l , r ] ; Driver code
from operator import xor NEW_LINE def findXOR ( n ) : NEW_LINE INDENT mod = n % 4 ; NEW_LINE if ( mod == 0 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT elif ( mod == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( mod == 2 ) : NEW_LINE INDENT return n + 1 ; NEW_LINE DEDENT elif ( mod == 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT def findXORFun ( l , r ) : NEW_LINE INDENT return ( xor ( findXOR ( l - 1 ) , findXOR ( r ) ) ) ; NEW_LINE DEDENT l = 4 ; r = 8 ; NEW_LINE print ( findXORFun ( l , r ) ) ; NEW_LINE