700262. Almost Prime Numbers

Easy

Here is the generated Markdown blog post for the "Almost Prime Numbers" problem:

Almost Prime Numbers

Summary

This problem involves identifying almost prime numbers within a given range. An almost prime number is a number that has only two distinct prime factors. This problem requires an understanding of prime numbers and their properties.

Detailed Explanation

To solve this problem, we can start by iterating through the given range of numbers. For each number, we need to check if it's prime or not. If the number is prime, then we need to find its prime factors. If a prime number has more than two distinct prime factors, it's not an almost prime number.

Here's a step-by-step breakdown of the solution:

  1. Start by iterating through the given range of numbers.
  2. For each number, check if it's prime or not. You can use the Sieve of Eratosthenes algorithm to efficiently check for primality.
  3. If the number is prime, then find its prime factors using a similar approach as in step 2.
  4. Count the distinct prime factors. If there are more than two, the number is not an almost prime number.

Here's a simple ASCII art diagram illustrating this process:

          +---------------+
          | Iterate through |
          |   range of numbers |
          +---------------+
                  |
                  v
+---------------------------------------+
|  Check if each number is prime?      |
|  (Use Sieve of Eratosthenes)         |
+---------------------------------------+
                  |
                  v
+---------------------------------------+
|  If prime, find its prime factors?   |
|  (Similar to step 2)              |
+---------------------------------------+
                  |
                  v
+---------------------------------------+
|  Count distinct prime factors?      |
|  (If more than two, not almost prime)|
+---------------------------------------+

Time complexity: O(n log log n) for the Sieve of Eratosthenes algorithm and O(sqrt(p)) for finding prime factors. Space complexity: O(n) for storing the sieve.

Optimized Solutions

Java

View on GeeksforGeeks
public class AlmostPrime {
    public static List<Integer> findAlmostPrimes(int start, int end) {
        List<Integer> almostPrimes = new ArrayList<>();
        boolean[] isPrime = new boolean[end + 1];
        
        // Initialize sieve
        for (int i = 2; i * i <= end; i++) {
            if (!isPrime[i]) {
                for (int j = i * i; j <= end; j += i) {
                    isPrime[j] = true;
                }
            }
        }
        
        // Find almost primes
        for (int i = start; i <= end; i++) {
            if (!isPrime[i]) {
                int factors = 0;
                for (int j = 2; j * j <= i; j++) {
                    if (i % j == 0) {
                        factors++;
                        while (i % j == 0) {
                            i /= j;
                        }
                    }
                }
                if (factors <= 2) {
                    almostPrimes.add(i);
                }
            }
        }
        
        return almostPrimes;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.