Find count of Almost Prime numbers from 1 to N
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.
Examples:
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:
C++
// CPP program to count almost prime numbers // from 1 to n #include <bits/stdc++.h> using namespace std; #define N 100005 // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. bool prime[N]; void SieveOfEratosthenes() { memset(prime, true, sizeof(prime)); prime[1] = false; for (int p = 2; p * p < N; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i < N; i += p) prime[i] = false; } } } // Function to count almost prime numbers // from 1 to n int almostPrimes(int n) { // to store required answer int ans = 0; // 6 is first almost prime number for (int i = 6; i <= n; i++) { // to count prime factors int c = 0; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { // if it is perfect square if (j * j == i) { if (prime[j]) c++; } else { if (prime[j]) c++; if (prime[i / j]) c++; } } } // if I is almost prime number if (c == 2) ans++; } return ans; } // Driver code int main() { SieveOfEratosthenes(); int n = 21; cout << almostPrimes(n); return 0; } |
Java
// Java program to count almost prime numbers // from 1 to n import java.io.*; class GFG { static int N = 100005; // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. static boolean prime[] = new boolean[N]; static void SieveOfEratosthenes() { for(int i=0;i<N;i++) prime[i] =true; prime[1] = false; for (int p = 2; p * p < N; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i < N; i += p) prime[i] = false; } } } // Function to count almost prime numbers // from 1 to n static int almostPrimes(int n) { // to store required answer int ans = 0; // 6 is first almost prime number for (int i = 6; i <= n; i++) { // to count prime factors int c = 0; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { // if it is perfect square if (j * j == i) { if (prime[j]) c++; } else { if (prime[j]) c++; if (prime[i / j]) c++; } } } // if I is almost prime number if (c == 2) ans++; } return ans; } // Driver code public static void main (String[] args) { SieveOfEratosthenes(); int n = 21; System.out.println( almostPrimes(n)); } } //This code is contributed by inder_verma.. |
Python 3
# Python 3 program to count almost # prime numbers # from 1 to n # from math import everything from math import * N = 100005 # Create a boolean array "prime[0..n]" # and initialize all entries it as true. # A value in prime[i] will # finally be false if i is Not a prime, else true. prime = [True] * N def SieveOfEratosthenes() : prime[1] = False for p in range(2, int(sqrt(N))) : # If prime[p] is not changed, then # it is a prime if prime[p] == True : # Update all multiples of p for i in range(2*p, N, p) : prime[i] = False # Function to count almost prime numbers # from 1 to n def almostPrimes(n) : # to store required answer ans = 0 # 6 is first almost prime number for i in range(6, n + 1) : # to count prime factors c = 0 for j in range(2, int(sqrt(i)) + 1) : # if it is perfect square if i % j == 0 : if j * j == i : if prime[j] : c += 1 else : if prime[j] : c += 1 if prime[i // j] : c += 1 # if I is almost prime number if c == 2 : ans += 1 return ans # Driver Code if __name__ == "__main__" : SieveOfEratosthenes() n = 21 print(almostPrimes(n)) # This code is contributed by ANKITRAI1 |
C#
// C# program to count almost // prime numbers from 1 to n using System; class GFG { static int N = 100005; // Create a boolean array "prime[0..n]" // and initialize all entries it as // true. A value in prime[i] will finally // be false if i is Not a prime, else true. static bool []prime = new bool[N]; static void SieveOfEratosthenes() { for(int i = 0; i < N; i++) prime[i] = true; prime[1] = false; for (int p = 2; p * p < N; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i < N; i += p) prime[i] = false; } } } // Function to count almost // prime numbers from 1 to n static int almostPrimes(int n) { // to store required answer int ans = 0; // 6 is first almost prime number for (int i = 6; i <= n; i++) { // to count prime factors int c = 0; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { // if it is perfect square if (j * j == i) { if (prime[j]) c++; } else { if (prime[j]) c++; if (prime[i / j]) c++; } } } // if I is almost prime number if (c == 2) ans++; } return ans; } // Driver code public static void Main () { SieveOfEratosthenes(); int n = 21; Console.WriteLine( almostPrimes(n)); } } // This code is contributed // by inder_verma |
PHP
<?php // PHP program to count almost prime // numbers from 1 to n $N = 100005; // Create a boolean array "prime[0..n]" // and initialize all entries it as true. // A value in prime[i] will // finally be false if i is Not a prime, else true. $prime = array_fill(0, $N, true); function SieveOfEratosthenes() { global $N, $prime; $prime[1] = false; for($p = 2; $p < (int)(sqrt($N)); $p++) { // If prime[p] is not changed, then // it is a prime if ($prime[$p] == true) // Update all multiples of p for($i = 2 * $p; $i < $N; $i += $p) $prime[$i] = false; } } // Function to count almost prime // numbers from 1 to n function almostPrimes($n) { global $prime; // to store required answer $ans = 0; // 6 is first almost prime number for($i = 6; $i < $n + 1; $i++) { // to count prime factors $c = 0; for($j = 2; $i >= $j * $j; $j++) { // if it is perfect square if ($i % $j == 0) { if ($j * $j == $i) { if ($prime[$j]) $c += 1; } else { if ($prime[$j]) $c += 1; if ($prime[($i / $j)]) $c += 1; } } } // if I is almost prime number if ($c == 2) $ans += 1; } return $ans; } // Driver Code SieveOfEratosthenes(); $n = 21; print(almostPrimes($n)); // This code is contributed by mits ?> |
8
Recommended Posts:
- Count of numbers below N whose sum of prime divisors is K
- Count numbers from range whose prime factors are only 2 and 3
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count common prime factors of two numbers
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Queries for the difference between the count of composite and prime numbers in a given range
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Find two prime numbers with given sum
- Find the XOR of first N Prime Numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Find the Product of first N Prime Numbers
- Find the sum of prime numbers in the Kth array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



