text: Find and Count total factors of co | 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 final 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: function countOfNum ( n , a , b ) { let cnt_of_a , cnt_of_b , cnt_of_ab , sum ; cnt_of_a = Math . floor ( n / a ) ; cnt_of_b = Math . floor ( n / b ) ; sum = cnt_of_b + cnt_of_a ; cnt_of_ab = Math . floor ( n / ( a * b ) ) ; sum = sum - cnt_of_ab ; return sum ; } function sumOfNum ( n , a , b ) { let i ; let sum = 0 ; let ans = new Set ( ) ; for ( i = a ; i <= n ; i = i + a ) { ans . add ( i ) ; } for ( i = b ; i <= n ; i = i + b ) { ans . add ( i ) ; } for ( let it of ans . values ( ) ) { sum = sum + it ; } return sum ; } let N = 88 ; let A = 11 ; let B = 8 ; let count = countOfNum ( N , A , B ) ; let sumofnum = sumOfNum ( N , A , B ) ; document . write ( sumofnum % count ) ;