Given a number N(1<=N<=109), the task is to find the total number of integers less than equal to n which have exactly 9 divisors.
Input: N = 100
The two numbers which have exactly 9 divisors are 36 and 100.
Input: N = 1000
The numbers are 36 100 196 225 256 441 484 676
A naive approach is to iterate for all numbers till N and count the numbers that have exactly 9 divisors. For counting number of divisors, one can easily iterate till N and check if N is divisible by i or not and keep a count.
Below is the implementation of above approach:
An efficient approach is to use the prime factors property to count the number of divisors of a number. The method can be found here. If any number(let x) can be expressed in terms of (p^2 * q^2) or (p^8), where p and q are prime factors of X, then X has a total of 9 divisors. The below steps can be followed to solve the above problem.
- Use Sieve technique to mark the smallest prime factor of a number.
- We just need to check for all the numbers in range[1-sqrt(n)] that can be expressed in terms of p*q since (p^2*q^2) has 9 factors, hence (p*q)^2 will also have exactly 9 factors.
- Iterate from 1 to sqrt(n) and check if i can be expressed as p*q, where p and q are prime numbers.
- Also check if i is prime then pow(i, 8)<=n or not, in that case count that number also.
- The summation of count of numbers that can be expressed in form p*q and p^8 is our answer.
Below is the implementation of the above approach:
# Python3 implementation of the above approach
# Function to count numbers
# having exactly 9 divisors
c = 0
limit = int(n ** (0.5))
# Sieve array, initially prime[i] = i
prime = [i for i in range(limit + 1)]
# use seive concept to store the
# first prime factor of every number
i = 2
while i * i <= limit: if prime[i] == i: # mark all factors of i for j in range(i * i, limit + 1, i): if prime[j] == j: prime[j] = i i += 1 # check for all numbers if they # can be expressed in form p*q for i in range(2, limit + 1): # p prime factor p = prime[i] # q prime factor q = prime[i // prime[i]] # if both prime factors are different # if p*q<=n and q!= if p * q == i and q != 1 and p != q: c += 1 elif prime[i] == i: # Check if it can be # expressed as p^8 if i ** 8 <= n: c += 1 return c # Driver Code if __name__ == "__main__": n = 1000 print(countNumbers(n)) # This code is contributed # by Rituraj Jain [tabby title="C#"]
Time Complexity: O(sqrt(N))
Auxiliary Space: O(sqrt(N))
- Count the numbers < N which have equal number of divisors as K
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Count all perfect divisors of a number
- Count elements in the given range which have maximum number of divisors
- Program to find count of numbers having odd number of divisors in given range
- Count ways to express even number ‘n’ as sum of even integers
- Count number of triplets with product equal to given number with duplicates allowed
- Find sum of divisors of all the divisors of a natural number
- Count number of subsets of a set with GCD equal to a given number
- Count pairs of natural numbers with GCD equal to given number
- Count Divisors of n in O(n^1/3)
- Find three integers less than or equal to N such that their LCM is maximum
- Check if count of divisors is even or odd
- Count Divisors of Factorial
- Check if the sum of distinct digits of two integers are equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.