text: Find and Count total factors of co | C # implementation of the above approach ; Function to return the count of numbers which are divisible by both A and B in the range [ 1 , N ] in constant time ; Compute the count of numbers divisible by A in the range [ 1 , N ] ; Compute the count of numbers divisible by B in the range [ 1 , N ] ; Adding the counts which are divisible by A and B ; The above value might contain repeated values which are divisible by both A and B . Therefore , the count of numbers which are divisible by both A and B are found ; The count computed above is subtracted to compute the readonly count ; Function to return the sum of numbers which are divisible by both A and B in the range [ 1 , N ] ; Set to store the numbers so that the numbers are not repeated ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to iterate through the set and find the sum ; Driver code
code: using System ; using System . Collections . Generic ; class GFG { static int countOfNum ( int n , int a , int b ) { int cnt_of_a , cnt_of_b , cnt_of_ab , sum ; cnt_of_a = n / a ; cnt_of_b = n / b ; sum = cnt_of_b + cnt_of_a ; cnt_of_ab = n / ( a * b ) ; sum = sum - cnt_of_ab ; return sum ; } static int sumOfNum ( int n , int a , int b ) { int i ; int sum = 0 ; HashSet < int > ans = new HashSet < int > ( ) ; for ( i = a ; i <= n ; i = i + a ) { ans . Add ( i ) ; } for ( i = b ; i <= n ; i = i + b ) { ans . Add ( i ) ; } foreach ( int it in ans ) { sum = sum + it ; } return sum ; } public static void Main ( String [ ] args ) { int N = 88 ; int A = 11 ; int B = 8 ; int count = countOfNum ( N , A , B ) ; int sumofnum = sumOfNum ( N , A , B ) ; Console . Write ( sumofnum % count ) ; } }