Count integers in range [L, R] not divisible by first K Prime numbers
Last Updated :
30 Mar, 2022
Given two positive integers L and R, the task is to find the count of numbers in the range [L, R] which are not divisible by any of the first K prime numbers.
Input: L = 10, R = 25, K = 3
Output: 5
Explanation: In the given range there are 5 numbers 11, 13, 17, 19, 23 which are not divisible by any of the first 3 prime numbers.
So the count is 5.
Input: L = 50, R = 1000, K = 5
Output: 196
Approach: The given problem can be solved using the following idea:
First store the first K prime numbers and then traverse for all the numbers in the range [L, R] and check if they are divisible by any of the first K prime numbers or not
- Store the K prime numbers in a vector by using the Sieve of Eratosthenes.
- Then for all the K prime numbers, iterate a loop and mark all the multiples of that prime number in the L to R as divisible by any of the first K prime numbers.
- After this iterate a loop and count the numbers which are marked as not divisible in the L to R.
- The final count is the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes( int n,
vector< int >& v, int k)
{
bool prime[n + 1];
memset (prime, true , sizeof (prime));
for ( int i = 2; i * i <= n; i++) {
if (prime[i]) {
for ( int j = i * i; j <= n;
j += i)
prime[j] = false ;
}
}
for ( int i = 2; i < n; i++) {
if (prime[i]) {
v.push_back(i);
}
if (v.size() == k) {
break ;
}
}
}
int total_count( int L, int R, int k)
{
vector< int > v;
SieveOfEratosthenes(100000, v, k);
int arr[R + 1] = { 0 };
for ( int i = 0; i < v.size(); i++) {
int val;
if (L % v[i] == 0) {
val = L;
}
else {
val = ((L / v[i]) + 1) * v[i];
}
for ( int j = val; j <= R; j = j
+ v[i]) {
arr[j] = 1;
}
}
int count = 0;
for ( int i = L; i <= R; i++) {
if (arr[i] == 0) {
count++;
}
}
return count;
}
int main()
{
int L = 10, R = 25;
int K = 3;
cout << total_count(L, R, K);
return 0;
}
|
Java
import java.io.*;
import java.util.Vector;
class GFG {
static void SieveOfEratosthenes( int n,
Vector<Integer> v, int k)
{
boolean [] prime = new boolean [n + 1 ];
for ( int i = 0 ; i < n + 1 ; i++) prime[i] = true ;
for ( int i = 2 ; i * i <= n; i++) {
if (prime[i]) {
for ( int j = i * i; j <= n;
j += i)
prime[j] = false ;
}
}
for ( int i = 2 ; i < n; i++) {
if (prime[i]) {
v.add(i);
}
if (v.size() == k) {
break ;
}
}
}
static int total_count( int L, int R, int k)
{
Vector<Integer> v = new Vector<>();
SieveOfEratosthenes( 100000 , v, k);
int [] arr = new int [R + 1 ];
for ( int i = 0 ; i < v.size(); i++) {
int val;
if (L % v.get(i) == 0 ) {
val = L;
}
else {
val = ((L / v.get(i)) + 1 ) * v.get(i);
}
for ( int j = val; j <= R; j = j
+ v.get(i)) {
arr[j] = 1 ;
}
}
int count = 0 ;
for ( int i = L; i <= R; i++) {
if (arr[i] == 0 ) {
count++;
}
}
return count;
}
public static void main (String[] args) {
int L = 10 , R = 25 ;
int K = 3 ;
System.out.print(total_count(L, R, K));
}
}
|
Python3
import math
def SieveOfEratosthenes(n, v, k):
prime = [ True for _ in range (n + 1 )]
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if (prime[i]):
for j in range (i * i, n + 1 , i):
prime[j] = False
for i in range ( 2 , n):
if (prime[i]):
v.append(i)
if ( len (v) = = k):
break
def total_count(L, R, k):
v = []
SieveOfEratosthenes( 100000 , v, k)
arr = [ 0 for _ in range (R + 1 )]
for i in range ( 0 , len (v)):
val = 0
if (L % v[i] = = 0 ):
val = L
else :
val = ((L / / v[i]) + 1 ) * v[i]
for j in range (val, R + 1 , v[i]):
arr[j] = 1
count = 0
for i in range (L, R + 1 ):
if (arr[i] = = 0 ):
count + = 1
return count
if __name__ = = "__main__" :
L, R = 10 , 25
K = 3
print (total_count(L, R, K))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void SieveOfEratosthenes( int n, List< int > v,
int k)
{
bool [] prime = new bool [n + 1];
for ( int i = 0; i < n + 1; i++)
prime[i] = true ;
for ( int i = 2; i * i <= n; i++) {
if (prime[i]) {
for ( int j = i * i; j <= n; j += i)
prime[j] = false ;
}
}
for ( int i = 2; i < n; i++) {
if (prime[i]) {
v.Add(i);
}
if (v.Count == k) {
break ;
}
}
}
static int total_count( int L, int R, int k)
{
List< int > v = new List< int >();
SieveOfEratosthenes(100000, v, k);
int [] arr = new int [R + 1];
for ( int i = 0; i < v.Count; i++) {
int val;
if (L % v[i] == 0) {
val = L;
}
else {
val = ((L / v[i]) + 1) * v[i];
}
for ( int j = val; j <= R; j = j + v[i]) {
arr[j] = 1;
}
}
int count = 0;
for ( int i = L; i <= R; i++) {
if (arr[i] == 0) {
count++;
}
}
return count;
}
public static void Main( string [] args)
{
int L = 10, R = 25;
int K = 3;
Console.Write(total_count(L, R, K));
}
}
|
Javascript
<script>
function SieveOfEratosthenes(n,
v, k)
{
let prime= new Array(n+1).fill( true );
for (let i = 2; i * i <= n; i++) {
if (prime[i]) {
for (let j = i * i; j <= n;
j += i)
prime[j] = false ;
}
}
for (let i = 2; i < n; i++) {
if (prime[i]) {
v.push(i);
}
if (v.length == k) {
break ;
}
}
}
function total_count( L, R, k)
{
let v =[];
SieveOfEratosthenes(100000, v, k);
let arr = new Array(R+1).fill(0);
for (let i = 0; i < v.length; i++) {
let val;
if (L % v[i] == 0) {
val = L;
}
else {
val = (Math.floor(L / v[i]) + 1) * v[i];
}
for (let j = val; j <= R; j = j
+ v[i]) {
arr[j] = 1;
}
}
let count = 0;
for (let i = L; i <= R; i++) {
if (arr[i] == 0) {
count++;
}
}
return count;
}
let L = 10, R = 25;
let K = 3;
document.write(total_count(L, R, K));
</script>
|
Time complexity: O(N*log(log(N)) + K*R) where N is a high positive value
Auxiliary Space: O(N+R)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...