Count all the numbers less than 10^6 whose minimum prime factor is N
Last Updated :
19 Nov, 2021
Given a number N which is prime. The task is to find all the numbers less than or equal to 10^6 whose minimum prime factor is N.Examples:
Input: N = 2
Output: 500000
Input: N = 3
Output: 166667
Approach: Use sieve of Eratosthenes to find the solution to the problem. Store all the prime numbers less than 10^6 . Form another sieve that will store the count of all the numbers whose minimum prime factor is the index of the sieve. Then display the count of the prime number N (i.e. sieve_count[n]+1), where n is the prime number. Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
int sieve_Prime[MAX + 4] = { 0 },
sieve_count[MAX + 4] = { 0 };
void form_sieve()
{
sieve_Prime[1] = 1;
for (int i = 2; i <= MAX; i++) {
if (sieve_Prime[i] == 0) {
for (int j = i * 2; j <= MAX; j += i) {
if (sieve_Prime[j] == 0) {
sieve_Prime[j] = 1;
sieve_count[i]++;
}
}
}
}
}
int main()
{
form_sieve();
int n = 2;
cout << "Count = " << (sieve_count[n] + 1) << endl;
n = 3;
cout << "Count = " << (sieve_count[n] + 1) << endl;
return 0;
}
Java
import java.io.*;
class GFG {
static int MAX = 1000000;
static int sieve_Prime[] = new int[MAX + 4];
static int sieve_count[] = new int[MAX + 4];
static void form_sieve()
{
sieve_Prime[1] = 1;
for (int i = 2; i <= MAX; i++) {
if (sieve_Prime[i] == 0) {
for (int j = i * 2; j <= MAX; j += i) {
if (sieve_Prime[j] == 0) {
sieve_Prime[j] = 1;
sieve_count[i]++;
}
}
}
}
}
public static void main (String[] args) {
form_sieve();
int n = 2;
System.out.println( "Count = " + (sieve_count[n] + 1));
n = 3;
System.out.println ("Count = " +(sieve_count[n] + 1));
}
}
Python3
MAX = 1000000
sieve_Prime = [0 for i in range(MAX + 4)]
sieve_count = [0 for i in range(MAX + 4)]
def form_sieve():
sieve_Prime[1] = 1
for i in range(2, MAX + 1):
if sieve_Prime[i] == 0:
for j in range(i * 2, MAX + 1, i):
if sieve_Prime[j] == 0:
sieve_Prime[j] = 1
sieve_count[i] += 1
form_sieve()
n = 2
print("Count =", sieve_count[n] + 1)
n = 3
print("Count =", sieve_count[n] + 1)
C#
using System;
class GFG {
static int MAX = 1000000;
static int []sieve_Prime = new int[MAX + 4];
static int []sieve_count = new int[MAX + 4];
static void form_sieve()
{
sieve_Prime[1] = 1;
for (int i = 2; i <= MAX; i++) {
if (sieve_Prime[i] == 0) {
for (int j = i * 2; j <= MAX; j += i) {
if (sieve_Prime[j] == 0) {
sieve_Prime[j] = 1;
sieve_count[i]++;
}
}
}
}
}
public static void Main () {
form_sieve();
int n = 2;
Console.WriteLine( "Count = " + (sieve_count[n] + 1));
n = 3;
Console.WriteLine ("Count = " +(sieve_count[n] + 1));
}
}
PHP
<?php
$MAX = 1000000;
$sieve_Prime = array_fill(0, $MAX + 4, NULL);
$sieve_count = array_fill(0, $MAX + 4, NULL);
function form_sieve()
{
global $sieve_Prime, $sieve_count, $MAX;
$sieve_Prime[1] = 1;
for ($i = 2; $i <= $MAX; $i++)
{
if ($sieve_Prime[$i] == 0)
{
for ($j = $i * 2; $j <= $MAX; $j += $i)
{
if ($sieve_Prime[$j] == 0)
{
$sieve_Prime[$j] = 1;
$sieve_count[$i]++;
}
}
}
}
}
form_sieve();
$n = 2;
echo "Count = " . ($sieve_count[$n] + 1) . "\n";
$n = 3;
echo "Count = " . ($sieve_count[$n] + 1) . "\n";
?>
Javascript
<script>
var MAX = 1000000;
var sieve_Prime = Array.from({length: MAX + 4},
(_, i) => 0);
var sieve_count = Array.from({length: MAX + 4},
(_, i) => 0);
function form_sieve()
{
sieve_Prime[1] = 1;
for (i = 2; i <= MAX; i++) {
if (sieve_Prime[i] == 0) {
for (j = i * 2; j <= MAX; j += i) {
if (sieve_Prime[j] == 0) {
sieve_Prime[j] = 1;
sieve_count[i]++;
}
}
}
}
}
form_sieve();
var n = 2;
document.write( "Count = " + (sieve_count[n] + 1));
n = 3;
document.write("<br>Count = " +(sieve_count[n] + 1));
</script>
Output
Count = 500000
Count = 166667
Time Complexity: O(N*log(log(N))), where N=106 .
Auxiliary Space: O(106 )
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...