Open In App

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
  1. The only number that is prime, as well as even, is ‘2’.
  2. So, we need to find all the numbers within the given range that have exactly 2 divisors, 
    i.e. prime numbers.

Naive Approach:

Below is the implementation of the above approach: 
 






#include <iostream>
 
using namespace std;
 
// Function to check if a number is prime
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;
}
 
// Function to count the divisors of a number
int countDivisors(int num)
{
    int count = 0;
    for (int i = 1; i <= num; i++) {
        if (num % i == 0)
            count++;
    }
    return count;
}
 
// Function to count the numbers in the given range
// with even prime divisors 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;
}




public class GFG {
 
    // Function to check if a number is prime
    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;
    }
 
    // Function to count the divisors of a number
    public static int countDivisors(int num)
    {
        int count = 0;
        for (int i = 1; i <= num; i++) {
            if (num % i == 0)
                count++;
        }
        return count;
    }
 
    // Function to count the numbers in the given range
    // with even prime divisors 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);
    }
}
// This code is contributed by shivamgupta0987654321




# Function to check if a number is prime
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
 
# Function to count the divisors of a number
def count_divisors(num):
    count = 0
    for i in range(1, num + 1):
        if num % i == 0:
            count += 1
    return count
 
# Function to count the numbers in the given range
# with even prime divisors 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)




using System;
 
class GFG {
    // Function to check if a number is prime
    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;
    }
 
    // Function to count the divisors of a number
    static int CountDivisors(int num)
    {
        int count = 0;
        for (int i = 1; i <= num; i++) {
            if (num % i == 0)
                count++;
        }
        return count;
    }
 
    // Function to count the numbers in the given range
    // with even prime divisors 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);
    }
}
// This code is contributed by shivamgupta0987654321




// Function to check if a number is prime
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 to count the divisors of a number
function countDivisors(num) {
    let count = 0;
    for (let i = 1; i <= num; i++) {
        if (num % i === 0) {
            count++;
        }
    }
    return count;
}
 
// Function to count the numbers in the given range
// with an even count of prime divisors
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;
}
 
// Driver code
const L = 3;
const R = 9;
const count = countNumbers(L, R);
console.log("Count: " + count);

Output
Count: 3

Time Complexity: O((R – L + 1) * sqrt(R)).
Auxiliary Space: O(1)

An efficient approach: 

Below is the implementation of the above approach: 




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
 
// stores whether the number is prime or not
bool prime[MAX + 1];
 
// stores the count of prime numbers
// less than or equal to the index
int sum[MAX + 1];
 
// create the sieve
void SieveOfEratosthenes()
{
    // Create a boolean array "prime[0..n]" and initialize
    // all the entries as true. A value in prime[i] will
    // finally be false if 'i' is Not a prime, else true.
    memset(prime, true, sizeof(prime));
    memset(sum, 0, sizeof(sum));
    prime[1] = false;
 
    for (int p = 2; p * p <= MAX; p++) {
 
        // If prime[p] is not changed, then it is a prime
        if (prime[p]) {
 
            // Update all multiples of p
            for (int i = p * 2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
 
    // stores the prefix sum of number
    // of primes less than or equal to 'i'
    for (int i = 1; i <= MAX; i++) {
        if (prime[i] == true)
            sum[i] = 1;
 
        sum[i] += sum[i - 1];
    }
}
 
// Driver code
int main()
{
    // create the sieve
    SieveOfEratosthenes();
 
    // 'l' and 'r' are the lower and upper bounds
    // of the range
    int l = 3, r = 9;
 
    // get the value of count
    int c = (sum[r] - sum[l - 1]);
 
    // display the count
    cout << "Count: " << c << endl;
 
    return 0;
}




// Java implementation of the approach
class GFG
{
    static final int MAX=1000000;
     
    // stores whether the number is prime or not
    static boolean []prime=new boolean[MAX + 1];
     
    // stores the count of prime numbers
    // less than or equal to the index
    static int []sum=new int[MAX + 1];
     
    // create the sieve
    static void SieveOfEratosthenes()
    {
        // Create a boolean array "prime[0..n]" and initialize
        // all the entries as true. A value in prime[i] will
        // finally be false if 'i' is Not a prime, else true.
        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] is not changed, then it is a prime
            if (prime[p]) {
     
                // Update all multiples of p
                for (int i = p * 2; i <= MAX; i += p)
                    prime[i] = false;
            }
        }
     
        // stores the prefix sum of number
        // of primes less than or equal to 'i'
        for (int i = 1; i <= MAX; i++) {
            if (prime[i] == true)
                sum[i] = 1;
     
            sum[i] += sum[i - 1];
        }
    }
     
    // Driver code
    public static void main(String []args)
    {
        // create the sieve
        SieveOfEratosthenes();
     
        // 'l' and 'r' are the lower and upper bounds
        // of the range
        int l = 3, r = 9;
     
        // get the value of count
        int c = (sum[r] - sum[l - 1]);
     
        // display the count
        System.out.println("Count: " + c);
     
    }
 
}




# Python 3 implementation of the approach
MAX = 1000000
 
# stores whether the number is prime or not
prime = [True] * (MAX + 1)
 
# stores the count of prime numbers
# less than or equal to the index
sum = [0] * (MAX + 1)
 
# create the sieve
def SieveOfEratosthenes():
 
    prime[1] = False
 
    p = 2
    while p * p <= MAX:
 
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p]):
 
            # Update all multiples of p
            i = p * 2
            while i <= MAX:
                prime[i] = False
                i += p
                 
        p += 1
 
    # stores the prefix sum of number
    # of primes less than or equal to 'i'
    for i in range(1, MAX + 1):
        if (prime[i] == True):
            sum[i] = 1
 
        sum[i] += sum[i - 1]
 
# Driver code
if __name__ == "__main__":
     
    # create the sieve
    SieveOfEratosthenes()
 
    # 'l' and 'r' are the lower and
    # upper bounds of the range
    l = 3
    r = 9
 
    # get the value of count
    c = (sum[r] - sum[l - 1])
 
    # display the count
    print("Count:", c)
 
# This code is contributed by ita_c




// C# implementation of the approach
 
 
using System;
class GFG
{
    static int MAX=1000000;
     
    // stores whether the number is prime or not
    static bool []prime=new bool[MAX + 1];
     
    // stores the count of prime numbers
    // less than or equal to the index
    static int []sum=new int[MAX + 1];
     
    // create the sieve
    static void SieveOfEratosthenes()
    {
        // Create a boolean array "prime[0..n]" and initialize
        // all the entries as true. A value in prime[i] will
        // finally be false if 'i' is Not a prime, else true.
        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] is not changed, then it is a prime
            if (prime[p]) {
     
                // Update all multiples of p
                for (int i = p * 2; i <= MAX; i += p)
                    prime[i] = false;
            }
        }
     
        // stores the prefix sum of number
        // of primes less than or equal to 'i'
        for (int i = 1; i <= MAX; i++) {
            if (prime[i] == true)
                sum[i] = 1;
     
            sum[i] += sum[i - 1];
        }
    }
     
    // Driver code
    public static void Main()
    {
        // create the sieve
        SieveOfEratosthenes();
     
        // 'l' and 'r' are the lower and upper bounds
        // of the range
        int l = 3, r = 9;
     
        // get the value of count
        int c = (sum[r] - sum[l - 1]);
     
        // display the count
        Console.WriteLine("Count: " + c);
     
    }
 
}




<script>
 
// Javascript implementation of the approach
var MAX = 1000000;
 
// stores whether the number is prime or not
var prime = Array(MAX+1).fill(true);
 
// stores the count of prime numbers
// less than or equal to the index
var sum = Array(MAX+1).fill(0);
 
// create the sieve
function SieveOfEratosthenes()
{
    // Create a boolean array "prime[0..n]" and initialize
    // all the entries as true. A value in prime[i] will
    // finally be false if 'i' is Not a prime, else true.
 
    prime[1] = false;
 
    for (var p = 2; p * p <= MAX; p++) {
 
        // If prime[p] is not changed, then it is a prime
        if (prime[p]) {
 
            // Update all multiples of p
            for (var i = p * 2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
 
    // stores the prefix sum of number
    // of primes less than or equal to 'i'
    for (var i = 1; i <= MAX; i++) {
        if (prime[i] == true)
            sum[i] = 1;
 
        sum[i] += sum[i - 1];
    }
}
 
// Driver code
// create the sieve
SieveOfEratosthenes();
// 'l' and 'r' are the lower and upper bounds
// of the range
var l = 3, r = 9;
// get the value of count
var c = (sum[r] - sum[l - 1]);
// display the count
document.write( "Count: " + c );
 
</script>




<?php
// PHP implementation of the approach
$MAX = 100000;
 
// Create a boolean array "prime[0..n]"
// and initialize all the entries as
// true. A value in prime[i] will finally
// be false if 'i' is Not a prime, else true.
 
// stores whether the number
// is prime or not
$prime = array_fill(0, $MAX + 1, true);
 
// stores the count of prime numbers
// less than or equal to the index
$sum = array_fill(0, $MAX + 1, 0);
 
// create the sieve
function SieveOfEratosthenes()
{
    global $MAX, $sum, $prime;
    $prime[1] = false;
 
    for ($p = 2; $p * $p <= $MAX; $p++)
    {
 
        // If prime[p] is not changed,
        // then it is a prime
        if ($prime[$p])
        {
 
            // Update all multiples of p
            for ($i = $p * 2; $i <= $MAX; $i += $p)
                $prime[$i] = false;
        }
    }
 
    // stores the prefix sum of number
    // of primes less than or equal to 'i'
    for ($i = 1; $i <= $MAX; $i++)
    {
        if ($prime[$i] == true)
            $sum[$i] = 1;
 
        $sum[$i] += $sum[$i - 1];
    }
}
 
// Driver code
 
// create the sieve
SieveOfEratosthenes();
 
// 'l' and 'r' are the lower
// and upper bounds of the range
$l = 3;
$r = 9;
 
// get the value of count
$c = ($sum[$r] - $sum[$l - 1]);
 
// display the count
echo "Count: " . $c . "\n";
 
// This code is contributed by mits
?>

Output
Count: 3






Time Complexity: O(MAX3/2)
Auxiliary Space: O(MAX)


Article Tags :