Sum of first N natural numbers which are divisible by 2 and 7 | Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code
< ? php function sum ( $ N ) { $ S1 = ( int ) ( ( $ N / 2 ) ) * ( int ) ( 2 * 2 + ( int ) ( $ N / 2 - 1 ) * 2 ) / 2 ; $ S2 = ( int ) ( ( $ N / 7 ) ) * ( int ) ( 2 * 7 + ( int ) ( $ N / 7 - 1 ) * 7 ) / 2 ; $ S3 = ( int ) ( ( $ N / 14 ) ) * ( int ) ( 2 * 14 + ( int ) ( $ N / 14 - 1 ) * 14 ) / 2 ; return ( $ S1 + $ S2 ) - $ S3 ; } $ N = 20 ; echo sum ( $ N ) ;