Open In App

Count of the non-prime divisors of a given number

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the count of non-prime divisors of the given number N.


Examples: 

Input: N = 8 
Output:
Explanation: 
Divisors of 8 are – {1, 2, 4, 8} 
Non-Prime Divisors – {1, 4, 8}


Input: N = 20 
Output:
Explanation: 
Divisors of 20 are – {1, 2, 4, 5, 10, 20} 
Non-Prime Divisors – {1, 4, 10, 20} 

Approach: The key observation in the problem is that any number can be written as a product of its prime factors as 

N=\prod_{i=1}^Ka_{i}^{b_i}
 

where K is the count of the prime factors of the given number. Using permutation and combination we can find that the total count of the 
factors that are 


\prod_{i=1}^k{(b_i + 1)}
 

Therefore, the count of the non-prime factors will be: 


\text{Count of Non-prime factors = } \prod_{i=1}^k{(b_i + 1)} - k
 

Below is the implementation of the above approach: 

C++

// C++ program to find count of
// non-prime divisors of given number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to factors of the given
// number
vector<int> getFactorization(int x)
{
    int count = 0;
    vector<int> v;
 
    // Loop to find the divisors of
    // the number 2
    while (x % 2 == 0) {
        count++;
        x = x / 2;
    }
    if (count != 0)
        v.push_back(count);
 
    // Loop to find the divisors of the
    // given number upto SQRT(N)
    for (int i = 3; i <= sqrt(x); i += 2) {
        count = 0;
        while (x % i == 0) {
            count++;
            x /= i;
        }
        if (count != 0)
            v.push_back(count);
    }
 
    // Condition to check if the rest
    // number is also a prime number
    if (x > 1) {
        v.push_back(1);
    }
    return v;
}
 
// Function to find the non-prime
// divisors of the given number
int nonPrimeDivisors(int N)
{
    vector<int> v = getFactorization(N);
    int ret = 1;
 
    // Loop to count the number of
    // the total divisors of given number
    for (int i = 0; i < v.size(); i++)
        ret = ret * (v[i] + 1);
 
    ret = ret - v.size();
    return ret;
}
 
// Driver Code
int main()
{
    int N = 8;
 
    // Function Call
    cout << nonPrimeDivisors(N) << endl;
    return 0;
}

                    

Java

// Java program to find
// count of non-prime
// divisors of given number
import java.util.*;
class GFG{
 
// Function to factors
// of the given number
static Vector<Integer> getFactorization(int x)
{
  int count = 0;
  Vector<Integer> v = new Vector<>();
 
  // Loop to find the
  // divisors of the number 2
  while (x % 2 == 0)
  {
    count++;
    x = x / 2;
  }
   
  if (count != 0)
    v.add(count);
 
  // Loop to find the divisors
  // of the given number upto SQRT(N)
  for (int i = 3;
           i <= Math.sqrt(x); i += 2)
  {
    count = 0;
    while (x % i == 0)
    {
      count++;
      x /= i;
    }
     
    if (count != 0)
      v.add(count);
  }
 
  // Condition to check if
  // the rest number is also
  // a prime number
  if (x > 1)
  {
    v.add(1);
  }
  return v;
}
 
// Function to find the non-prime
// divisors of the given number
static int nonPrimeDivisors(int N)
{
  Vector<Integer> v = getFactorization(N);
  int ret = 1;
 
  // Loop to count the number of
  // the total divisors of given number
  for (int i = 0; i < v.size(); i++)
    ret = ret * (v.get(i) + 1);
 
  ret = ret - v.size();
  return ret;
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 8;
 
  // Function Call
  System.out.println(nonPrimeDivisors(N));
}
}
 
// This code is contributed by shikhasingrajput

                    

Python3

# Python3 program to find count of
# non-prime divisors of given number
from math import sqrt
 
# Function to factors of the given
# number
def getFactorization(x):
     
    count = 0
    v = []
 
    # Loop to find the divisors of
    # the number 2
    while (x % 2 == 0):
        count += 1
        x = x // 2
 
    if (count != 0):
        v.append(count)
 
    # Loop to find the divisors of the
    # given number upto SQRT(N)
    for i in range(3, int(sqrt(x)) + 12):
        count = 0
         
        while (x % i == 0):
            count += 1
            x //= i
             
        if (count != 0):
            v.append(count)
 
    # Condition to check if the rest
    # number is also a prime number
    if (x > 1):
        v.append(1)
         
    return v
 
# Function to find the non-prime
# divisors of the given number
def nonPrimeDivisors(N):
     
    v = getFactorization(N)
    ret = 1
 
    # Loop to count the number of
    # the total divisors of given number
    for i in range(len(v)):
        ret = ret * (v[i] + 1)
    ret = ret - len(v)
     
    return ret
 
# Driver Code
if __name__ == '__main__':
     
    N = 8
 
    # Function Call
    print(nonPrimeDivisors(N))
 
# This code is contributed by Samarth

                    

C#

// C# program to find
// count of non-prime
// divisors of given number
using System;
using System.Collections.Generic;
class GFG{
 
// Function to factors
// of the given number
static List<int> getFactorization(int x)
{
  int count = 0;
  List<int> v = new List<int>();
 
  // Loop to find the
  // divisors of the number 2
  while (x % 2 == 0)
  {
    count++;
    x = x / 2;
  }
   
  if (count != 0)
    v.Add(count);
 
  // Loop to find the divisors
  // of the given number upto
  // SQRT(N)
  for (int i = 3;
           i <= Math.Sqrt(x); i += 2)
  {
    count = 0;
    while (x % i == 0)
    {
      count++;
      x /= i;
    }
     
    if (count != 0)
      v.Add(count);
  }
 
  // Condition to check if
  // the rest number is also
  // a prime number
  if (x > 1)
  {
    v.Add(1);
  }
  return v;
}
 
// Function to find the non-prime
// divisors of the given number
static int nonPrimeDivisors(int N)
{
  List<int> v = getFactorization(N);
  int ret = 1;
 
  // Loop to count the number of
  // the total divisors of given number
  for (int i = 0; i < v.Count; i++)
    ret = ret * (v[i] + 1);
 
  ret = ret - v.Count;
  return ret;
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 8;
 
  // Function Call
  Console.WriteLine(nonPrimeDivisors(N));
}
}
 
// This code is contributed by gauravrajput1

                    

Javascript

<script>
 
// Javascript program to find
// count of non-prime
// divisors of given number
 
// Function to factors
// of the given number
function getFactorization(x)
{
      let count = 0;
      let v = [];
  
      // Loop to find the
      // divisors of the number 2
      while (x % 2 == 0)
      {
        count++;
        x = Math.floor(x / 2);
      }
    
      if (count != 0)
        v.push(count);
  
      // Loop to find the divisors
      // of the given number upto
      // SQRT(N)
      for (let i = 3;
        i <= Math.floor(Math.sqrt(x)); i += 2)
      {
        count = 0;
        while (x % i == 0)
        {
              count++;
              x = Math.floor(x / i);
        }
      
        if (count != 0)
          v.push(count);
      }
  
      // Condition to check if
      // the rest number is also
      // a prime number
      if (x > 1)
      {
        v.push(1);
      }
      return v;
}
  
// Function to find the non-prime
// divisors of the given number
function nonPrimeDivisors(N)
{
      let v = getFactorization(N);
      let ret = 1;
  
      // Loop to count the number of
      // the total divisors of given number
      for (let i = 0; i < v.length; i++)
        ret = ret * (v[i] + 1);
  
      ret = ret - v.length;
      return ret;
}
 
// Driver Code
     
    let N = 8;
  
  // Function Call
  document.write(nonPrimeDivisors(N));
     
</script>

                    

Output
3






Approach 2:

One approach is to check all the divisors of the given number and count the divisors that are not prime.

Algorithm:

Initialize a count variable to 0.
For each number i from 1 to N, check if i is a divisor of N.
If i is a divisor of N, check if it is prime.
If i is not prime, increment the count variable.
Return the count variable.

C++

#include <iostream>
#include <cmath> // needed for sqrt() function
 
using namespace std;
 
// function to check if a number is prime
bool is_prime(int n) {
    if (n <= 1) { // 1 and numbers less than 1 are not prime
        return false;
    }
    for (int i = 2; i <= sqrt(n); i++) { // check divisibility from 2 to the square root of n
        if (n % i == 0) { // if n is divisible by i, then n is not a prime number
            return false;
        }
    }
    return true; // if no factors are found, then n is a prime number
}
 
// function to count the number of non-prime divisors of a given integer
int count_non_prime_divisors(int N) {
    int count = 0;
    for (int i = 1; i <= N; i++) {
        if (N % i == 0) { // if i is a divisor of N
            if (!is_prime(i)) { // if i is not a prime number, increment the count
                count += 1;
            }
        }
    }
    return count;
}
 
// main function to test the count_non_prime_divisors function
int main() {
    int N = 20;
    cout << "Number of non-prime divisors of " << N << " is " << count_non_prime_divisors(N) << endl;
    return 0;
}

                    

Java

import java.util.*;
 
public class Main {
 
  // helper method to check if an integer is prime
  public static boolean isPrime(int n)
  {
     
    // check if n is less than or equal to 1, which is not a prime number
    if (n <= 1) {
      return false;
    }
     
    // check if n is divisible by any integer from 2 to the square root of n
    for (int i = 2; i <= Math.sqrt(n); i++) {
      if (n % i == 0) { // if it is divisible, then n is not prime
        return false;
      }
    }
     
    // if n is not divisible by any integer from 2
    // to the square root of n, then n is prime
    return true;
  }
 
  // helper method to count the number of non-prime divisors of an integer N
  public static int countNonPrimeDivisors(int N)
  {
     
    int count = 0; // initialize the count of non-prime divisors to 0
    // loop through all integers from 1 to N
    for (int i = 1; i <= N; i++) {
      if (N % i == 0) { // if i is a divisor of N
        if (!isPrime(i)) { // if i is not prime
          count += 1; // increment the count of non-prime divisors
        }
      }
    }
     
    // return the count of non-prime divisors of N
    return count;
  }
 
  // main method to test the countNonPrimeDivisors method
  public static void main(String[] args)
  {
    int N = 20; // test value of N
    // print the number of non-prime divisors of
    // N using the countNonPrimeDivisors method
    System.out.println("Number of non-prime divisors of " + N + " is " + countNonPrimeDivisors(N));
  }
}

                    

Python3

def count_non_prime_divisors(N):
    count = 0
    for i in range(1, N+1):
        if N % i == 0:
            if is_prime(i) == False:
                count += 1
    return count
 
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True
 
N = 20
print("Number of non-prime divisors of", N, "is", count_non_prime_divisors(N))

                    

C#

using System;
 
class GFG {
    // Function to check if a number is prime
    static bool IsPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.Sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
 
    // Function to count the number of non-prime divisors
  // of a given integer
    static int CountNonPrimeDivisors(int N) {
        int count = 0;
        for (int i = 1; i <= N; i++) {
            if (N % i == 0) {
                if (!IsPrime(i)) {
                    count += 1;
                }
            }
        }
        return count;
    }
 
    // Main function to test the CountNonPrimeDivisors function
    static void Main() {
        int N = 20;
        Console.WriteLine("Number of non-prime divisors of " + N + " is " + CountNonPrimeDivisors(N));
    }
}

                    

Javascript

// function to check if a number is prime
function isPrime(n) {
    if (n <= 1) { // 1 and numbers less than 1 are not prime
        return false;
    }
    for (let i = 2; i <= Math.sqrt(n); i++) { // check divisibility from 2 to the square root of n
        if (n % i === 0) { // if n is divisible by i, then n is not a prime number
            return false;
        }
    }
    return true; // if no factors are found, then n is a prime number
}
 
// function to count the number of non-prime divisors of a given integer
function countNonPrimeDivisors(N) {
    let count = 0;
    for (let i = 1; i <= N; i++) {
        if (N % i === 0) { // if i is a divisor of N
            if (!isPrime(i)) { // if i is not a prime number, increment the count
                count += 1;
            }
        }
    }
    return count;
}
 
// main function to test the countNonPrimeDivisors function
 
    let N = 20;
    console.log(`Number of non-prime divisors of ${N} is ${countNonPrimeDivisors(N)}`);

                    

Output
Number of non-prime divisors of 20 is 4






Time Complexity: O(N * sqrt(N)), as we need to check all the divisors of N and for each divisor, we check if it is prime, which takes O(sqrt(N)) time.
Auxiliary Space: O(1), as we are not using any extra space.



Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads