In this article, we will discuss how to efficiently count the number of integers up to N that have exactly 9 divisors. This is a commonly asked problem in coding interviews and competitive programming.
Given an integer N, we need to find how many numbers in the range [1, N] have exactly 9 divisors.
N = 100
2
The numbers 36 and 100 have exactly 9 divisors:
1, 2, 3, 4, 6, 9, 12, 18, 36
(9 divisors)1, 2, 4, 5, 10, 20, 25, 50, 100
(9 divisors)Thus, the answer is 2.
A number X has exactly 9 divisors if and only if it is of one of the following two forms:
1, 2, 4, 8, 16, 32, 64, 128, 256
)1, 2, 3, 4, 6, 9, 12, 18, 36
)To efficiently compute the numbers satisfying the above conditions, we:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// Function to generate prime numbers using Sieve of Eratosthenes
vector<int> generatePrimes(int n) {
vector<bool> isPrime(n + 1, true);
vector<int> primes;
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i)
isPrime[j] = false;
}
}
for (int i = 2; i <= n; i++) {
if (isPrime[i])
primes.push_back(i);
}
return primes;
}
// Function to count numbers with exactly 9 divisors
int countNumbersWith9Divisors(int N) {
vector<int> primes = generatePrimes(sqrt(N));
int count = 0;
// Case 1: p^8
for (int prime : primes) {
long long power8 = pow(prime, 8);
if (power8 > N) break;
count++;
}
// Case 2: p1^2 * p2^2
int len = primes.size();
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
long long num = (long long)primes[i] * primes[i] * primes[j] * primes[j];
if (num > N) break;
count++;
}
}
return count;
}
int main() {
int N;
cout << "Enter N: ";
cin >> N;
cout << "Numbers with exactly 9 divisors: " << countNumbersWith9Divisors(N) << endl;
return 0;
}
import java.util.ArrayList;
import java.util.Scanner;
public class NineDivisors {
// Function to generate prime numbers using Sieve of Eratosthenes
static ArrayList<Integer> generatePrimes(int n) {
boolean[] isPrime = new boolean[n + 1];
ArrayList<Integer> primes = new ArrayList<>();
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i)
isPrime[j] = false;
}
}
for (int i = 2; i <= n; i++) {
if (isPrime[i])
primes.add(i);
}
return primes;
}
// Function to count numbers with exactly 9 divisors
static int countNumbersWith9Divisors(int N) {
ArrayList<Integer> primes = generatePrimes((int) Math.sqrt(N));
int count = 0;
// Case 1: p^8
for (int prime : primes) {
long power8 = (long) Math.pow(prime, 8);
if (power8 > N) break;
count++;
}
// Case 2: p1^2 * p2^2
int len = primes.size();
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
long num = (long) primes.get(i) * primes.get(i) * primes.get(j) * primes.get(j);
if (num > N) break;
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter N: ");
int N = scanner.nextInt();
scanner.close();
System.out.println("Numbers with exactly 9 divisors: " + countNumbersWith9Divisors(N));
}
}
This problem is a great way to learn number theory, prime factorization, and the Sieve of Eratosthenes. Let me know if you