Count pairs ( i , j ) such that ( i + j ) is divisible by A and B both | PHP implementation of above approach ; Function to find the LCM ; Function to count the pairs ; Driver code
< ? php function gcd ( $ a , $ b ) { return $ b == 0 ? $ a : gcd ( $ b , $ a % $ b ) ; } function find_LCM ( $ x , $ y ) { return ( int ) ( ( $ x * $ y ) / gcd ( $ x , $ y ) ) ; } function CountPairs ( $ n , $ m , $ A , $ B ) { $ cnt = 0 ; $ lcm = find_LCM ( $ A , $ B ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ cnt += ( int ) ( ( $ m + ( $ i % $ lcm ) ) / $ lcm ) ; return $ cnt ; } $ n = 60 ; $ m = 90 ; $ A = 5 ; $ B = 10 ; echo CountPairs ( $ n , $ m , $ A , $ B ) ; ? >