Encuentra el número total de factores compuestos para un número dado

Dado un número entero N , la tarea es encontrar el número total de factores compuestos de N . Los factores compuestos de un número son los factores que no son primos.
Ejemplos: 
 

Entrada: N = 24 
Salida:
1, 2, 3, 4, 6, 8, 12 y 24 son los factores de 24. 
De los cuales solo 4, 6, 8, 12 y 24 son compuestos.
Entrada: N = 100 
Salida:
 

Acercarse: 
 

  • Encuentre todos los factores de N y guárdelos en una variable totalFactors
  • Encuentre todos los factores primos de N y guárdelos en una variable primeFactors
  • Ahora, el total de factores compuestos será totalFactors – primeFactors – 1 (se resta 1 porque 1 no es ni primo ni compuesto).

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count
// of prime factors of n
int composite_factors(int n)
{
    int count = 0;
    int i, j;
 
    // Initialise array with 0
    int a[n + 1] = { 0 };
    for (i = 1; i <= n; ++i) {
        if (n % i == 0) {
 
            // Stored i value into an array
            a[i] = i;
        }
    }
 
    // Every non-zero value at a[i] denotes
    // that i is a factor of n
    for (i = 2; i <= n; i++) {
        j = 2;
        int p = 1;
 
        // Find if i is prime
        while (j < a[i]) {
            if (a[i] % j == 0) {
                p = 0;
                break;
            }
            j++;
        }
 
        // If i is a factor of n
        // and i is not prime
        if (p == 0 && a[i] != 0) {
            count++;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int n = 100;
 
    cout << composite_factors(n);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class Gfg
{
 
// Function to return the count
// of prime factors of n
public static int composite_factors(int n)
{
    int count = 0;
    int i, j;
 
    // Initialise array with 0
    int[] a=new int[n+1];
    for( i = 0; i < n; i++)
    {
        a[i]=0;
    }
    for (i = 1; i <= n; ++i)
    {
        if (n % i == 0)
        {
 
            // Stored i value into an array
            a[i] = i;
        }
    }
 
    // Every non-zero value at a[i] denotes
    // that i is a factor of n
    for (i = 2; i <= n; i++)
    {
        j = 2;
        int p = 1;
 
        // Find if i is prime
        while (j < a[i])
        {
            if (a[i] % j == 0)
            {
                p = 0;
                break;
            }
            j++;
        }
 
        // If i is a factor of n
        // and i is not prime
        if (p == 0 && a[i] != 0)
        {
            count++;
        }
     
}
    return count;
}
 
 
// Driver code
public static void main(String[] args)
{
    int n = 100;
     
    System.out.println(composite_factors(n));
 
}
}
 
// This code is contributed by nidhi16bcs2007

Python3

# Python3 implementation of the approach
 
# Function to return the count
# of prime factors of n
def composite_factors(n) :
 
    count = 0;
     
    # Initialise array with 0
    a = [0]*(n + 1) ;
     
    for i in range(1, n + 1) :
        if (n % i == 0) :
 
            # Stored i value into an array
            a[i] = i;
 
    # Every non-zero value at a[i] denotes
    # that i is a factor of n
    for i in range(2,n + 1) :
        j = 2;
        p = 1;
 
        # Find if i is prime
        while (j < a[i]) :
            if (a[i] % j == 0) :
                p = 0;
                break;
                 
            j += 1;
 
 
        # If i is a factor of n
        # and i is not prime
        if (p == 0 and a[i] != 0) :
            count += 1;
 
    return count;
 
 
# Driver code
if __name__ == "__main__" :
 
    n = 100;
 
    print(composite_factors(n));
     
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the count
// of prime factors of n
static int composite_factors(int n)
{
    int count = 0;
    int i, j;
 
    // Initialise array with 0
    int[] a = new int[n + 1];
    for( i = 0; i < n; i++)
    {
        a[i]=0;
    }
    for (i = 1; i <= n; ++i)
    {
        if (n % i == 0)
        {
 
            // Stored i value into an array
            a[i] = i;
        }
    }
 
    // Every non-zero value at a[i] denotes
    // that i is a factor of n
    for (i = 2; i <= n; i++)
    {
        j = 2;
        int p = 1;
 
        // Find if i is prime
        while (j < a[i])
        {
            if (a[i] % j == 0)
            {
                p = 0;
                break;
            }
            j+=1;
        }
 
        // If i is a factor of n
        // and i is not prime
        if (p == 0 && a[i] != 0)
        {
            count += 1;
        }
 
}
    return count;
}
 
 
// Driver code
public static void Main()
{
    int n = 100;
 
    Console.WriteLine(composite_factors(n));
}
}
 
// This code is contributed by mohit kumar 29

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to return the count
// of prime factors of n
function composite_factors(n)
{
    var count = 0;
    var i, j;
 
    // Initialise array with 0
    var a = Array(n + 1).fill(0);
    for (i = 1; i <= n; ++i) {
        if (n % i == 0) {
 
            // Stored i value into an array
            a[i] = i;
        }
    }
 
    // Every non-zero value at a[i] denotes
    // that i is a factor of n
    for (i = 2; i <= n; i++) {
        j = 2;
        var p = 1;
 
        // Find if i is prime
        while (j < a[i]) {
            if (a[i] % j == 0) {
                p = 0;
                break;
            }
            j++;
        }
 
        // If i is a factor of n
        // and i is not prime
        if (p == 0 && a[i] != 0) {
            count++;
        }
    }
 
    return count;
}
 
// Driver code
    var n = 100;
 
    document.write(composite_factors(n));
 
</script>
Producción: 

6

 

Complejidad temporal: O(n*val) donde n es el número dado y val es el factor más grande de n.

Espacio Auxiliar: O(n)

Publicación traducida automáticamente

Artículo escrito por nidhiva y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *