Find difference between count of Prime and Composite in given Range
Given two integers L and R, the task is to find the value of (number of composites – number of primes) that lie in the range [L, R].
Note: 1 is considered neither a prime nor a composite number.
Examples:
Input: L = 2, R = 10
Output: 1
Explanation: The composite numbers in the range are 4, 6, 8, 9, 10 and the prime numbers are 2, 3, 5, 7. So the output is 1.
Input: L = 4, R = 5
Output: 0
Approach: The problem can be solved by using the following idea:
Find the count of primes and the count of composites within the given range and then find the difference.
Follow the steps mentioned below to implement the idea:
- Traverse from i = L to R:
- Traverse from j = 2 to the square root of the i.
- If i is divisible by j, then it is not a prime number. So increment the count of composite numbers.
- Otherwise, increment the count of prime numbers.
- Return the difference as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findDiff( int L, int R)
{
int prime = 0, comp = 0;
bool flag;
for ( int i = L; i <= R; i++) {
flag = false ;
for ( int j = 2; j <= sqrt (i); j++) {
if (i % j == 0) {
flag = true ;
break ;
}
}
if (flag)
comp++;
else
prime++;
}
return comp - prime;
}
int main()
{
int L = 2, R = 10;
cout << findDiff(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int findDiff( int L, int R)
{
int prime = 0 , comp = 0 ;
boolean flag = true ;
for ( int i = L; i <= R; i++) {
flag = false ;
for ( int j = 2 ; j <= Math.sqrt(i); j++) {
if (i % j == 0 ) {
flag = true ;
break ;
}
}
if (flag == true )
comp++;
else
prime++;
}
return comp - prime;
}
public static void main(String[] args)
{
int L = 2 , R = 10 ;
System.out.print(findDiff(L, R));
}
}
|
Python3
import math
def findDiff(L, R):
prime = 0
comp = 0
flag = True
for i in range (L, R + 1 ):
flag = False
for j in range ( 2 ,( int )(math.sqrt(i)) + 1 ):
if (i % j = = 0 ):
flag = True
break
if (flag):
comp = comp + 1
else :
prime = prime + 1
return comp - prime
L = 2
R = 10
print (findDiff(L, R))
|
C#
using System;
class GFG {
static int findDiff( int L, int R)
{
int prime = 0, comp = 0;
bool flag = true ;
for ( int i = L; i <= R; i++) {
flag = false ;
for ( int j = 2; j <= Math.Sqrt(i); j++) {
if (i % j == 0) {
flag = true ;
break ;
}
}
if (flag)
comp++;
else
prime++;
}
return comp - prime;
}
public static void Main()
{
int L = 2, R = 10;
Console.Write(findDiff(L, R));
}
}
|
Javascript
const findDiff = (L, R) => {
let prime = 0, comp = 0;
let flag;
for (let i = L; i <= R; i++) {
flag = false ;
for (let j = 2; j <= parseInt(Math.sqrt(i)); j++) {
if (i % j == 0) {
flag = true ;
break ;
}
}
if (flag)
comp++;
else
prime++;
}
return comp - prime;
}
let L = 2, R = 10;
console.log(findDiff(L, R));
|
Time Complexity: O((R-L) * sqrt( R))
Auxiliary Space: O(1)
Last Updated :
21 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...