Given a number N. Find number of almost primes from 1 to . A number is called almost if it has exactly two distinct prime factors.
Note: The numbers can have any number of non-prime factors but should have exactly two prime factors.
Input : N = 10 Output : 2 Explanation : 6, 10 are such numbers. Input : N = 21 Output : 8
An efficient solution is to find prime numbers using Sieve of Eratosthenes. And find distinct prime factors count for numbers less than N.
Please Refer: Almost Prime Numbers
Below is the implementation of the above approach:
= $j * $j; $j++)
// if it is perfect square
if ($i % $j == 0)
if ($j * $j == $i)
$c += 1;
$c += 1;
if ($prime[($i / $j)])
$c += 1;
// if I is almost prime number
if ($c == 2)
$ans += 1;
// Driver Code
$n = 21;
// This code is contributed by mits
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Count numbers from range whose prime factors are only 2 and 3
- Count common prime factors of two numbers
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Find two prime numbers with given sum
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Find product of prime numbers between 1 to n
- Find the Product of first N Prime Numbers
- Program to find sum of prime numbers between 1 to n
- Program to find Prime Numbers Between given Interval
- Find two distinct prime numbers with given product
- Find the prime numbers which can written as sum of most consecutive primes
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.