Beautiful Quadruples

  • + 0 comments

    I found a way mathmateically to calculate the number of ALL combinations where order does not matter. I meant to subtract the number of quadruples where each number XOR = 0 from that total, but it turns out it isn't easy to find that.

    def sum_range(start,end):
        #integer skips
        start,end=sorted([start,end])
        return (end-start+1)*(start+end)//2
    
    def findTotal(a,b,c,d):
        #because a,b,c,d is sorted, d is max
        base=d-c+1
        same_frequency_as_bass=d-(b-1)
        same_frequency_sum=sum_range(base,same_frequency_as_bass)*sum_range(b-a+1,b)
      
        staggered_sum=0
        freq=0
        for i in range(0,b-1): #remember that range is [).
            freq+=min([i+1,a])
            staggered_sum+=(d-i)*freq
      
        return same_frequency_sum+staggered_sum