Sum of all numbers divisible by 6 in a given range | function to calculate the sum of all numbers divisible by 6 in range L - R . . ; no of multiples of 6 upto r ; no of multiples of 6 upto l - 1 ; summation of all multiples of 6 upto r ; summation of all multiples of 6 upto l - 1 ; returns the answer ; Driver Code
< ? php function sum ( $ L , $ R ) { $ p = intval ( $ R / 6 ) ; $ q = intval ( ( $ L - 1 ) / 6 ) ; $ sumR = intval ( 3 * ( $ p * ( $ p + 1 ) ) ) ; $ sumL = intval ( ( $ q * ( $ q + 1 ) ) * 3 ) ; return $ sumR - $ sumL ; } $ L = 1 ; $ R = 20 ; echo sum ( $ L , $ R ) ; ? >