Sum of the numbers upto N that are divisible by 2 or 5 | Function to find the sum ; sum2 is sum of numbers divisible by 2 ; sum5 is sum of number divisible by 5 ; sum10 of numbers divisible by 2 and 5 ; Driver Code
< ? php function findSum ( $ n ) { $ sum2 = ( ( int ) ( $ n / 2 ) * ( 4 + ( ( int ) ( $ n / 2 ) - 1 ) * 2 ) ) / 2 ; $ sum5 = ( ( int ) ( $ n / 5 ) * ( 10 + ( $ n / 5 - 1 ) * 5 ) ) / 2 ; $ sum10 = ( ( int ) ( $ n / 10 ) * ( 20 + ( $ n / 10 - 1 ) * 10 ) ) / 2 ; return $ sum2 + $ sum5 - $ sum10 ; } $ n = 5 ; echo findSum ( $ n ) ; ? >