Count number of integers less than or equal to N which has exactly 9 divisors | Java implementation of above approach ; Function to count factors in O ( N ) ; iterate and check if factor or not ; Function to count numbers having exactly 9 divisors ; check for all numbers <= N ; check if exactly 9 factors or not ; Driver Code
import java . io . * ; class GFG { static int numberOfDivisors ( int num ) { int c = 0 ; for ( int i = 1 ; i <= num ; i ++ ) { if ( num % i == 0 ) { c += 1 ; } } return c ; } static int countNumbers ( int n ) { int c = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( numberOfDivisors ( i ) == 9 ) c += 1 ; } return c ; } public static void main ( String [ ] args ) { int n = 1000 ; System . out . print ( countNumbers ( n ) ) ; } }