Numbers in range [L, R] such that the count of their divisors is both even and prime
Given a range [L, R], the task is to find the numbers from the range which have the count of their divisors as even as well as prime.
Then, print the count of the numbers found. The values of L and R are less than 10^6 and L< R.
Examples:
Input: L=3, R=9
Output: Count = 3
Explanation: The numbers are 3, 5, 7
Input : L=3, R=17
Output : Count: 6
- The only number that is prime, as well as even, is ‘2’.
- So, we need to find all the numbers within the given range that have exactly 2 divisors,
i.e. prime numbers.
Naive Approach:
- Iterate through each number x in the range [L, R].
- For each number x, count the number of divisors it has.
- Check if the count of divisors is both even and prime. If it is, increment a counter.
- After iterating through all numbers in the range, return the final count.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool isPrime( int num)
{
if (num < 2)
return false ;
for ( int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false ;
}
return true ;
}
int countDivisors( int num)
{
int count = 0;
for ( int i = 1; i <= num; i++) {
if (num % i == 0)
count++;
}
return count;
}
int countNumbers( int L, int R)
{
int count = 0;
for ( int x = L; x <= R; x++) {
int divisors = countDivisors(x);
if (divisors % 2 == 0 && isPrime(divisors))
count++;
}
return count;
}
int main()
{
int L = 3, R = 9;
int count = countNumbers(L, R);
cout << "Count: " << count << endl;
return 0;
}
|
Java
public class GFG {
public static boolean isPrime( int num)
{
if (num < 2 )
return false ;
for ( int i = 2 ; i * i <= num; i++) {
if (num % i == 0 )
return false ;
}
return true ;
}
public static int countDivisors( int num)
{
int count = 0 ;
for ( int i = 1 ; i <= num; i++) {
if (num % i == 0 )
count++;
}
return count;
}
public static int countNumbers( int L, int R)
{
int count = 0 ;
for ( int x = L; x <= R; x++) {
int divisors = countDivisors(x);
if (divisors % 2 == 0 && isPrime(divisors))
count++;
}
return count;
}
public static void main(String[] args)
{
int L = 3 , R = 9 ;
int count = countNumbers(L, R);
System.out.println( "Count: " + count);
}
}
|
Python3
def is_prime(num):
if num < 2 :
return False
for i in range ( 2 , int (num * * 0.5 ) + 1 ):
if num % i = = 0 :
return False
return True
def count_divisors(num):
count = 0
for i in range ( 1 , num + 1 ):
if num % i = = 0 :
count + = 1
return count
def count_numbers(L, R):
count = 0
for x in range (L, R + 1 ):
divisors = count_divisors(x)
if divisors % 2 = = 0 and is_prime(divisors):
count + = 1
return count
L = 3
R = 9
count = count_numbers(L, R)
print ( "Count:" , count)
|
C#
using System;
class GFG {
static bool IsPrime( int num)
{
if (num < 2)
return false ;
for ( int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false ;
}
return true ;
}
static int CountDivisors( int num)
{
int count = 0;
for ( int i = 1; i <= num; i++) {
if (num % i == 0)
count++;
}
return count;
}
static int CountNumbers( int L, int R)
{
int count = 0;
for ( int x = L; x <= R; x++) {
int divisors = CountDivisors(x);
if (divisors % 2 == 0 && IsPrime(divisors))
count++;
}
return count;
}
static void Main()
{
int L = 3, R = 9;
int count = CountNumbers(L, R);
Console.WriteLine( "Count: " + count);
}
}
|
Javascript
function isPrime(num) {
if (num < 2) {
return false ;
}
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) {
return false ;
}
}
return true ;
}
function countDivisors(num) {
let count = 0;
for (let i = 1; i <= num; i++) {
if (num % i === 0) {
count++;
}
}
return count;
}
function countNumbers(L, R) {
let count = 0;
for (let x = L; x <= R; x++) {
let divisors = countDivisors(x);
if (divisors % 2 === 0 && isPrime(divisors)) {
count++;
}
}
return count;
}
const L = 3;
const R = 9;
const count = countNumbers(L, R);
console.log( "Count: " + count);
|
Time Complexity: O((R – L + 1) * sqrt(R)).
Auxiliary Space: O(1)
An efficient approach:
- We have to count the prime numbers in range [L, R].
- First, create a sieve which will help in determining whether the number is prime or not in O(1) time.
- Then, create a prefix array to store the count of prime numbers where, element at index ‘i’ holds the count of the prime numbers from ‘1’ to ‘i’.
- Now, if we want to find the count of prime numbers in range [L, R], the count will be (sum[R] – sum[L-1])
- Finally, print the result i.e. (sum[R] – sum[L-1])
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
bool prime[MAX + 1];
int sum[MAX + 1];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
memset (sum, 0, sizeof (sum));
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p]) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
for ( int i = 1; i <= MAX; i++) {
if (prime[i] == true )
sum[i] = 1;
sum[i] += sum[i - 1];
}
}
int main()
{
SieveOfEratosthenes();
int l = 3, r = 9;
int c = (sum[r] - sum[l - 1]);
cout << "Count: " << c << endl;
return 0;
}
|
Java
class GFG
{
static final int MAX= 1000000 ;
static boolean []prime= new boolean [MAX + 1 ];
static int []sum= new int [MAX + 1 ];
static void SieveOfEratosthenes()
{
for ( int i= 0 ;i<=MAX;i++)
prime[i]= true ;
for ( int i= 0 ;i<=MAX;i++)
sum[i]= 0 ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= MAX; p++) {
if (prime[p]) {
for ( int i = p * 2 ; i <= MAX; i += p)
prime[i] = false ;
}
}
for ( int i = 1 ; i <= MAX; i++) {
if (prime[i] == true )
sum[i] = 1 ;
sum[i] += sum[i - 1 ];
}
}
public static void main(String []args)
{
SieveOfEratosthenes();
int l = 3 , r = 9 ;
int c = (sum[r] - sum[l - 1 ]);
System.out.println( "Count: " + c);
}
}
|
Python 3
MAX = 1000000
prime = [ True ] * ( MAX + 1 )
sum = [ 0 ] * ( MAX + 1 )
def SieveOfEratosthenes():
prime[ 1 ] = False
p = 2
while p * p < = MAX :
if (prime[p]):
i = p * 2
while i < = MAX :
prime[i] = False
i + = p
p + = 1
for i in range ( 1 , MAX + 1 ):
if (prime[i] = = True ):
sum [i] = 1
sum [i] + = sum [i - 1 ]
if __name__ = = "__main__" :
SieveOfEratosthenes()
l = 3
r = 9
c = ( sum [r] - sum [l - 1 ])
print ( "Count:" , c)
|
C#
using System;
class GFG
{
static int MAX=1000000;
static bool []prime= new bool [MAX + 1];
static int []sum= new int [MAX + 1];
static void SieveOfEratosthenes()
{
for ( int i=0;i<=MAX;i++)
prime[i]= true ;
for ( int i=0;i<=MAX;i++)
sum[i]=0;
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p]) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
for ( int i = 1; i <= MAX; i++) {
if (prime[i] == true )
sum[i] = 1;
sum[i] += sum[i - 1];
}
}
public static void Main()
{
SieveOfEratosthenes();
int l = 3, r = 9;
int c = (sum[r] - sum[l - 1]);
Console.WriteLine( "Count: " + c);
}
}
|
Javascript
<script>
var MAX = 1000000;
var prime = Array(MAX+1).fill( true );
var sum = Array(MAX+1).fill(0);
function SieveOfEratosthenes()
{
prime[1] = false ;
for ( var p = 2; p * p <= MAX; p++) {
if (prime[p]) {
for ( var i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
for ( var i = 1; i <= MAX; i++) {
if (prime[i] == true )
sum[i] = 1;
sum[i] += sum[i - 1];
}
}
SieveOfEratosthenes();
var l = 3, r = 9;
var c = (sum[r] - sum[l - 1]);
document.write( "Count: " + c );
</script>
|
PHP
<?php
$MAX = 100000;
$prime = array_fill (0, $MAX + 1, true);
$sum = array_fill (0, $MAX + 1, 0);
function SieveOfEratosthenes()
{
global $MAX , $sum , $prime ;
$prime [1] = false;
for ( $p = 2; $p * $p <= $MAX ; $p ++)
{
if ( $prime [ $p ])
{
for ( $i = $p * 2; $i <= $MAX ; $i += $p )
$prime [ $i ] = false;
}
}
for ( $i = 1; $i <= $MAX ; $i ++)
{
if ( $prime [ $i ] == true)
$sum [ $i ] = 1;
$sum [ $i ] += $sum [ $i - 1];
}
}
SieveOfEratosthenes();
$l = 3;
$r = 9;
$c = ( $sum [ $r ] - $sum [ $l - 1]);
echo "Count: " . $c . "\n" ;
?>
|
Time Complexity: O(MAX3/2)
Auxiliary Space: O(MAX)
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...