Formas de expresar un número como producto de dos factores diferentes

Dado un número n, escribe un programa para calcular el número de formas en que los números pueden expresarse como el producto de dos factores diferentes.
 

Ejemplos: 

Input : 12
Output : 3
12 can be expressed as 1 * 12, 2 * 6 and 3*4. 

Input : 36
Output : 4
36 can be expressed as 1 * 36, 2 * 18, 3 * 12 and 4 * 9.
All factors of 12 are = 1, 2, 3, 4, 6, 12

We can observe that factors always exist in
pair which is equal to number.

Here (1, 12), (2, 6) and (3, 4) are such pairs.

Como un número se puede expresar como el producto de dos factores, solo necesitamos encontrar el número de factores del número hasta la raíz cuadrada del número. Y solo necesitamos encontrar solo pares diferentes, por lo que en el caso de un cuadrado perfecto no incluimos ese factor.
 

C++

// CPP program to find number of ways
// in which number expressed as
// product of two different factors
#include <bits/stdc++.h>
using namespace std;
 
// To count number of ways in which
// number expressed as product
// of two different numbers
int countWays(int n)
{
    // To store count of such pairs
    int count = 0;
 
    // Counting number of pairs
    // upto sqrt(n) - 1
    for (int i = 1; i * i < n; i++)
        if (n % i == 0)
            count++;
 
    // To return count of pairs
    return count;
}
 
// Driver program to test countWays()
int main()
{
    int n = 12;
    cout << countWays(n) << endl;
    return 0;
}

Java

// Java program to find number of ways
// in which number expressed as
// product of two different factors
public class Main {
 
    // To count number of ways in which
    // number expressed as product
    // of two different numbers
    static int countWays(int n)
    {
        // To store count of such pairs
        int count = 0;
 
        // Counting number of pairs
        // upto sqrt(n) - 1
        for (int i = 1; i * i < n; i++)
            if (n % i == 0)
                count++;
 
        // To return count of pairs
        return count;
    }
 
    // Driver program to test countWays()
    public static void main(String[] args)
    {
        int n = 12;
        System.out.println(countWays(n));
    }
}

Python 3

# Python 3 program to find number of ways
# in which number expressed as
# product of two different factors
 
# To count number of ways in which
# number expressed as product
# of two different numbers
def countWays(n):
     
    # To store count of such pairs
    count = 0
    i = 1
     
    # Counting number of pairs
    # upto sqrt(n) - 1
    while ((i * i)<n) :
        if(n % i == 0):
            count += 1   
        i += 1
         
    # To return count of pairs
    return count
 
# Driver program to test countWays()
n = 12
print (countWays(n))
 
# This code is contributed
# by Azkia Anam.

C#

// C# program to find number of ways
// in which number expressed as
// product of two different factors
using System;
 
public class main {
 
    // To count number of ways in which
    // number expressed as product
    // of two different numbers
    static int countWays(int n)
    {
 
        // To store count of such pairs
        int count = 0;
 
        // Counting number of pairs
        // upto sqrt(n) - 1
        for (int i = 1; i * i < n; i++)
            if (n % i == 0)
                count++;
 
        // To return count of pairs
        return count;
    }
 
    // Driver program to test countWays()
    public static void Main()
    {
        int n = 12;
 
        Console.WriteLine(countWays(n));
    }
}
 
// This code is contributed by Anant Agarwal.

PHP

<?php
// PHP program to find number of ways
// in which number expressed as
// product of two different factors
 
// To count number of ways in which
// number expressed as product
// of two different numbers
function countWays($n)
{
    // To store count of such pairs
    $count = 0;
 
    // Counting number of pairs
    // upto sqrt(n) - 1
    for ($i = 1; $i * $i < $n; $i++)
        if ($n % $i == 0)
            $count++;
 
    // To return count of pairs
    return $count;
}
 
// Driver Code
$n = 12;
echo countWays($n), "\n";
 
// This code is contributed by ajit
?>

Javascript

<script>
// JavaScript program to find number of ways
// in which number expressed as
// product of two different factors
 
    // To count number of ways in which
    // number expressed as product
    // of two different numbers
    function countWays(n)
    {
        // To store count of such pairs
        let count = 0;
 
        // Counting number of pairs
        // upto sqrt(n) - 1
        for (let i = 1; i * i < n; i++)
            if (n % i == 0)
                count++;
 
        // To return count of pairs
        return count;
    }
  
// Driver Code
 
    let n = 12;
    document.write(countWays(n));
         
</script>

Producción:  

3

Complejidad temporal: O(√n) 
Espacio auxiliar: O(1)

Este artículo es una contribución de Aarti_Rathi y nuclode . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 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 *