- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count all the numbers less than 10^6 whose minimum prime factor is N C++

We are given a prime number let’s say, num and the task is to calculate the count of all the numbers less than 10^6 whose minimum prime factor is equal to num.

## For Example

Input− num = 7Output− Number of prime factors = 38095Input− num = 3Output− Number of prime factors = 16666

Approach used in the below program is as follows

Input the number let’s say num

Start the loop, from i to 2 and i should be less than or equals to max value and increment the value of i

Inside the loop, check if s_prime[i] = 0

Create the loop, set the j to i * 2 and j should be less than equals to max and set j to j + i

Now check, if s_prime[j] = 1

Set s_prime[j] = 1

Increment s_count[i] by 1

Print the result

## Example

#include <bits/stdc++.h> using namespace std; #define MAX 1000000 // a sieve for prime number and // to count the number of prime int s_prime[MAX + 4] = { 0 }, s_count[MAX + 4] = { 0 }; void create_sieve(){ // As 1 is not a prime number s_prime[1] = 1; // creating the sieve for (int i = 2; i <= MAX; i++){ // if i is a prime number if (s_prime[i] == 0){ for (int j = i * 2; j <= MAX; j += i){ // if i is the least prime factor if (s_prime[j] == 0){ // The number j is not a prime s_prime[j] = 1; // counting the numbers whose least prime factor // is i s_count[i]++; } } } } } int main(){ // create the sieve create_sieve(); int N = 7; cout << "Number of prime factors = " << (s_count[N] + 1) << endl; N = 3; cout << "Number of prime factors = " << (s_count[N] + 1) << endl; return 0; }

## Output

If we run the above code it will generate the following output −

Number of prime factors = 38095 Number of prime factors = 166667

- Related Questions & Answers
- Print all prime numbers less than or equal to N in C++
- Print all Semi-Prime Numbers less than or equal to N in C++
- Nearest prime less than given number n C++
- An interesting solution to get all prime numbers smaller than n?
- Count pairs with sum as a prime number and less than n in C++
- Find all factorial numbers less than or equal to n in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count pairs in a sorted array whose product is less than k in C++
- Count natural numbers whose all permutation are greater than that number in C++
- Print all numbers less than N with at-most 2 unique digits in C++
- Count ordered pairs with product less than N in C++
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++
- Minimum number of sets with numbers less than Y in C++
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++

Advertisements